1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Correct processing of bool parameters. Some timeouts added.

This commit is contained in:
Badiboy 2020-05-16 17:34:56 +03:00
parent 046276b491
commit 2368421332
2 changed files with 235 additions and 135 deletions

View File

@ -636,7 +636,7 @@ class TeleBot:
apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id, apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id,
reply_markup, parse_mode, disable_notification, timeout)) reply_markup, parse_mode, disable_notification, timeout))
def forward_message(self, chat_id, from_chat_id, message_id, disable_notification=None): def forward_message(self, chat_id, from_chat_id, message_id, disable_notification=None, timeout=None):
""" """
Use this method to forward messages of any kind. Use this method to forward messages of any kind.
:param disable_notification: :param disable_notification:
@ -646,7 +646,7 @@ class TeleBot:
:return: API reply. :return: API reply.
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification)) apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification, timeout))
def delete_message(self, chat_id, message_id): def delete_message(self, chat_id, message_id):
""" """
@ -657,7 +657,10 @@ class TeleBot:
""" """
return apihelper.delete_message(self.token, chat_id, message_id) return apihelper.delete_message(self.token, chat_id, message_id)
def send_dice(self, chat_id, emoji=None, disable_notification=None, reply_to_message_id=None, reply_markup=None): def send_dice(
self, chat_id,
emoji=None, disable_notification=None, reply_to_message_id=None,
reply_markup=None, timeout=None):
""" """
Use this method to send dices. Use this method to send dices.
:param chat_id: :param chat_id:
@ -668,11 +671,13 @@ class TeleBot:
:return: Message :return: Message
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_dice(self.token, chat_id, emoji, disable_notification, reply_to_message_id, reply_markup) apihelper.send_dice(
self.token, chat_id, emoji, disable_notification, reply_to_message_id,
reply_markup, timeout)
) )
def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None, disable_notification=None): parse_mode=None, disable_notification=None, timeout=None):
""" """
Use this method to send photos. Use this method to send photos.
:param disable_notification: :param disable_notification:
@ -686,7 +691,7 @@ class TeleBot:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup, apihelper.send_photo(self.token, chat_id, photo, caption, reply_to_message_id, reply_markup,
parse_mode, disable_notification)) parse_mode, disable_notification, timeout))
def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None, def send_audio(self, chat_id, audio, caption=None, duration=None, performer=None, title=None,
reply_to_message_id=None, reply_markup=None, parse_mode=None, disable_notification=None, reply_to_message_id=None, reply_markup=None, parse_mode=None, disable_notification=None,
@ -747,8 +752,9 @@ class TeleBot:
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup, apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
parse_mode, disable_notification, timeout, caption=caption)) parse_mode, disable_notification, timeout, caption=caption))
def send_sticker(self, chat_id, data, reply_to_message_id=None, reply_markup=None, disable_notification=None, def send_sticker(
timeout=None): self, chat_id, data, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None):
""" """
Use this method to send .webp stickers. Use this method to send .webp stickers.
:param chat_id: :param chat_id:
@ -760,8 +766,9 @@ class TeleBot:
:return: API reply. :return: API reply.
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_data(self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup, apihelper.send_data(
disable_notification, timeout)) self.token, chat_id, data, 'sticker', reply_to_message_id, reply_markup,
disable_notification, timeout))
def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None, def send_video(self, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None): parse_mode=None, supports_streaming=None, disable_notification=None, timeout=None):
@ -820,7 +827,9 @@ class TeleBot:
apihelper.send_video_note(self.token, chat_id, data, duration, length, reply_to_message_id, reply_markup, apihelper.send_video_note(self.token, chat_id, data, duration, length, reply_to_message_id, reply_markup,
disable_notification, timeout)) disable_notification, timeout))
def send_media_group(self, chat_id, media, disable_notification=None, reply_to_message_id=None): def send_media_group(
self, chat_id, media,
disable_notification=None, reply_to_message_id=None, timeout=None):
""" """
send a group of photos or videos as an album. On success, an array of the sent Messages is returned. send a group of photos or videos as an album. On success, an array of the sent Messages is returned.
:param chat_id: :param chat_id:
@ -829,14 +838,16 @@ class TeleBot:
:param reply_to_message_id: :param reply_to_message_id:
:return: :return:
""" """
result = apihelper.send_media_group(self.token, chat_id, media, disable_notification, reply_to_message_id) result = apihelper.send_media_group(
self.token, chat_id, media, disable_notification, reply_to_message_id, timeout)
ret = [] ret = []
for msg in result: for msg in result:
ret.append(types.Message.de_json(msg)) ret.append(types.Message.de_json(msg))
return ret return ret
def send_location(self, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None, reply_markup=None, def send_location(
disable_notification=None): self, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None,
reply_markup=None, disable_notification=None, timeout=None):
""" """
Use this method to send point on the map. Use this method to send point on the map.
:param chat_id: :param chat_id:
@ -849,12 +860,12 @@ class TeleBot:
:return: API reply. :return: API reply.
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_location(self.token, chat_id, latitude, longitude, live_period, reply_to_message_id, apihelper.send_location(
reply_markup, self.token, chat_id, latitude, longitude, live_period, reply_to_message_id,
disable_notification)) reply_markup, disable_notification, timeout))
def edit_message_live_location(self, latitude, longitude, chat_id=None, message_id=None, def edit_message_live_location(self, latitude, longitude, chat_id=None, message_id=None,
inline_message_id=None, reply_markup=None): inline_message_id=None, reply_markup=None, timeout=None):
""" """
Use this method to edit live location Use this method to edit live location
:param latitude: :param latitude:
@ -866,10 +877,13 @@ class TeleBot:
:return: :return:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.edit_message_live_location(self.token, latitude, longitude, chat_id, message_id, apihelper.edit_message_live_location(
inline_message_id, reply_markup)) self.token, latitude, longitude, chat_id, message_id,
inline_message_id, reply_markup, timeout))
def stop_message_live_location(self, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None): def stop_message_live_location(
self, chat_id=None, message_id=None,
inline_message_id=None, reply_markup=None, timeout=None):
""" """
Use this method to stop updating a live location message sent by the bot Use this method to stop updating a live location message sent by the bot
or via the bot (for inline bots) before live_period expires or via the bot (for inline bots) before live_period expires
@ -880,10 +894,12 @@ class TeleBot:
:return: :return:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.stop_message_live_location(self.token, chat_id, message_id, inline_message_id, reply_markup)) apihelper.stop_message_live_location(
self.token, chat_id, message_id, inline_message_id, reply_markup, timeout))
def send_venue(self, chat_id, latitude, longitude, title, address, foursquare_id=None, disable_notification=None, def send_venue(
reply_to_message_id=None, reply_markup=None): self, chat_id, latitude, longitude, title, address, foursquare_id=None, disable_notification=None,
reply_to_message_id=None, reply_markup=None, timeout=None):
""" """
Use this method to send information about a venue. Use this method to send information about a venue.
:param chat_id: Integer or String : Unique identifier for the target chat or username of the target channel :param chat_id: Integer or String : Unique identifier for the target chat or username of the target channel
@ -898,18 +914,22 @@ class TeleBot:
:return: :return:
""" """
return types.Message.de_json( return types.Message.de_json(
apihelper.send_venue(self.token, chat_id, latitude, longitude, title, address, foursquare_id, apihelper.send_venue(
disable_notification, reply_to_message_id, reply_markup) self.token, chat_id, latitude, longitude, title, address, foursquare_id,
disable_notification, reply_to_message_id, reply_markup, timeout)
) )
def send_contact(self, chat_id, phone_number, first_name, last_name=None, disable_notification=None, def send_contact(
reply_to_message_id=None, reply_markup=None): self, chat_id, phone_number, first_name,
last_name=None, disable_notification=None,
reply_to_message_id=None, reply_markup=None, timeout=None):
return types.Message.de_json( return types.Message.de_json(
apihelper.send_contact(self.token, chat_id, phone_number, first_name, last_name, disable_notification, apihelper.send_contact(
reply_to_message_id, reply_markup) self.token, chat_id, phone_number, first_name, last_name, disable_notification,
reply_to_message_id, reply_markup, timeout)
) )
def send_chat_action(self, chat_id, action): def send_chat_action(self, chat_id, action, timeout=None):
""" """
Use this method when you need to tell the user that something is happening on the bot's side. Use this method when you need to tell the user that something is happening on the bot's side.
The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear
@ -919,7 +939,7 @@ class TeleBot:
'record_audio', 'upload_audio', 'upload_document', 'find_location', 'record_video_note', 'upload_video_note'. 'record_audio', 'upload_audio', 'upload_document', 'find_location', 'record_video_note', 'upload_video_note'.
:return: API reply. :type: boolean :return: API reply. :type: boolean
""" """
return apihelper.send_chat_action(self.token, chat_id, action) return apihelper.send_chat_action(self.token, chat_id, action, timeout)
def kick_chat_member(self, chat_id, user_id, until_date=None): def kick_chat_member(self, chat_id, user_id, until_date=None):
""" """
@ -941,9 +961,12 @@ class TeleBot:
""" """
return apihelper.unban_chat_member(self.token, chat_id, user_id) return apihelper.unban_chat_member(self.token, chat_id, user_id)
def restrict_chat_member(self, chat_id, user_id, until_date=None, can_send_messages=None, def restrict_chat_member(
can_send_media_messages=None, can_send_other_messages=None, self, chat_id, user_id, until_date=None,
can_add_web_page_previews=None, can_invite_users=None): can_send_messages=None, can_send_media_messages=None,
can_send_polls=None, can_send_other_messages=None,
can_add_web_page_previews=None, can_change_info=None,
can_invite_users=None, can_pin_messages=None):
""" """
Use this method to restrict a user in a supergroup. Use this method to restrict a user in a supergroup.
The bot must be an administrator in the supergroup for this to work and must have The bot must be an administrator in the supergroup for this to work and must have
@ -958,17 +981,23 @@ class TeleBot:
:param can_send_messages: Pass True, if the user can send text messages, contacts, locations and venues :param can_send_messages: Pass True, if the user can send text messages, contacts, locations and venues
:param can_send_media_messages Pass True, if the user can send audios, documents, photos, videos, video notes :param can_send_media_messages Pass True, if the user can send audios, documents, photos, videos, video notes
and voice notes, implies can_send_messages and voice notes, implies can_send_messages
:param can_send_polls: Pass True, if the user is allowed to send polls, implies can_send_messages
:param can_send_other_messages: Pass True, if the user can send animations, games, stickers and :param can_send_other_messages: Pass True, if the user can send animations, games, stickers and
use inline bots, implies can_send_media_messages use inline bots, implies can_send_media_messages
:param can_add_web_page_previews: Pass True, if the user may add web page previews to their messages, :param can_add_web_page_previews: Pass True, if the user may add web page previews to their messages,
implies can_send_media_messages implies can_send_media_messages
:param can_invite_users: Pass True, if the user is allowed to invite new users to the chat, :param can_change_info: Pass True, if the user is allowed to change the chat title, photo and other settings. Ignored in public supergroups
implies can_invite_users :param can_invite_users: Pass True, if the user is allowed to invite new users to the chat,
implies can_invite_users
:param can_pin_messages: Pass True, if the user is allowed to pin messages. Ignored in public supergroups
:return: types.Message :return: types.Message
""" """
return apihelper.restrict_chat_member(self.token, chat_id, user_id, until_date, can_send_messages, return apihelper.restrict_chat_member(
can_send_media_messages, can_send_other_messages, self.token, chat_id, user_id, until_date,
can_add_web_page_previews, can_invite_users) can_send_messages, can_send_media_messages,
can_send_polls, can_send_other_messages,
can_add_web_page_previews, can_change_info,
can_invite_users, can_pin_messages)
def promote_chat_member(self, chat_id, user_id, can_change_info=None, can_post_messages=None, def promote_chat_member(self, chat_id, user_id, can_change_info=None, can_post_messages=None,
can_edit_messages=None, can_delete_messages=None, can_invite_users=None, can_edit_messages=None, can_delete_messages=None, can_invite_users=None,
@ -1169,8 +1198,9 @@ class TeleBot:
return result return result
return types.Message.de_json(result) return types.Message.de_json(result)
def send_game(self, chat_id, game_short_name, disable_notification=None, reply_to_message_id=None, def send_game(
reply_markup=None): self, chat_id, game_short_name, disable_notification=None,
reply_to_message_id=None, reply_markup=None, timeout=None):
""" """
Used to send the game Used to send the game
:param chat_id: :param chat_id:
@ -1180,8 +1210,9 @@ class TeleBot:
:param reply_markup: :param reply_markup:
:return: :return:
""" """
result = apihelper.send_game(self.token, chat_id, game_short_name, disable_notification, reply_to_message_id, result = apihelper.send_game(
reply_markup) self.token, chat_id, game_short_name, disable_notification,
reply_to_message_id, reply_markup, timeout)
return types.Message.de_json(result) return types.Message.de_json(result)
def set_game_score(self, user_id, score, force=None, chat_id=None, message_id=None, inline_message_id=None, def set_game_score(self, user_id, score, force=None, chat_id=None, message_id=None, inline_message_id=None,
@ -1222,7 +1253,7 @@ class TeleBot:
start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None, start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None,
need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None, need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None,
is_flexible=None, disable_notification=None, reply_to_message_id=None, reply_markup=None, is_flexible=None, disable_notification=None, reply_to_message_id=None, reply_markup=None,
provider_data=None): provider_data=None, timeout=None):
""" """
Sends invoice Sends invoice
:param chat_id: :param chat_id:
@ -1248,19 +1279,19 @@ class TeleBot:
:param provider_data: :param provider_data:
:return: :return:
""" """
result = apihelper.send_invoice(self.token, chat_id, title, description, invoice_payload, provider_token, result = apihelper.send_invoice(
currency, prices, start_parameter, photo_url, photo_size, photo_width, self.token, chat_id, title, description, invoice_payload, provider_token,
photo_height, currency, prices, start_parameter, photo_url, photo_size, photo_width,
need_name, need_phone_number, need_email, need_shipping_address, is_flexible, photo_height, need_name, need_phone_number, need_email, need_shipping_address,
disable_notification, reply_to_message_id, reply_markup, provider_data) is_flexible, disable_notification, reply_to_message_id, reply_markup,
provider_data, timeout)
return types.Message.de_json(result) return types.Message.de_json(result)
def send_poll( def send_poll(
self, chat_id, self, chat_id, question, options,
question, options,
is_anonymous=None, type=None, allows_multiple_answers=None, correct_option_id=None, is_anonymous=None, type=None, allows_multiple_answers=None, correct_option_id=None,
explanation=None, explanation_parse_mode=None, open_period=None, close_date=None, is_closed=None, explanation=None, explanation_parse_mode=None, open_period=None, close_date=None, is_closed=None,
disable_notifications=False, reply_to_message_id=None, reply_markup=None): disable_notifications=False, reply_to_message_id=None, reply_markup=None, timeout=None):
""" """
Send polls Send polls
:param chat_id: :param chat_id:
@ -1290,7 +1321,7 @@ class TeleBot:
question, options, question, options,
is_anonymous, type, allows_multiple_answers, correct_option_id, is_anonymous, type, allows_multiple_answers, correct_option_id,
explanation, explanation_parse_mode, open_period, close_date, is_closed, explanation, explanation_parse_mode, open_period, close_date, is_closed,
disable_notifications, reply_to_message_id, reply_markup)) disable_notifications, reply_to_message_id, reply_markup, timeout))
def stop_poll(self, chat_id, message_id): def stop_poll(self, chat_id, message_id):
""" """

View File

@ -57,10 +57,13 @@ def _make_request(token, method_name, method='get', params=None, files=None):
if files and format_header_param: if files and format_header_param:
fields.format_header_param = _no_encode(format_header_param) fields.format_header_param = _no_encode(format_header_param)
if params: if params:
if 'timeout' in params: read_timeout = params['timeout'] + 10 if 'timeout' in params:
if 'connect-timeout' in params: connect_timeout = params['connect-timeout'] + 10 read_timeout = params.pop('timeout') + 10
result = _get_req_session().request(method, request_url, params=params, files=files, if 'connect-timeout' in params:
timeout=(connect_timeout, read_timeout), proxies=proxy) connect_timeout = params.pop('connect-timeout') + 10
result = _get_req_session().request(
method, request_url, params=params, files=files,
timeout=(connect_timeout, read_timeout), proxies=proxy)
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8'))) logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
return _check_result(method_name, result)['result'] return _check_result(method_name, result)['result']
@ -128,8 +131,10 @@ def download_file(token, file_path):
return result.content return result.content
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None, def send_message(
parse_mode=None, disable_notification=None, timeout=None): token, chat_id, text,
disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None, disable_notification=None, timeout=None):
""" """
Use this method to send text messages. On success, the sent Message is returned. Use this method to send text messages. On success, the sent Message is returned.
:param token: :param token:
@ -140,11 +145,12 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m
:param reply_markup: :param reply_markup:
:param parse_mode: :param parse_mode:
:param disable_notification: :param disable_notification:
:param timeout:
:return: :return:
""" """
method_url = r'sendMessage' method_url = r'sendMessage'
payload = {'chat_id': str(chat_id), 'text': text} payload = {'chat_id': str(chat_id), 'text': text}
if disable_web_page_preview: if disable_web_page_preview is not None:
payload['disable_web_page_preview'] = disable_web_page_preview payload['disable_web_page_preview'] = disable_web_page_preview
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
@ -152,7 +158,7 @@ def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_m
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -251,30 +257,41 @@ def get_chat_member(token, chat_id, user_id):
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def forward_message(token, chat_id, from_chat_id, message_id, disable_notification=None): def forward_message(
token, chat_id, from_chat_id, message_id,
disable_notification=None, timeout=None):
method_url = r'forwardMessage' method_url = r'forwardMessage'
payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id} payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id}
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def send_dice(token, chat_id, emoji=None, disable_notification=None, reply_to_message_id=None, reply_markup=None): def send_dice(
token, chat_id,
emoji=None, disable_notification=None, reply_to_message_id=None,
reply_markup=None, timeout=None):
method_url = r'sendDice' method_url = r'sendDice'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
if emoji: if emoji:
payload['emoji'] = emoji payload['emoji'] = emoji
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None, def send_photo(
parse_mode=None, disable_notification=None): token, chat_id, photo,
caption=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None, disable_notification=None, timeout=None):
method_url = r'sendPhoto' method_url = r'sendPhoto'
payload = {'chat_id': chat_id} payload = {'chat_id': chat_id}
files = None files = None
@ -290,25 +307,36 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload, files=files, method='post') return _make_request(token, method_url, params=payload, files=files, method='post')
def send_media_group(token, chat_id, media, disable_notification=None, reply_to_message_id=None): def send_media_group(
token, chat_id, media,
disable_notification=None, reply_to_message_id=None,
timeout=None):
method_url = r'sendMediaGroup' method_url = r'sendMediaGroup'
media_json, files = _convert_input_media_array(media) media_json, files = _convert_input_media_array(media)
payload = {'chat_id': chat_id, 'media': media_json} payload = {'chat_id': chat_id, 'media': media_json}
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
return _make_request(token, method_url, params=payload, method='post' if files else 'get', if timeout:
files=files if files else None) payload['connect-timeout'] = timeout
return _make_request(
token, method_url, params=payload,
method='post' if files else 'get',
files=files if files else None)
def send_location(token, chat_id, latitude, longitude, live_period=None, reply_to_message_id=None, reply_markup=None, def send_location(
disable_notification=None): token, chat_id, latitude, longitude,
live_period=None, reply_to_message_id=None, reply_markup=None,
disable_notification=None, timeout=None):
method_url = r'sendLocation' method_url = r'sendLocation'
payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude} payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude}
if live_period: if live_period:
@ -317,13 +345,15 @@ def send_location(token, chat_id, latitude, longitude, live_period=None, reply_t
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def edit_message_live_location(token, latitude, longitude, chat_id=None, message_id=None, def edit_message_live_location(token, latitude, longitude, chat_id=None, message_id=None,
inline_message_id=None, reply_markup=None): inline_message_id=None, reply_markup=None, timeout=None):
method_url = r'editMessageLiveLocation' method_url = r'editMessageLiveLocation'
payload = {'latitude': latitude, 'longitude': longitude} payload = {'latitude': latitude, 'longitude': longitude}
if chat_id: if chat_id:
@ -334,11 +364,14 @@ def edit_message_live_location(token, latitude, longitude, chat_id=None, message
payload['inline_message_id'] = inline_message_id payload['inline_message_id'] = inline_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def stop_message_live_location(token, chat_id=None, message_id=None, def stop_message_live_location(
inline_message_id=None, reply_markup=None): token, chat_id=None, message_id=None,
inline_message_id=None, reply_markup=None, timeout=None):
method_url = r'stopMessageLiveLocation' method_url = r'stopMessageLiveLocation'
payload = {} payload = {}
if chat_id: if chat_id:
@ -349,42 +382,54 @@ def stop_message_live_location(token, chat_id=None, message_id=None,
payload['inline_message_id'] = inline_message_id payload['inline_message_id'] = inline_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def send_venue(token, chat_id, latitude, longitude, title, address, foursquare_id=None, disable_notification=None, def send_venue(
reply_to_message_id=None, reply_markup=None): token, chat_id, latitude, longitude, title, address,
foursquare_id=None, disable_notification=None,
reply_to_message_id=None, reply_markup=None, timeout=None):
method_url = r'sendVenue' method_url = r'sendVenue'
payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude, 'title': title, 'address': address} payload = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude, 'title': title, 'address': address}
if foursquare_id: if foursquare_id:
payload['foursquare_id'] = foursquare_id payload['foursquare_id'] = foursquare_id
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def send_contact(token, chat_id, phone_number, first_name, last_name=None, disable_notification=None, def send_contact(
reply_to_message_id=None, reply_markup=None): token, chat_id, phone_number, first_name,
last_name=None, disable_notification=None,
reply_to_message_id=None, reply_markup=None, timeout=None):
method_url = r'sendContact' method_url = r'sendContact'
payload = {'chat_id': chat_id, 'phone_number': phone_number, 'first_name': first_name} payload = {'chat_id': chat_id, 'phone_number': phone_number, 'first_name': first_name}
if last_name: if last_name:
payload['last_name'] = last_name payload['last_name'] = last_name
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
def send_chat_action(token, chat_id, action): def send_chat_action(token, chat_id, action, timeout=None):
method_url = r'sendChatAction' method_url = r'sendChatAction'
payload = {'chat_id': chat_id, 'action': action} payload = {'chat_id': chat_id, 'action': action}
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
@ -407,9 +452,9 @@ def send_video(token, chat_id, data, duration=None, caption=None, reply_to_messa
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if supports_streaming: if supports_streaming is not None:
payload['supports_streaming'] = supports_streaming payload['supports_streaming'] = supports_streaming
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -435,7 +480,7 @@ def send_animation(token, chat_id, data, duration=None, caption=None, reply_to_m
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -461,7 +506,7 @@ def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_mess
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -487,7 +532,7 @@ def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_m
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -517,7 +562,7 @@ def send_audio(token, chat_id, audio, caption=None, duration=None, performer=Non
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -539,7 +584,7 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if parse_mode and data_type == 'document': if parse_mode and data_type == 'document':
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if timeout: if timeout:
payload['connect-timeout'] = timeout payload['connect-timeout'] = timeout
@ -569,46 +614,56 @@ def unban_chat_member(token, chat_id, user_id):
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
def restrict_chat_member(token, chat_id, user_id, until_date=None, can_send_messages=None, def restrict_chat_member(
can_send_media_messages=None, can_send_other_messages=None, token, chat_id, user_id, until_date=None,
can_add_web_page_previews=None, can_invite_users=None): can_send_messages=None, can_send_media_messages=None,
can_send_polls=None, can_send_other_messages=None,
can_add_web_page_previews=None, can_change_info=None,
can_invite_users=None, can_pin_messages=None):
method_url = 'restrictChatMember' method_url = 'restrictChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id} payload = {'chat_id': chat_id, 'user_id': user_id}
if until_date: if until_date is not None:
payload['until_date'] = until_date payload['until_date'] = until_date
if can_send_messages: if can_send_messages is not None:
payload['can_send_messages'] = can_send_messages payload['can_send_messages'] = can_send_messages
if can_send_media_messages: if can_send_media_messages is not None:
payload['can_send_media_messages'] = can_send_media_messages payload['can_send_media_messages'] = can_send_media_messages
if can_send_other_messages: if can_send_polls is not None:
payload['can_send_polls'] = can_send_polls
if can_send_other_messages is not None:
payload['can_send_other_messages'] = can_send_other_messages payload['can_send_other_messages'] = can_send_other_messages
if can_add_web_page_previews: if can_add_web_page_previews is not None:
payload['can_add_web_page_previews'] = can_add_web_page_previews payload['can_add_web_page_previews'] = can_add_web_page_previews
if can_invite_users: if can_change_info is not None:
payload['can_change_info'] = can_change_info
if can_invite_users is not None:
payload['can_invite_users'] = can_invite_users payload['can_invite_users'] = can_invite_users
if can_pin_messages is not None:
payload['can_pin_messages'] = can_pin_messages
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
def promote_chat_member(token, chat_id, user_id, can_change_info=None, can_post_messages=None, def promote_chat_member(
can_edit_messages=None, can_delete_messages=None, can_invite_users=None, token, chat_id, user_id, can_change_info=None, can_post_messages=None,
can_restrict_members=None, can_pin_messages=None, can_promote_members=None): can_edit_messages=None, can_delete_messages=None, can_invite_users=None,
can_restrict_members=None, can_pin_messages=None, can_promote_members=None):
method_url = 'promoteChatMember' method_url = 'promoteChatMember'
payload = {'chat_id': chat_id, 'user_id': user_id} payload = {'chat_id': chat_id, 'user_id': user_id}
if can_change_info: if can_change_info is not None:
payload['can_change_info'] = can_change_info payload['can_change_info'] = can_change_info
if can_post_messages: if can_post_messages is not None:
payload['can_post_messages'] = can_post_messages payload['can_post_messages'] = can_post_messages
if can_edit_messages: if can_edit_messages is not None:
payload['can_edit_messages'] = can_edit_messages payload['can_edit_messages'] = can_edit_messages
if can_delete_messages: if can_delete_messages is not None:
payload['can_delete_messages'] = can_delete_messages payload['can_delete_messages'] = can_delete_messages
if can_invite_users: if can_invite_users is not None:
payload['can_invite_users'] = can_invite_users payload['can_invite_users'] = can_invite_users
if can_restrict_members: if can_restrict_members is not None:
payload['can_restrict_members'] = can_restrict_members payload['can_restrict_members'] = can_restrict_members
if can_pin_messages: if can_pin_messages is not None:
payload['can_pin_messages'] = can_pin_messages payload['can_pin_messages'] = can_pin_messages
if can_promote_members: if can_promote_members is not None:
payload['can_promote_members'] = can_promote_members payload['can_promote_members'] = can_promote_members
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
@ -669,9 +724,11 @@ def set_chat_description(token, chat_id, description):
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
def pin_chat_message(token, chat_id, message_id, disable_notification=False): def pin_chat_message(token, chat_id, message_id, disable_notification=None):
method_url = 'pinChatMessage' method_url = 'pinChatMessage'
payload = {'chat_id': chat_id, 'message_id': message_id, 'disable_notification': disable_notification} payload = {'chat_id': chat_id, 'message_id': message_id}
if disable_notification is not None:
payload['disable_notification'] = disable_notification
return _make_request(token, method_url, params=payload, method='post') return _make_request(token, method_url, params=payload, method='post')
@ -695,7 +752,7 @@ def edit_message_text(token, text, chat_id=None, message_id=None, inline_message
payload['inline_message_id'] = inline_message_id payload['inline_message_id'] = inline_message_id
if parse_mode: if parse_mode:
payload['parse_mode'] = parse_mode payload['parse_mode'] = parse_mode
if disable_web_page_preview: if disable_web_page_preview is not None:
payload['disable_web_page_preview'] = disable_web_page_preview payload['disable_web_page_preview'] = disable_web_page_preview
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
@ -756,15 +813,19 @@ def delete_message(token, chat_id, message_id):
# Game # Game
def send_game(token, chat_id, game_short_name, disable_notification=None, reply_to_message_id=None, reply_markup=None): def send_game(
token, chat_id, game_short_name,
disable_notification=None, reply_to_message_id=None, reply_markup=None, timeout=None):
method_url = r'sendGame' method_url = r'sendGame'
payload = {'chat_id': chat_id, 'game_short_name': game_short_name} payload = {'chat_id': chat_id, 'game_short_name': game_short_name}
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup: if reply_markup:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
@ -785,7 +846,7 @@ def set_game_score(token, user_id, score, force=None, disable_edit_message=None,
""" """
method_url = r'setGameScore' method_url = r'setGameScore'
payload = {'user_id': user_id, 'score': score} payload = {'user_id': user_id, 'score': score}
if force: if force is not None:
payload['force'] = force payload['force'] = force
if chat_id: if chat_id:
payload['chat_id'] = chat_id payload['chat_id'] = chat_id
@ -793,7 +854,7 @@ def set_game_score(token, user_id, score, force=None, disable_edit_message=None,
payload['message_id'] = message_id payload['message_id'] = message_id
if inline_message_id: if inline_message_id:
payload['inline_message_id'] = inline_message_id payload['inline_message_id'] = inline_message_id
if disable_edit_message: if disable_edit_message is not None:
payload['disable_edit_message'] = disable_edit_message payload['disable_edit_message'] = disable_edit_message
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
@ -823,10 +884,12 @@ def get_game_high_scores(token, user_id, chat_id=None, message_id=None, inline_m
# Payments (https://core.telegram.org/bots/api#payments) # Payments (https://core.telegram.org/bots/api#payments)
def send_invoice(token, chat_id, title, description, invoice_payload, provider_token, currency, prices, def send_invoice(
start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None, token, chat_id, title, description, invoice_payload, provider_token, currency, prices,
need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None, is_flexible=None, start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None,
disable_notification=None, reply_to_message_id=None, reply_markup=None, provider_data=None): need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None, is_flexible=None,
disable_notification=None, reply_to_message_id=None, reply_markup=None, provider_data=None,
timeout=None):
""" """
Use this method to send invoices. On success, the sent Message is returned. Use this method to send invoices. On success, the sent Message is returned.
:param token: Bot's token (you don't need to fill this) :param token: Bot's token (you don't need to fill this)
@ -865,17 +928,17 @@ def send_invoice(token, chat_id, title, description, invoice_payload, provider_t
payload['photo_width'] = photo_width payload['photo_width'] = photo_width
if photo_height: if photo_height:
payload['photo_height'] = photo_height payload['photo_height'] = photo_height
if need_name: if need_name is not None:
payload['need_name'] = need_name payload['need_name'] = need_name
if need_phone_number: if need_phone_number is not None:
payload['need_phone_number'] = need_phone_number payload['need_phone_number'] = need_phone_number
if need_email: if need_email is not None:
payload['need_email'] = need_email payload['need_email'] = need_email
if need_shipping_address: if need_shipping_address is not None:
payload['need_shipping_address'] = need_shipping_address payload['need_shipping_address'] = need_shipping_address
if is_flexible: if is_flexible is not None:
payload['is_flexible'] = is_flexible payload['is_flexible'] = is_flexible
if disable_notification: if disable_notification is not None:
payload['disable_notification'] = disable_notification payload['disable_notification'] = disable_notification
if reply_to_message_id: if reply_to_message_id:
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
@ -883,6 +946,8 @@ def send_invoice(token, chat_id, title, description, invoice_payload, provider_t
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if provider_data: if provider_data:
payload['provider_data'] = provider_data payload['provider_data'] = provider_data
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)
@ -940,7 +1005,7 @@ def answer_callback_query(token, callback_query_id, text=None, show_alert=None,
payload = {'callback_query_id': callback_query_id} payload = {'callback_query_id': callback_query_id}
if text: if text:
payload['text'] = text payload['text'] = text
if show_alert: if show_alert is not None:
payload['show_alert'] = show_alert payload['show_alert'] = show_alert
if url: if url:
payload['url'] = url payload['url'] = url
@ -955,7 +1020,7 @@ def answer_inline_query(token, inline_query_id, results, cache_time=None, is_per
payload = {'inline_query_id': inline_query_id, 'results': _convert_list_json_serializable(results)} payload = {'inline_query_id': inline_query_id, 'results': _convert_list_json_serializable(results)}
if cache_time is not None: if cache_time is not None:
payload['cache_time'] = cache_time payload['cache_time'] = cache_time
if is_personal: if is_personal is not None:
payload['is_personal'] = is_personal payload['is_personal'] = is_personal
if next_offset is not None: if next_offset is not None:
payload['next_offset'] = next_offset payload['next_offset'] = next_offset
@ -978,7 +1043,9 @@ def upload_sticker_file(token, user_id, png_sticker):
return _make_request(token, method_url, params=payload, files=files, method='post') return _make_request(token, method_url, params=payload, files=files, method='post')
def create_new_sticker_set(token, user_id, name, title, png_sticker, emojis, contains_masks=None, mask_position=None): def create_new_sticker_set(
token, user_id, name, title, png_sticker, emojis,
contains_masks=None, mask_position=None):
method_url = 'createNewStickerSet' method_url = 'createNewStickerSet'
payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis} payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis}
files = None files = None
@ -986,7 +1053,7 @@ def create_new_sticker_set(token, user_id, name, title, png_sticker, emojis, con
files = {'png_sticker': png_sticker} files = {'png_sticker': png_sticker}
else: else:
payload['png_sticker'] = png_sticker payload['png_sticker'] = png_sticker
if contains_masks: if contains_masks is not None:
payload['contains_masks'] = contains_masks payload['contains_masks'] = contains_masks
if mask_position: if mask_position:
payload['mask_position'] = mask_position.to_json() payload['mask_position'] = mask_position.to_json()
@ -1023,7 +1090,7 @@ def send_poll(
question, options, question, options,
is_anonymous = None, type = None, allows_multiple_answers = None, correct_option_id = None, is_anonymous = None, type = None, allows_multiple_answers = None, correct_option_id = None,
explanation = None, explanation_parse_mode=None, open_period = None, close_date = None, is_closed = None, explanation = None, explanation_parse_mode=None, open_period = None, close_date = None, is_closed = None,
disable_notifications=False, reply_to_message_id=None, reply_markup=None): disable_notifications=False, reply_to_message_id=None, reply_markup=None, timeout=None):
method_url = r'sendPoll' method_url = r'sendPoll'
payload = { payload = {
'chat_id': str(chat_id), 'chat_id': str(chat_id),
@ -1055,6 +1122,8 @@ def send_poll(
payload['reply_to_message_id'] = reply_to_message_id payload['reply_to_message_id'] = reply_to_message_id
if reply_markup is not None: if reply_markup is not None:
payload['reply_markup'] = _convert_markup(reply_markup) payload['reply_markup'] = _convert_markup(reply_markup)
if timeout:
payload['connect-timeout'] = timeout
return _make_request(token, method_url, params=payload) return _make_request(token, method_url, params=payload)