Update iter_messages to support fetching channel comments

Closes #1598.
This commit is contained in:
Lonami Exo 2020-10-16 10:39:02 +02:00
parent 4e1f582b17
commit 5952a40c6d

View File

@ -19,7 +19,7 @@ class _MessagesIter(RequestIter):
""" """
async def _init( async def _init(
self, entity, offset_id, min_id, max_id, self, entity, offset_id, min_id, max_id,
from_user, offset_date, add_offset, filter, search from_user, offset_date, add_offset, filter, search, reply_to
): ):
# Note that entity being `None` will perform a global search. # Note that entity being `None` will perform a global search.
if entity: if entity:
@ -87,6 +87,18 @@ class _MessagesIter(RequestIter):
offset_id=offset_id, offset_id=offset_id,
limit=1 limit=1
) )
elif reply_to is not None:
self.request = functions.messages.GetRepliesRequest(
peer=self.entity,
msg_id=reply_to,
offset_id=offset_id,
offset_date=offset_date,
add_offset=add_offset,
limit=1,
max_id=0,
min_id=0,
hash=0
)
elif search is not None or filter or from_user: elif search is not None or filter or from_user:
# Telegram completely ignores `from_id` in private chats # Telegram completely ignores `from_id` in private chats
ty = helpers._entity_type(self.entity) ty = helpers._entity_type(self.entity)
@ -236,7 +248,7 @@ class _MessagesIter(RequestIter):
# (only for the first request), it's safe to just clear it off. # (only for the first request), it's safe to just clear it off.
self.request.max_date = None self.request.max_date = None
else: else:
# getHistory and searchGlobal call it offset_date # getHistory, searchGlobal and getReplies call it offset_date
self.request.offset_date = last_message.date self.request.offset_date = last_message.date
if isinstance(self.request, functions.messages.SearchGlobalRequest): if isinstance(self.request, functions.messages.SearchGlobalRequest):
@ -325,7 +337,8 @@ class MessageMethods:
from_user: 'hints.EntityLike' = None, from_user: 'hints.EntityLike' = None,
wait_time: float = None, wait_time: float = None,
ids: 'typing.Union[int, typing.Sequence[int]]' = None, ids: 'typing.Union[int, typing.Sequence[int]]' = None,
reverse: bool = False reverse: bool = False,
reply_to: int = None
) -> 'typing.Union[_MessagesIter, _IDsIter]': ) -> 'typing.Union[_MessagesIter, _IDsIter]':
""" """
Iterator over the messages for the given chat. Iterator over the messages for the given chat.
@ -433,6 +446,26 @@ class MessageMethods:
You cannot use this if both `entity` and `ids` are `None`. You cannot use this if both `entity` and `ids` are `None`.
reply_to (`int`, optional):
If set to a message ID, the messages that reply to this ID
will be returned. This feature is also known as comments in
posts of broadcast channels, or viewing threads in groups.
This feature can only be used in broadcast channels and their
linked megagroups. Using it in a chat or private conversation
will result in ``telethon.errors.PeerIdInvalidError`` to occur.
When using this parameter, the ``filter`` and ``search``
parameters have no effect, since Telegram's API doesn't
support searching messages in replies.
.. note::
This feature is used to get replies to a message in the
*discussion* group. If the same broadcast channel sends
a message and replies to it itself, that reply will not
be included in the results.
Yields Yields
Instances of `Message <telethon.tl.custom.message.Message>`. Instances of `Message <telethon.tl.custom.message.Message>`.
@ -459,6 +492,10 @@ class MessageMethods:
from telethon.tl.types import InputMessagesFilterPhotos from telethon.tl.types import InputMessagesFilterPhotos
async for message in client.iter_messages(chat, filter=InputMessagesFilterPhotos): async for message in client.iter_messages(chat, filter=InputMessagesFilterPhotos):
print(message.photo) print(message.photo)
# Getting comments from a post in a channel:
async for message in client.iter_messages(channel, reply_to=123):
print(message.chat.title, message.text)
""" """
if ids is not None: if ids is not None:
if not utils.is_list_like(ids): if not utils.is_list_like(ids):
@ -486,7 +523,8 @@ class MessageMethods:
offset_date=offset_date, offset_date=offset_date,
add_offset=add_offset, add_offset=add_offset,
filter=filter, filter=filter,
search=search search=search,
reply_to=reply_to
) )
async def get_messages(self: 'TelegramClient', *args, **kwargs) -> 'hints.TotalList': async def get_messages(self: 'TelegramClient', *args, **kwargs) -> 'hints.TotalList':