From bee6bab8213e1d22731146b9e92245fd1a42cdb5 Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 17 Nov 2024 18:10:08 +0400 Subject: [PATCH] Added the class PreparedInlineMessage and the method savePreparedInlineMessage, allowing bots to suggest users to send a specific message from a Mini App via the method shareMessage. --- telebot/__init__.py | 36 ++++++++++++++++++++++++++++++++++++ telebot/apihelper.py | 15 +++++++++++++++ telebot/async_telebot.py | 30 ++++++++++++++++++++++++++++++ telebot/asyncio_helper.py | 14 ++++++++++++++ telebot/types.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+) diff --git a/telebot/__init__.py b/telebot/__init__.py index 494c0ec5d..586f44c9a 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -6769,6 +6769,42 @@ def answer_web_app_query(self, web_app_query_id: str, result: types.InlineQueryR """ return apihelper.answer_web_app_query(self.token, web_app_query_id, result) + def save_prepared_inline_message( + self, user_id: int, result: types.InlineQueryResultBase, allow_user_chats: Optional[bool]=None, + allow_bot_chats: Optional[bool]=None, allow_group_chats: Optional[bool]=None, + allow_channel_chats: Optional[bool]=None) -> types.PreparedInlineMessage: + """ + Use this method to store a message that can be sent by a user of a Mini App. + Returns a PreparedInlineMessage object. + + Telegram Documentation: https://core.telegram.org/bots/api#savepreparedinlinemessage + + :param user_id: Unique identifier of the target user that can use the prepared message + :type user_id: :obj:`int` + + :param result: A JSON-serialized object describing the message to be sent + :type result: :class:`telebot.types.InlineQueryResultBase` + + :param allow_user_chats: Pass True if the message can be sent to private chats with users + :type allow_user_chats: :obj:`bool` + + :param allow_bot_chats: Pass True if the message can be sent to private chats with bots + :type allow_bot_chats: :obj:`bool` + + :param allow_group_chats: Pass True if the message can be sent to group and supergroup chats + :type allow_group_chats: :obj:`bool` + + :param allow_channel_chats: Pass True if the message can be sent to channel chats + :type allow_channel_chats: :obj:`bool` + + :return: On success, a PreparedInlineMessage object is returned. + :rtype: :class:`telebot.types.PreparedInlineMessage` + """ + return types.PreparedInlineMessage.de_json( + apihelper.save_prepared_inline_message( + self.token, user_id, result, allow_user_chats=allow_user_chats, allow_bot_chats=allow_bot_chats, + allow_group_chats=allow_group_chats, allow_channel_chats=allow_channel_chats) + ) def register_for_reply(self, message: types.Message, callback: Callable, *args, **kwargs) -> None: """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 68623922f..01a1a77ba 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1981,6 +1981,21 @@ def answer_web_app_query(token, web_app_query_id, result: types.InlineQueryResul return _make_request(token, method_url, params=payload, method='post') +def save_prepared_inline_message(token, user_id, result: types.InlineQueryResultBase, allow_user_chats=None, + allow_bot_chats=None, allow_group_chats=None, allow_channel_chats=None): + method_url = 'savePreparedInlineMessage' + payload = {'user_id': user_id, 'result': result.to_json()} + if allow_user_chats is not None: + payload['allow_user_chats'] = allow_user_chats + if allow_bot_chats is not None: + payload['allow_bot_chats'] = allow_bot_chats + if allow_group_chats is not None: + payload['allow_group_chats'] = allow_group_chats + if allow_channel_chats is not None: + payload['allow_channel_chats'] = allow_channel_chats + return _make_request(token, method_url, params=payload, method='post') + + def create_invoice_link(token, title, description, payload, provider_token, currency, prices, max_tip_amount=None, suggested_tip_amounts=None, provider_data=None, photo_url=None, photo_size=None, photo_width=None, photo_height=None, need_name=None, need_phone_number=None, diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 6ab489756..659596f59 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -3148,6 +3148,36 @@ async def answer_web_app_query(self, web_app_query_id: str, result: types.Inline return await asyncio_helper.answer_web_app_query(self.token, web_app_query_id, result) + async def save_prepared_inline_message(self, user_id: int, result: types.InlineQueryResultBase, allow_user_chats: Optional[bool]=None, + allow_bot_chats: Optional[bool]=None, allow_group_chats: Optional[bool]=None, allow_channel_chats: Optional[bool]=None) -> types.PreparedInlineMessage: + """ + Stores a message that can be sent by a user of a Mini App. Returns a PreparedInlineMessage object. + + Telegram Documentation: https://core.telegram.org/bots/api#savepreparedinlinemessage + + :param user_id: Unique identifier of the target user that can use the prepared message + :type user_id: :obj:`int` + + :param result: A JSON-serialized object describing the message to be sent + :type result: :class:`telebot.types.InlineQueryResultBase` + + :param allow_user_chats: Pass True if the message can be sent to private chats with users + :type allow_user_chats: :obj:`bool`, optional + + :param allow_bot_chats: Pass True if the message can be sent to private chats with bots + :type allow_bot_chats: :obj:`bool`, optional + + :param allow_group_chats: Pass True if the message can be sent to group and supergroup chats + :type allow_group_chats: :obj:`bool`, optional + + :param allow_channel_chats: Pass True if the message can be sent to channel chats + :type allow_channel_chats: :obj:`bool`, optional + + :return: :class:`telebot.types.PreparedInlineMessage` + """ + result = await asyncio_helper.save_prepared_inline_message(self.token, user_id, result, allow_user_chats, allow_bot_chats, allow_group_chats, allow_channel_chats) + return types.PreparedInlineMessage.de_json(result) + async def get_chat_member(self, chat_id: Union[int, str], user_id: int) -> types.ChatMember: """ Use this method to get information about a member of a chat. Returns a ChatMember object on success. diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 41d94d0bd..4523e7173 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -406,6 +406,20 @@ async def answer_web_app_query(token, web_app_query_id, result: types.InlineQuer return await _process_request(token, method_url, params=payload, method='post') +async def save_prepared_inline_message(token, user_id, result: types.InlineQueryResultBase, allow_user_chats=None, allow_bot_chats=None, allow_group_chats=None, allow_channel_chats=None): + method_url = r'savePreparedInlineMessage' + payload = {'user_id': user_id, 'result': result.to_json()} + if allow_user_chats is not None: + payload['allow_user_chats'] = allow_user_chats + if allow_bot_chats is not None: + payload['allow_bot_chats'] = allow_bot_chats + if allow_group_chats is not None: + payload['allow_group_chats'] = allow_group_chats + if allow_channel_chats is not None: + payload['allow_channel_chats'] = allow_channel_chats + return await _process_request(token, method_url, params=payload) + + async def get_chat_member(token, chat_id, user_id): method_url = r'getChatMember' payload = {'chat_id': chat_id, 'user_id': user_id} diff --git a/telebot/types.py b/telebot/types.py index 76cf20b61..904379dbc 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -10997,3 +10997,31 @@ def de_json(cls, json_string): if json_string is None: return None obj = cls.check_json(json_string) return cls(**obj) + + +class PreparedInlineMessage(JsonDeserializable): + """ + Describes an inline message to be sent by a user of a Mini App. + + Telegram documentation: https://core.telegram.org/bots/api#preparedinlinemessage + + :param id: Unique identifier of the prepared message + :type id: :obj:`str` + + :param expiration_date: Expiration date of the prepared message, in Unix time. Expired prepared messages can no longer be used + :type expiration_date: :obj:`int` + + :return: Instance of the class + :rtype: :class:`PreparedInlineMessage` + """ + + def __init__(self, id, expiration_date, **kwargs): + self.id: str = id + self.expiration_date: int = expiration_date + + @classmethod + def de_json(cls, json_string): + if json_string is None: return None + obj = cls.check_json(json_string) + return cls(**obj) +