From 167e602907f72e7b50f24e10647334194b8d6627 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sat, 22 Jun 2024 13:35:03 +0300 Subject: [PATCH] Add "timeout" parameter to edit_xxx functions --- telebot/__init__.py | 33 +++++++++++++++++++------ telebot/apihelper.py | 27 ++++++++++++++++----- telebot/async_telebot.py | 51 ++++++++++++++++++++++++++++++--------- telebot/asyncio_helper.py | 27 ++++++++++++++++----- 4 files changed, 106 insertions(+), 32 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 07cda06ae..4100e26a0 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -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. @@ -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` """ @@ -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 @@ -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. @@ -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 @@ -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. @@ -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 @@ -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. @@ -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` """ @@ -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 diff --git a/telebot/apihelper.py b/telebot/apihelper.py index d6a372cba..54ce9f691 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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: @@ -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: @@ -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} @@ -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: @@ -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') @@ -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' diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index af677e5ef..2a338722a 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -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) @@ -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. @@ -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` """ @@ -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) @@ -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. @@ -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) @@ -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. @@ -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) @@ -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. @@ -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) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index ef755630f..0a484f3b4 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1360,8 +1360,9 @@ async def unpin_all_chat_messages(token, chat_id): # Updating messages -async 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): +async 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: @@ -1380,11 +1381,14 @@ async def edit_message_text(token, text, chat_id=None, message_id=None, inline_m 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 await _process_request(token, method_url, params=payload, method='post') -async 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): +async 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: @@ -1403,10 +1407,14 @@ async def edit_message_caption(token, caption, chat_id=None, message_id=None, in 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 await _process_request(token, method_url, params=payload, method='post') -async def edit_message_media(token, media, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, business_connection_id=None): +async 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 = await convert_input_media(media) payload = {'media': media_json} @@ -1420,10 +1428,14 @@ async def edit_message_media(token, media, chat_id=None, message_id=None, inline payload['reply_markup'] = await _convert_markup(reply_markup) if business_connection_id: payload['business_connection_id'] = business_connection_id + if timeout: + payload['timeout'] = timeout return await _process_request(token, method_url, params=payload, files=file, method='post' if file else 'get') -async def edit_message_reply_markup(token, chat_id=None, message_id=None, inline_message_id=None, reply_markup=None, business_connection_id=None): +async 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: @@ -1436,6 +1448,8 @@ async def edit_message_reply_markup(token, chat_id=None, message_id=None, inline payload['reply_markup'] = await _convert_markup(reply_markup) if business_connection_id: payload['business_connection_id'] = business_connection_id + if timeout: + payload['timeout'] = timeout return await _process_request(token, method_url, params=payload, method='post') @@ -1569,6 +1583,7 @@ async 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'