Skip to content

Commit

Permalink
Merge pull request #2315 from Badiboy/master
Browse files Browse the repository at this point in the history
Add "timeout" parameter to edit_xxx functions
  • Loading branch information
coder2020official authored Jun 22, 2024
2 parents b84baa9 + 167e602 commit 1f287fa
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 32 deletions.
33 changes: 25 additions & 8 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4654,7 +4654,8 @@ def edit_message_text(
disable_web_page_preview: Optional[bool]=None, # deprecated, for backward compatibility
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
link_preview_options : Optional[types.LinkPreviewOptions]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit text and game messages.
Expand Down Expand Up @@ -4690,6 +4691,9 @@ def edit_message_text(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
Expand Down Expand Up @@ -4717,7 +4721,7 @@ def edit_message_text(
result = apihelper.edit_message_text(
self.token, text, chat_id=chat_id, message_id=message_id, inline_message_id=inline_message_id,
parse_mode=parse_mode, entities=entities, reply_markup=reply_markup, link_preview_options=link_preview_options,
business_connection_id=business_connection_id)
business_connection_id=business_connection_id, timeout=timeout)

if type(result) == bool: # if edit inline message return is bool not Message.
return result
Expand All @@ -4729,7 +4733,8 @@ def edit_message_media(
message_id: Optional[int]=None,
inline_message_id: Optional[str]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit animation, audio, document, photo, or video messages.
If a message is a part of a message album, then it can be edited only to a photo or a video.
Expand All @@ -4755,12 +4760,15 @@ def edit_message_media(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
result = apihelper.edit_message_media(
self.token, media, chat_id=chat_id, message_id=message_id, inline_message_id=inline_message_id,
reply_markup=reply_markup, business_connection_id=business_connection_id)
reply_markup=reply_markup, business_connection_id=business_connection_id, timeout=timeout)

if type(result) == bool: # if edit inline message return is bool not Message.
return result
Expand All @@ -4772,7 +4780,8 @@ def edit_message_reply_markup(
message_id: Optional[int]=None,
inline_message_id: Optional[str]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit only the reply markup of messages.
Expand All @@ -4793,12 +4802,15 @@ def edit_message_reply_markup(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
result = apihelper.edit_message_reply_markup(
self.token, chat_id=chat_id, message_id=message_id, inline_message_id=inline_message_id,
reply_markup=reply_markup, business_connection_id=business_connection_id)
reply_markup=reply_markup, business_connection_id=business_connection_id, timeout=timeout)

if type(result) == bool:
return result
Expand Down Expand Up @@ -5550,7 +5562,8 @@ def edit_message_caption(
caption_entities: Optional[List[types.MessageEntity]]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
show_caption_above_media: Optional[bool]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit captions of messages.
Expand Down Expand Up @@ -5583,6 +5596,9 @@ def edit_message_caption(
:param business_connection_id: Identifier of the business connection to use for the message
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` | :obj:`bool`
"""
Expand All @@ -5591,7 +5607,8 @@ def edit_message_caption(
result = apihelper.edit_message_caption(
self.token, caption, chat_id=chat_id, message_id=message_id, inline_message_id=inline_message_id,
parse_mode=parse_mode, caption_entities=caption_entities, reply_markup=reply_markup,
show_caption_above_media=show_caption_above_media, business_connection_id=business_connection_id)
show_caption_above_media=show_caption_above_media, business_connection_id=business_connection_id,
timeout=timeout)

if type(result) == bool:
return result
Expand Down
27 changes: 21 additions & 6 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,8 +1382,9 @@ def unpin_all_chat_messages(token, chat_id):

# Updating messages

def edit_message_text(token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None,
entities = None, reply_markup=None, link_preview_options=None, business_connection_id=None):
def edit_message_text(
token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None, entities = None,
reply_markup=None, link_preview_options=None, business_connection_id=None, timeout=None):
method_url = r'editMessageText'
payload = {'text': text}
if chat_id:
Expand All @@ -1402,11 +1403,14 @@ def edit_message_text(token, text, chat_id=None, message_id=None, inline_message
payload['link_preview_options'] = link_preview_options.to_json()
if business_connection_id:
payload['business_connection_id'] = business_connection_id
if timeout:
payload['timeout'] = timeout
return _make_request(token, method_url, params=payload, method='post')


def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_message_id=None,
parse_mode=None, caption_entities=None,reply_markup=None, show_caption_above_media=None, business_connection_id=None):
def edit_message_caption(
token, caption, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None, caption_entities=None,
reply_markup=None, show_caption_above_media=None, business_connection_id=None, timeout=None):
method_url = r'editMessageCaption'
payload = {'caption': caption}
if chat_id:
Expand All @@ -1425,10 +1429,14 @@ def edit_message_caption(token, caption, chat_id=None, message_id=None, inline_m
payload['show_caption_above_media'] = show_caption_above_media
if business_connection_id:
payload['business_connection_id'] = business_connection_id
if timeout:
payload['timeout'] = timeout
return _make_request(token, method_url, params=payload, method='post')


def edit_message_media(token, media, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, business_connection_id=None):
def edit_message_media(
token, media, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None,
business_connection_id=None, timeout=None):
method_url = r'editMessageMedia'
media_json, file = convert_input_media(media)
payload = {'media': media_json}
Expand All @@ -1442,10 +1450,14 @@ def edit_message_media(token, media, chat_id=None, message_id=None, inline_messa
payload['reply_markup'] = _convert_markup(reply_markup)
if business_connection_id:
payload['business_connection_id'] = business_connection_id
if timeout:
payload['timeout'] = timeout
return _make_request(token, method_url, params=payload, files=file, method='post' if file else 'get')


def edit_message_reply_markup(token, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, business_connection_id=None):
def edit_message_reply_markup(
token, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, business_connection_id=None,
timeout=None):
method_url = r'editMessageReplyMarkup'
payload = {}
if chat_id:
Expand All @@ -1458,6 +1470,8 @@ def edit_message_reply_markup(token, chat_id=None, message_id=None, inline_messa
payload['reply_markup'] = _convert_markup(reply_markup)
if business_connection_id:
payload['business_connection_id'] = business_connection_id
if timeout:
payload['timeout'] = timeout
return _make_request(token, method_url, params=payload, method='post')


Expand Down Expand Up @@ -1590,6 +1604,7 @@ def send_invoice(
:param protect_content: Protects the contents of the sent message from forwarding and saving
:param message_thread_id: Unique identifier for the target message thread (topic) of the forum; for forum supergroups only
:param reply_parameters: A JSON-serialized object for an inline keyboard. If empty, one 'Pay total price' button will be shown. If not empty, the first button must be a Pay button.
:param message_effect_id: Unique identifier of the message effect to be added to the message; for private chats only
:return:
"""
method_url = r'sendInvoice'
Expand Down
51 changes: 39 additions & 12 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,11 +2362,17 @@ def register_business_message_handler(self,
:param callback: function to be called
:type callback: :obj:`function`
:param commands: list of commands
:type commands: :obj:`list` of :obj:`str`
:param regexp: Regular expression
:type regexp: :obj:`str`
:param func: Function executed as a filter
:type func: :obj:`function`
:param pass_bot: True if you need to pass TeleBot instance to handler(useful for separating handlers into different files)
:type pass_bot: :obj:`bool`
:param content_types: Supported message content types. Must be a list. Defaults to ['text'].
:type content_types: :obj:`list` of :obj:`str`
:param kwargs: Optional keyword arguments(custom filters)
Expand Down Expand Up @@ -6009,7 +6015,8 @@ async def edit_message_text(
disable_web_page_preview: Optional[bool]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
link_preview_options: Optional[types.LinkPreviewOptions]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit text and game messages.
Expand Down Expand Up @@ -6045,6 +6052,9 @@ async def edit_message_text(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
Expand All @@ -6069,8 +6079,9 @@ async def edit_message_text(
# create a LinkPreviewOptions object
link_preview_options = types.LinkPreviewOptions(is_disabled=self.disable_web_page_preview)

result = await asyncio_helper.edit_message_text(self.token, text, chat_id, message_id, inline_message_id, parse_mode,
entities, reply_markup, link_preview_options, business_connection_id)
result = await asyncio_helper.edit_message_text(
self.token, text, chat_id, message_id, inline_message_id, parse_mode, entities, reply_markup,
link_preview_options, business_connection_id, timeout)
if type(result) == bool: # if edit inline message return is bool not Message.
return result
return types.Message.de_json(result)
Expand All @@ -6080,7 +6091,8 @@ async def edit_message_media(
message_id: Optional[int]=None,
inline_message_id: Optional[str]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit animation, audio, document, photo, or video messages.
If a message is a part of a message album, then it can be edited only to a photo or a video.
Expand All @@ -6106,10 +6118,14 @@ async def edit_message_media(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
result = await asyncio_helper.edit_message_media(self.token, media, chat_id, message_id, inline_message_id, reply_markup, business_connection_id)
result = await asyncio_helper.edit_message_media(
self.token, media, chat_id, message_id, inline_message_id, reply_markup, business_connection_id, timeout)
if type(result) == bool: # if edit inline message return is bool not Message.
return result
return types.Message.de_json(result)
Expand All @@ -6119,7 +6135,8 @@ async def edit_message_reply_markup(
message_id: Optional[int]=None,
inline_message_id: Optional[str]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit only the reply markup of messages.
Expand All @@ -6140,10 +6157,14 @@ async def edit_message_reply_markup(
:param business_connection_id: Unique identifier of the business connection
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` or :obj:`bool`
"""
result = await asyncio_helper.edit_message_reply_markup(self.token, chat_id, message_id, inline_message_id, reply_markup, business_connection_id)
result = await asyncio_helper.edit_message_reply_markup(
self.token, chat_id, message_id, inline_message_id, reply_markup, business_connection_id, timeout)
if type(result) == bool:
return result
return types.Message.de_json(result)
Expand Down Expand Up @@ -6870,7 +6891,8 @@ async def edit_message_caption(
caption_entities: Optional[List[types.MessageEntity]]=None,
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
show_caption_above_media: Optional[bool]=None,
business_connection_id: Optional[str]=None) -> Union[types.Message, bool]:
business_connection_id: Optional[str]=None,
timeout: Optional[int]=None) -> Union[types.Message, bool]:
"""
Use this method to edit captions of messages.
Expand Down Expand Up @@ -6903,13 +6925,18 @@ async def edit_message_caption(
:param business_connection_id: Identifier of the business connection to send the message through
:type business_connection_id: :obj:`str`
:param timeout: Timeout in seconds for the request.
:type timeout: :obj:`int`
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
:rtype: :obj:`types.Message` | :obj:`bool`
"""
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode

result = await asyncio_helper.edit_message_caption(self.token, caption, chat_id, message_id, inline_message_id,
parse_mode, caption_entities, reply_markup, show_caption_above_media=show_caption_above_media, business_connection_id=business_connection_id)
result = await asyncio_helper.edit_message_caption(
self.token, caption, chat_id, message_id, inline_message_id, parse_mode, caption_entities, reply_markup,
show_caption_above_media=show_caption_above_media, business_connection_id=business_connection_id,
timeout=timeout)
if type(result) == bool:
return result
return types.Message.de_json(result)
Expand Down
Loading

0 comments on commit 1f287fa

Please sign in to comment.