Add comment_to parameter to more easily post comments

This commit is contained in:
Lonami Exo 2021-01-28 21:05:00 +01:00
parent 6f7640af18
commit ea57db7aad
3 changed files with 47 additions and 4 deletions

View File

@ -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 <telethon.tl.custom.message.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 <telethon.tl.custom.message.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

View File

@ -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 <telethon.tl.custom.message.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 <telethon.tl.custom.message.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:

View File

@ -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