Skip to content

Commit 12d25e2

Browse files
Bot API 10.1 Rich Messages Partial Commit
1 parent 5ca9250 commit 12d25e2

5 files changed

Lines changed: 785 additions & 8 deletions

File tree

telebot/__init__.py

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,7 +4453,105 @@ def send_message_draft(
44534453
"""
44544454
return apihelper.send_message_draft(
44554455
self.token, chat_id, draft_id, text, parse_mode=parse_mode, entities=entities, message_thread_id=message_thread_id)
4456+
4457+
def send_rich_message(
4458+
self, chat_id: Union[int, str],
4459+
rich_message: types.InputRichMessage,
4460+
business_connection_id: Optional[str]=None,
4461+
message_thread_id: Optional[int]=None,
4462+
direct_messages_topic_id: Optional[int]=None,
4463+
disable_notification: Optional[bool]=None,
4464+
protect_content: Optional[bool]=None,
4465+
allow_paid_broadcast: Optional[bool]=None,
4466+
message_effect_id: Optional[str]=None,
4467+
suggested_post_parameters: Optional[types.SuggestedPostParameters]=None,
4468+
reply_parameters: Optional[types.ReplyParameters]=None,
4469+
reply_markup: Optional[REPLY_MARKUP_TYPES]=None) -> types.Message:
4470+
"""
4471+
Use this method to send rich messages. If the message contains a block with a media element,
4472+
then the bot must have the right to send the media to the chat. On success, the sent Message is returned.
4473+
4474+
Telegram documentation: https://core.telegram.org/bots/api#sendrichmessage
4475+
4476+
:param chat_id: Unique identifier for the target chat or username of the target bot, supergroup or channel in the format @username
4477+
:type chat_id: :obj:`int` or :obj:`str`
4478+
4479+
:param rich_message: The message to be sent
4480+
:type rich_message: :class:`telebot.types.InputRichMessage`
4481+
4482+
:param disable_notification: Sends the message silently. Users will receive a notification with no sound.
4483+
:type disable_notification: :obj:`bool`
4484+
4485+
:param protect_content: Protects the contents of the sent message from forwarding and saving
4486+
:type protect_content: :obj:`bool`
44564487

4488+
:param allow_paid_broadcast: Pass True to allow up to 1000 messages per second, ignoring broadcasting limits for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance.
4489+
:type allow_paid_broadcast: :obj:`bool`
4490+
4491+
:param message_effect_id: Unique identifier of the message effect to be added to the message; for private chats only
4492+
:type message_effect_id: :obj:`str`
4493+
4494+
:param suggested_post_parameters: A JSON-serialized object containing the parameters of the suggested post to send;
4495+
for direct messages chats only. If the message is sent as a reply to another suggested post, then that suggested post is automatically declined.
4496+
:type suggested_post_parameters: :class:`telebot.types.SuggestedPostParameters`
4497+
4498+
:param reply_parameters: Description of the message to reply to
4499+
:type reply_parameters: :class:`telebot.types.ReplyParameters`
4500+
4501+
:param reply_markup: Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove a reply keyboard or to force a reply from the user.
4502+
:type reply_markup: :class:`telebot.types.InlineKeyboardMarkup` or :class:`telebot.types.ReplyKeyboardMarkup` or :class:`telebot.types.ReplyKeyboardRemove` or :class:`telebot.types.ForceReply`
4503+
4504+
:param business_connection_id: Identifier of a business connection
4505+
:type business_connection_id: :obj:`str`
4506+
4507+
:param message_thread_id: The thread identifier of a message from which the reply will be sent
4508+
:type message_thread_id: :obj:`int`
4509+
4510+
:param direct_messages_topic_id: Identifier of the direct messages topic to which the message will be sent; required if the message is sent to a direct messages chat
4511+
:type direct_messages_topic_id: :obj:`int`
4512+
4513+
:return: On success, the sent Message is returned.
4514+
:rtype: :class:`telebot.types.Message`
4515+
"""
4516+
disable_notification = self.disable_notification if (disable_notification is None) else disable_notification
4517+
protect_content = self.protect_content if (protect_content is None) else protect_content
4518+
4519+
return types.Message.de_json(
4520+
apihelper.send_rich_message(
4521+
self.token, chat_id, rich_message, disable_notification=disable_notification, protect_content=protect_content,
4522+
allow_paid_broadcast=allow_paid_broadcast, message_effect_id=message_effect_id, suggested_post_parameters=suggested_post_parameters, reply_parameters=reply_parameters,
4523+
reply_markup=reply_markup, business_connection_id=business_connection_id, message_thread_id=message_thread_id, direct_messages_topic_id=direct_messages_topic_id)
4524+
)
4525+
4526+
def send_rich_message_draft(
4527+
self, chat_id: int,
4528+
draft_id: int,
4529+
rich_message: types.InputRichMessage,
4530+
message_thread_id: Optional[int]=None) -> bool:
4531+
"""
4532+
Use this method to stream a partial rich message to a user while the message is being generated
4533+
Note that the streamed draft is ephemeral and acts as a temporary 30-second preview - once the output
4534+
is finalized, you must call sendRichMessage with the complete message to persist it in the user's chat. Returns True on success.
4535+
4536+
Telegram documentation: https://core.telegram.org/bots/api#sendrichmessagedraft
4537+
4538+
:param chat_id: Unique identifier for the target private chat
4539+
:type chat_id: :obj:`int`
4540+
4541+
:param draft_id: Unique identifier of the message draft; must be non-zero. Changes to drafts with the same identifier are animated.
4542+
:type draft_id: :obj:`int`
4543+
4544+
:param rich_message: The partial message to be streamed
4545+
:type rich_message: :class:`telebot.types.InputRichMessage`
4546+
4547+
:param message_thread_id: Unique identifier for the target message thread
4548+
:type message_thread_id: :obj:`int`
4549+
4550+
:return: Returns True on success.
4551+
:rtype: :obj:`bool`
4552+
"""
4553+
return apihelper.send_rich_message_draft(
4554+
self.token, chat_id, draft_id, rich_message, message_thread_id=message_thread_id)
44574555

44584556

44594557
def send_chat_action(
@@ -5114,6 +5212,42 @@ def decline_chat_join_request(self, chat_id: Union[str, int], user_id: Union[int
51145212
"""
51155213
return apihelper.decline_chat_join_request(self.token, chat_id, user_id)
51165214

5215+
def answer_chat_join_request_query(self, chat_join_request_query_id: str, result: str) -> bool:
5216+
"""
5217+
Use this method to process a received chat join request query. Returns True on success.
5218+
5219+
Telegram documentation: https://core.telegram.org/bots/api#answerchatjoinrequestquery
5220+
5221+
:param chat_join_request_query_id: Unique identifier of the join request query
5222+
:type chat_join_request_query_id: :obj:`str`
5223+
5224+
:param result: Result of the query. Must be either “approve” to allow the user to join the chat,
5225+
“decline” to disallow the user to join the chat, or “queue” to leave the decision to other administrators.
5226+
:type result: :obj:`str`
5227+
5228+
:return: True on success.
5229+
:rtype: :obj:`bool`
5230+
"""
5231+
return apihelper.answer_chat_join_request_query(self.token, chat_join_request_query_id, result)
5232+
5233+
def send_chat_join_request_web_app(self, chat_join_request_query_id: str, web_app_url: str) -> bool:
5234+
"""
5235+
Use this method to process a received chat join request query by showing a Mini App to the
5236+
user before deciding the outcome. Call answerChatJoinRequestQuery to resolve the join request query based on the user interaction with the Mini App.
5237+
Returns True on success.
5238+
5239+
Telegram documentation: https://core.telegram.org/bots/api#sendchatjoinrequestwebapp
5240+
5241+
:param chat_join_request_query_id: Unique identifier of the join request query
5242+
:type chat_join_request_query_id: :obj:`str`
5243+
5244+
:param web_app_url: The URL of the Mini App to be opened
5245+
:type web_app_url: :obj:`str`
5246+
5247+
:return: True on success.
5248+
:rtype: :obj:`bool`
5249+
"""
5250+
return apihelper.send_chat_join_request_web_app(self.token, chat_join_request_query_id, web_app_url)
51175251

51185252
def set_chat_photo(self, chat_id: Union[int, str], photo: Any) -> bool:
51195253
"""
@@ -5659,7 +5793,8 @@ def edit_message_text(
56595793
reply_markup: Optional[types.InlineKeyboardMarkup]=None,
56605794
link_preview_options : Optional[types.LinkPreviewOptions]=None,
56615795
business_connection_id: Optional[str]=None,
5662-
timeout: Optional[int]=None) -> Union[types.Message, bool]:
5796+
timeout: Optional[int]=None,
5797+
rich_message: Optional[types.InputRichMessage]=None) -> Union[types.Message, bool]:
56635798
"""
56645799
Use this method to edit text and game messages.
56655800

@@ -5698,6 +5833,9 @@ def edit_message_text(
56985833
:param timeout: Timeout in seconds for the request.
56995834
:type timeout: :obj:`int`
57005835

5836+
:param rich_message: New rich content of the message; required if text isn't specified
5837+
:type rich_message: :obj:`types.InputRichMessage`
5838+
57015839
:return: On success, if edited message is sent by the bot, the edited Message is returned, otherwise True is returned.
57025840
:rtype: :obj:`types.Message` or :obj:`bool`
57035841
"""
@@ -5725,7 +5863,7 @@ def edit_message_text(
57255863
result = apihelper.edit_message_text(
57265864
self.token, text, chat_id=chat_id, message_id=message_id, inline_message_id=inline_message_id,
57275865
parse_mode=parse_mode, entities=entities, reply_markup=reply_markup, link_preview_options=link_preview_options,
5728-
business_connection_id=business_connection_id, timeout=timeout)
5866+
business_connection_id=business_connection_id, timeout=timeout, rich_message=rich_message)
57295867

57305868
if isinstance(result, bool): # if edit inline message return is bool not Message.
57315869
return result

telebot/apihelper.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,41 @@ def send_message(
282282
return _make_request(token, method_url, params=payload, method='post')
283283

284284

285+
def send_rich_message(
286+
token, chat_id, rich_message,
287+
disable_notification=None, protect_content=None, message_effect_id=None,
288+
reply_parameters=None, reply_markup=None, business_connection_id=None,
289+
direct_messages_topic_id=None, suggested_post_parameters=None, allow_paid_broadcast=None):
290+
method_url = r'sendRichMessage'
291+
payload = {'chat_id': str(chat_id), 'rich_message': rich_message.to_json()}
292+
if disable_notification is not None:
293+
payload['disable_notification'] = disable_notification
294+
if protect_content is not None:
295+
payload['protect_content'] = protect_content
296+
if message_effect_id:
297+
payload['message_effect_id'] = message_effect_id
298+
if reply_parameters:
299+
payload['reply_parameters'] = reply_parameters.to_json()
300+
if reply_markup:
301+
payload['reply_markup'] = _convert_markup(reply_markup)
302+
if business_connection_id:
303+
payload['business_connection_id'] = business_connection_id
304+
if direct_messages_topic_id is not None:
305+
payload['direct_messages_topic_id'] = direct_messages_topic_id
306+
if suggested_post_parameters is not None:
307+
payload['suggested_post_parameters'] = suggested_post_parameters.to_json()
308+
if allow_paid_broadcast is not None:
309+
payload['allow_paid_broadcast'] = allow_paid_broadcast
310+
return _make_request(token, method_url, params=payload, method='post')
311+
312+
def send_rich_message_draft(token, chat_id, draft_id, rich_message, message_thread_id=None):
313+
method_url = r'sendRichMessageDraft'
314+
payload = {'chat_id': str(chat_id), 'draft_id': draft_id, 'rich_message': rich_message.to_json()}
315+
if message_thread_id is not None:
316+
payload['message_thread_id'] = message_thread_id
317+
return _make_request(token, method_url, params=payload, method='post')
318+
319+
285320
def set_webhook(token, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None,
286321
drop_pending_updates = None, timeout=None, secret_token=None):
287322
method_url = r'setWebhook'
@@ -1516,6 +1551,24 @@ def decline_chat_join_request(token, chat_id, user_id):
15161551
return _make_request(token, method_url, params=payload, method='post')
15171552

15181553

1554+
def answer_chat_join_request_query(token, chat_join_request_query_id, result):
1555+
method_url = 'answerChatJoinRequestQuery'
1556+
payload = {
1557+
'chat_join_request_query_id': chat_join_request_query_id,
1558+
'result': result
1559+
}
1560+
return _make_request(token, method_url, params=payload, method='post')
1561+
1562+
1563+
def send_chat_join_request_web_app(token, chat_join_request_query_id, web_app_url):
1564+
method_url = 'sendChatJoinRequestWebApp'
1565+
payload = {
1566+
'chat_join_request_query_id': chat_join_request_query_id,
1567+
'web_app_url': web_app_url
1568+
}
1569+
return _make_request(token, method_url, params=payload, method='post')
1570+
1571+
15191572
def set_chat_photo(token, chat_id, photo):
15201573
method_url = 'setChatPhoto'
15211574
payload = {'chat_id': chat_id}
@@ -1737,7 +1790,7 @@ def unpin_all_chat_messages(token, chat_id):
17371790

17381791
def edit_message_text(
17391792
token, text, chat_id=None, message_id=None, inline_message_id=None, parse_mode=None, entities = None,
1740-
reply_markup=None, link_preview_options=None, business_connection_id=None, timeout=None):
1793+
reply_markup=None, link_preview_options=None, business_connection_id=None, timeout=None, rich_message=None):
17411794
method_url = r'editMessageText'
17421795
payload = {'text': text}
17431796
if chat_id:
@@ -1758,6 +1811,8 @@ def edit_message_text(
17581811
payload['business_connection_id'] = business_connection_id
17591812
if timeout:
17601813
payload['timeout'] = timeout
1814+
if rich_message:
1815+
payload['rich_message'] = rich_message.to_json()
17611816
return _make_request(token, method_url, params=payload, method='post')
17621817

17631818

0 commit comments

Comments
 (0)