Skip to content

Commit

Permalink
Added the class PreparedInlineMessage and the method savePreparedInl…
Browse files Browse the repository at this point in the history
…ineMessage, allowing bots to suggest users to send a specific message from a Mini App via the method shareMessage.
  • Loading branch information
coder2020official committed Nov 17, 2024
1 parent e55b097 commit bee6bab
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
36 changes: 36 additions & 0 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
15 changes: 15 additions & 0 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
28 changes: 28 additions & 0 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit bee6bab

Please sign in to comment.