2015-06-26 09:55:13 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import requests
|
2015-06-30 06:49:35 +03:00
|
|
|
import telebot
|
2015-06-30 08:20:44 +03:00
|
|
|
from telebot import types
|
2015-08-31 12:46:18 +03:00
|
|
|
from telebot import util
|
2015-06-30 06:49:35 +03:00
|
|
|
|
2015-07-20 04:56:17 +03:00
|
|
|
logger = telebot.logger
|
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
def _make_request(token, method_name, method='get', params=None, files=None):
|
2015-07-02 14:43:49 +03:00
|
|
|
"""
|
|
|
|
Makes a request to the Telegram API.
|
|
|
|
:param token: The bot's API token. (Created with @BotFather)
|
|
|
|
:param method_name: Name of the API method to be called. (E.g. 'getUpdates')
|
|
|
|
:param method: HTTP method to be used. Defaults to 'get'.
|
|
|
|
:param params: Optional parameters. Should be a dictionary with key-value pairs.
|
|
|
|
:param files: Optional files.
|
2015-08-19 22:29:17 +03:00
|
|
|
:return: The result parsed to a JSON dictionary.
|
2015-07-02 14:43:49 +03:00
|
|
|
"""
|
2015-07-01 23:16:13 +03:00
|
|
|
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
|
|
|
|
result = requests.request(method, request_url, params=params, files=files)
|
2015-07-20 04:56:17 +03:00
|
|
|
logger.debug(result.text)
|
2015-08-19 22:29:17 +03:00
|
|
|
return _check_result(method_name, result)['result']
|
|
|
|
|
|
|
|
|
|
|
|
def _check_result(method_name, result):
|
|
|
|
"""
|
|
|
|
Checks whether `result` is a valid API response.
|
|
|
|
A result is considered invalid if:
|
|
|
|
- The server returned an HTTP response code other than 200
|
|
|
|
- The content of the result is invalid JSON.
|
|
|
|
- The method call was unsuccessful (The JSON 'ok' field equals False)
|
|
|
|
|
|
|
|
:raises ApiException: if one of the above listed cases is applicable
|
|
|
|
:param method_name: The name of the method called
|
|
|
|
:param result: The returned result of the method request
|
|
|
|
:return: The result parsed to a JSON dictionary.
|
|
|
|
"""
|
2015-07-01 23:16:13 +03:00
|
|
|
if result.status_code != 200:
|
2015-08-19 22:29:17 +03:00
|
|
|
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
|
|
|
|
.format(result.status_code, result.reason, result.text)
|
|
|
|
raise ApiException(msg, method_name, result)
|
|
|
|
|
2015-07-01 23:16:13 +03:00
|
|
|
try:
|
|
|
|
result_json = result.json()
|
|
|
|
except:
|
2015-08-19 22:29:17 +03:00
|
|
|
msg = 'The server returned an invalid JSON response. Response body:\n[{0}]'\
|
|
|
|
.format(result.text)
|
|
|
|
raise ApiException(msg, method_name, result)
|
|
|
|
|
|
|
|
if not result_json['ok']:
|
|
|
|
msg = 'Error code: {0} Description: {1}'\
|
|
|
|
.format(result_json['error_code'], result_json['description'])
|
|
|
|
raise ApiException(msg, method_name, result)
|
|
|
|
return result_json
|
2015-07-01 23:16:13 +03:00
|
|
|
|
|
|
|
|
2015-06-26 10:46:02 +03:00
|
|
|
def get_me(token):
|
2015-07-01 23:16:13 +03:00
|
|
|
method_url = 'getMe'
|
|
|
|
return _make_request(token, method_url)
|
2015-06-26 09:55:13 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-06-26 09:55:13 +03:00
|
|
|
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None):
|
2015-06-26 10:46:02 +03:00
|
|
|
"""
|
|
|
|
Use this method to send text messages. On success, the sent Message is returned.
|
|
|
|
:param token:
|
|
|
|
:param chat_id:
|
|
|
|
:param text:
|
|
|
|
:param disable_web_page_preview:
|
|
|
|
:param reply_to_message_id:
|
|
|
|
:param reply_markup:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-06-26 09:55:13 +03:00
|
|
|
method_url = r'sendMessage'
|
2015-06-26 10:19:05 +03:00
|
|
|
payload = {'chat_id': str(chat_id), 'text': text}
|
2015-06-26 09:55:13 +03:00
|
|
|
if disable_web_page_preview:
|
2015-06-26 10:19:05 +03:00
|
|
|
payload['disable_web_page_preview'] = disable_web_page_preview
|
2015-06-26 09:55:13 +03:00
|
|
|
if reply_to_message_id:
|
2015-06-26 10:19:05 +03:00
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
2015-06-26 09:55:13 +03:00
|
|
|
if reply_markup:
|
2015-07-02 14:43:49 +03:00
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
2015-07-25 21:59:36 +03:00
|
|
|
return _make_request(token, method_url, params=payload, method='post')
|
2015-06-26 13:02:30 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
2015-07-02 23:32:18 +03:00
|
|
|
def get_updates(token, offset=None, limit=None, timeout=None):
|
2015-06-26 13:02:30 +03:00
|
|
|
method_url = r'getUpdates'
|
2015-07-02 23:32:18 +03:00
|
|
|
payload = {}
|
|
|
|
if offset:
|
|
|
|
payload['offset'] = offset
|
|
|
|
if limit:
|
|
|
|
payload['limit'] = limit
|
|
|
|
if timeout:
|
|
|
|
payload['timeout'] = timeout
|
|
|
|
return _make_request(token, method_url, params=payload)
|
2015-06-26 17:16:11 +03:00
|
|
|
|
2015-07-01 23:34:40 +03:00
|
|
|
|
2015-06-30 17:40:44 +03:00
|
|
|
def get_user_profile_photos(token, user_id, offset=None, limit=None):
|
|
|
|
method_url = r'getUserProfilePhotos'
|
|
|
|
payload = {'user_id': user_id}
|
|
|
|
if offset:
|
|
|
|
payload['offset'] = offset
|
|
|
|
if limit:
|
|
|
|
payload['limit'] = limit
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload)
|
2015-06-30 17:40:44 +03:00
|
|
|
|
2015-06-26 17:35:52 +03:00
|
|
|
|
|
|
|
def forward_message(token, chat_id, from_chat_id, message_id):
|
2015-06-26 17:16:11 +03:00
|
|
|
method_url = r'forwardMessage'
|
2015-06-26 17:35:52 +03:00
|
|
|
payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id}
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload)
|
2015-06-26 20:53:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None):
|
|
|
|
method_url = r'sendPhoto'
|
|
|
|
payload = {'chat_id': chat_id}
|
2015-07-14 08:24:32 +03:00
|
|
|
files = None
|
2015-08-31 12:46:18 +03:00
|
|
|
if not util.is_string(photo):
|
2015-07-14 08:24:32 +03:00
|
|
|
files = {'photo': photo}
|
|
|
|
else:
|
|
|
|
payload['photo'] = photo
|
2015-06-26 20:53:07 +03:00
|
|
|
if caption:
|
|
|
|
payload['caption'] = caption
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-07-02 14:43:49 +03:00
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload, files=files, method='post')
|
2015-06-27 16:55:45 +03:00
|
|
|
|
2015-06-26 20:53:07 +03:00
|
|
|
|
2015-06-27 17:11:18 +03:00
|
|
|
def send_location(token, chat_id, latitude, longitude, reply_to_message_id=None, reply_markup=None):
|
|
|
|
method_url = r'sendLocation'
|
|
|
|
payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude}
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-07-02 14:43:49 +03:00
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload)
|
2015-06-27 17:11:18 +03:00
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
|
|
|
|
def send_chat_action(token, chat_id, action):
|
2015-06-28 12:56:32 +03:00
|
|
|
method_url = r'sendChatAction'
|
|
|
|
payload = {'chat_id': chat_id, 'action': action}
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload)
|
2015-06-27 17:11:18 +03:00
|
|
|
|
2015-06-30 06:54:04 +03:00
|
|
|
|
2015-08-01 05:12:15 +03:00
|
|
|
def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None):
|
|
|
|
method_url = r'sendVideo'
|
|
|
|
payload = {'chat_id': chat_id}
|
|
|
|
files = None
|
2015-08-31 12:46:18 +03:00
|
|
|
if not util.is_string(data):
|
2015-08-01 05:12:15 +03:00
|
|
|
files = {'video': data}
|
|
|
|
else:
|
|
|
|
payload['video'] = data
|
|
|
|
if duration:
|
|
|
|
payload['duration'] = duration
|
|
|
|
if caption:
|
|
|
|
payload['caption'] = caption
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
|
|
|
return _make_request(token, method_url, params=payload, files=files, method='post')
|
|
|
|
|
|
|
|
|
2015-08-19 13:08:01 +03:00
|
|
|
def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None):
|
|
|
|
method_url = r'sendVoice'
|
|
|
|
payload = {'chat_id': chat_id}
|
|
|
|
files = None
|
2015-08-31 12:46:18 +03:00
|
|
|
if not util.is_string(voice):
|
2015-08-19 13:08:01 +03:00
|
|
|
files = {'voice': voice}
|
|
|
|
else:
|
|
|
|
payload['voice'] = voice
|
|
|
|
if duration:
|
|
|
|
payload['duration'] = duration
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
|
|
|
return _make_request(token, method_url, params=payload, files=files, method='post')
|
|
|
|
|
|
|
|
|
|
|
|
def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None,
|
|
|
|
reply_markup=None):
|
|
|
|
method_url = r'sendAudio'
|
|
|
|
payload = {'chat_id': chat_id}
|
|
|
|
files = None
|
2015-08-31 12:46:18 +03:00
|
|
|
if not util.is_string(audio):
|
2015-08-19 13:08:01 +03:00
|
|
|
files = {'audio': audio}
|
|
|
|
else:
|
|
|
|
payload['audio'] = audio
|
|
|
|
if duration:
|
|
|
|
payload['duration'] = duration
|
|
|
|
if performer:
|
|
|
|
payload['performer'] = performer
|
|
|
|
if title:
|
|
|
|
payload['title'] = title
|
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
|
|
|
return _make_request(token, method_url, params=payload, files=files, method='post')
|
|
|
|
|
|
|
|
|
2015-06-26 21:14:45 +03:00
|
|
|
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None):
|
|
|
|
method_url = get_method_by_type(data_type)
|
2015-06-26 20:53:07 +03:00
|
|
|
payload = {'chat_id': chat_id}
|
2015-07-14 08:24:32 +03:00
|
|
|
files = None
|
2015-08-31 12:46:18 +03:00
|
|
|
if not util.is_string(data):
|
2015-07-14 08:24:32 +03:00
|
|
|
files = {data_type: data}
|
|
|
|
else:
|
|
|
|
payload[data_type] = data
|
2015-06-26 20:53:07 +03:00
|
|
|
if reply_to_message_id:
|
|
|
|
payload['reply_to_message_id'] = reply_to_message_id
|
|
|
|
if reply_markup:
|
2015-07-02 14:43:49 +03:00
|
|
|
payload['reply_markup'] = _convert_markup(reply_markup)
|
2015-07-01 23:16:13 +03:00
|
|
|
return _make_request(token, method_url, params=payload, files=files, method='post')
|
2015-06-27 16:55:45 +03:00
|
|
|
|
2015-06-26 20:53:07 +03:00
|
|
|
|
2015-06-26 21:14:45 +03:00
|
|
|
def get_method_by_type(data_type):
|
|
|
|
if data_type == 'document':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendDocument'
|
2015-06-26 21:14:45 +03:00
|
|
|
if data_type == 'sticker':
|
2015-06-26 20:53:07 +03:00
|
|
|
return 'sendSticker'
|
2015-06-27 16:55:45 +03:00
|
|
|
|
|
|
|
|
2015-07-02 14:43:49 +03:00
|
|
|
def _convert_markup(markup):
|
2015-07-01 23:16:13 +03:00
|
|
|
if isinstance(markup, types.JsonSerializable):
|
2015-06-30 08:20:44 +03:00
|
|
|
return markup.to_json()
|
2015-07-01 19:17:33 +03:00
|
|
|
return markup
|
2015-06-27 16:55:45 +03:00
|
|
|
|
2015-07-02 02:18:23 +03:00
|
|
|
class ApiException(Exception):
|
2015-07-02 14:43:49 +03:00
|
|
|
"""
|
|
|
|
This class represents an Exception thrown when a call to the Telegram API fails.
|
2015-08-19 22:29:17 +03:00
|
|
|
In addition to an informative message, it has a `function_name` and a `result` attribute, which respectively
|
|
|
|
contain the name of the failed function and the returned result that made the function to be considered as
|
|
|
|
failed.
|
2015-07-02 14:43:49 +03:00
|
|
|
"""
|
2015-07-17 04:35:37 +03:00
|
|
|
|
2015-08-19 22:29:17 +03:00
|
|
|
def __init__(self, msg, function_name, result):
|
|
|
|
super(ApiException, self).__init__("A request to the Telegram API was unsuccessful. {0}".format(msg))
|
2015-07-02 14:43:49 +03:00
|
|
|
self.function_name = function_name
|
2015-06-27 16:55:45 +03:00
|
|
|
self.result = result
|