From ea57db7aad19bf90d57b8949fea69c5e66e57fa1 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 28 Jan 2021 21:05:00 +0100 Subject: [PATCH] Add comment_to parameter to more easily post comments --- telethon/client/messages.py | 27 ++++++++++++++++++++++++++- telethon/client/uploads.py | 18 +++++++++++++++--- telethon/tl/custom/message.py | 6 ++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/telethon/client/messages.py b/telethon/client/messages.py index 057b9069..ba3b8532 100644 --- a/telethon/client/messages.py +++ b/telethon/client/messages.py @@ -577,6 +577,19 @@ class MessageMethods: # region Message sending/editing/deleting + async def _get_comment_data( + self: 'TelegramClient', + entity: 'hints.EntityLike', + message: 'typing.Union[int, types.Message]' + ): + r = await self(functions.messages.GetDiscussionMessageRequest( + peer=entity, + msg_id=utils.get_message_id(message) + )) + m = r.messages[0] + chat = next(c for c in r.chats if c.id == m.peer_id.channel_id) + return utils.get_input_peer(chat), m.id + async def send_message( self: 'TelegramClient', entity: 'hints.EntityLike', @@ -591,7 +604,8 @@ class MessageMethods: clear_draft: bool = False, buttons: 'hints.MarkupLike' = None, silent: bool = None, - schedule: 'hints.DateLike' = None + schedule: 'hints.DateLike' = None, + comment_to: 'typing.Union[int, types.Message]' = None ) -> 'types.Message': """ Sends a message to the specified user, chat or channel. @@ -672,6 +686,14 @@ class MessageMethods: it will be scheduled to be automatically sent at a later time. + comment_to (`int` | `Message `, optional): + Similar to ``reply_to``, but replies in the linked group of a + broadcast channel instead (effectively leaving a "comment to" + the specified message). + + This parameter takes precedence over ``reply_to``. If there is + no linked chat, `telethon.errors.sgIdInvalidError` is raised. + Returns The sent `custom.Message `. @@ -740,6 +762,9 @@ class MessageMethods: ) entity = await self.get_input_entity(entity) + if comment_to is not None: + entity, reply_to = await self._get_comment_data(entity, comment_to) + if isinstance(message, types.Message): if buttons is None: markup = message.reply_markup diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 7f75842a..191f7727 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -114,6 +114,7 @@ class UploadMethods: silent: bool = None, supports_streaming: bool = False, schedule: 'hints.DateLike' = None, + comment_to: 'typing.Union[int, types.Message]' = None, **kwargs) -> 'types.Message': """ Sends message with the given file to the specified entity. @@ -260,6 +261,14 @@ class UploadMethods: it will be scheduled to be automatically sent at a later time. + comment_to (`int` | `Message `, optional): + Similar to ``reply_to``, but replies in the linked group of a + broadcast channel instead (effectively leaving a "comment to" + the specified message). + + This parameter takes precedence over ``reply_to``. If there is + no linked chat, `telethon.errors.sgIdInvalidError` is raised. + Returns The `Message ` (or messages) containing the sent file, or messages if a list of them was passed. @@ -317,6 +326,12 @@ class UploadMethods: if not caption: caption = '' + entity = await self.get_input_entity(entity) + if comment_to is not None: + entity, reply_to = await self._get_comment_data(entity, comment_to) + else: + reply_to = utils.get_message_id(reply_to + # First check if the user passed an iterable, in which case # we may want to send grouped. if utils.is_list_like(file): @@ -351,9 +366,6 @@ class UploadMethods: return result - entity = await self.get_input_entity(entity) - reply_to = utils.get_message_id(reply_to) - if formatting_entities is not None: msg_entities = formatting_entities else: diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index 24b04326..c6e20b76 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -235,6 +235,7 @@ class _Message(ChatGetter, SenderGetter): self._via_bot = None self._via_input_bot = None self._action_entities = None + self._linked_chat = None sender_id = None if from_id is not None: @@ -295,6 +296,11 @@ class _Message(ChatGetter, SenderGetter): self._action_entities = [entities.get(utils.get_peer_id( types.PeerChat(self.action.chat_id)))] + if self.replies and self.replies.channel_id: + self._linked_chat = entities.get(utils.get_peer_id( + types.PeerChannel(self.replies.channel_id))) + + # endregion Initialization # region Public Properties