Telethon
Telethon copied to clipboard
Working with threads/comments
It is the second day I follow he RTFM approach and I failed... I can get regular messages, But I get stuck on channel, that has comments to messages. I believe this functionality is described here: https://core.telegram.org/api/threads
When I do:
async with client:
async for message in client.iter_messages(Channel):
if (message.replies):
print("Message ID:",message.id,'Count replies',message.replies.replies,'\n')
I can get results:
Message ID: 27 Count replies 7
Message ID: 26 Count replies 19
Message ID: 24 Count replies 6
Each message id has message.text so I can get into them. But how to read replies to Message? They seem to belong to class 'telethon.tl.types.PeerUser'
Am I dumb and cannot read docs, or the solutions is not so much straightforward?
You can now get comments in a channel post with the reply_to parameter in client. iter_messages(). Comments are messages that “reply to” a specific channel message, hence the name (which is consistent with how Telegram’s API calls it).
looks like the thing I look for.
also found this: https://github.com/LonamiWebs/Telethon/issues/1598
But did not help much. I do not get into comments...
ChannelMessages(pts=126, count=7, messages=[], chats=[], users=[], inexact=False, offset_id_offset=7)
It took me reading through @Lonami code commitment to understand that I have to do:
async with client:
async for message in client.iter_messages(ID_of_the_GroupChat, reply_to=ID_of_the_message_with_comments):
print(message.chat.title, message.text)
Yeah maybe the documentation could use some work or the parameter renaming (from the technical term to the actually used one).
Yeah maybe the documentation could use some work or the parameter renaming (from the technical term to the actually used one).
I am now struggling with error and trying to diagnose.... In some conversations, when running:
async with client:
async for message in client.iter_messages(Auction): # Go through messages in Auction
if message.text and message.id == 118:
if "Основной канал" not in message.text:
a = "https://t.me/c/"+str(message.peer_id.channel_id)+"/"+str(message.id)
print(a)
print(message.id,message.text)
print(message)
top_bid = await client.get_messages(Auction, reply_to=(message.id))
Auction - defined auction number (group chat) message.id == 118, message causing error.
There is 50 messages that have replies, this one has replies (as a conversation) and this is the only one that causes error:
---------------------------------------------------------------------------
---------------------------------------------------------------------------
MsgIdInvalidError Traceback (most recent call last)
/tmp/ipykernel_528/3491937414.py in async-def-wrapper()
38 print(top_bid[0])
5 frames
[/usr/local/lib/python3.7/dist-packages/telethon/client/messages.py](https://localhost:8080/#) in get_messages(self, *args, **kwargs)
584 return None
585
--> 586 return await it.collect()
587
588 get_messages.__signature__ = inspect.signature(iter_messages)
[/usr/local/lib/python3.7/dist-packages/telethon/requestiter.py](https://localhost:8080/#) in collect(self)
111 """
112 result = helpers.TotalList()
--> 113 async for message in self:
114 result.append(message)
115
[/usr/local/lib/python3.7/dist-packages/telethon/requestiter.py](https://localhost:8080/#) in __anext__(self)
72 self.index = 0
73 self.buffer = []
---> 74 if await self._load_next_chunk():
75 self.left = len(self.buffer)
76
[/usr/local/lib/python3.7/dist-packages/telethon/client/messages.py](https://localhost:8080/#) in _load_next_chunk(self)
182 self.request.add_offset = self.add_offset - self.request.limit
183
--> 184 r = await self.client(self.request)
185 self.total = getattr(r, 'count', len(r.messages))
186
[/usr/local/lib/python3.7/dist-packages/telethon/client/users.py](https://localhost:8080/#) in __call__(self, request, ordered, flood_sleep_threshold)
28 class UserMethods:
29 async def __call__(self: 'TelegramClient', request, ordered=False, flood_sleep_threshold=None):
---> 30 return await self._call(self._sender, request, ordered=ordered)
31
32 async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sleep_threshold=None):
[/usr/local/lib/python3.7/dist-packages/telethon/client/users.py](https://localhost:8080/#) in _call(self, sender, request, ordered, flood_sleep_threshold)
82 return results
83 else:
---> 84 result = await future
85 self.session.process_entities(result)
86 self._entity_cache.add(result)
MsgIdInvalidError: The message ID used in the peer was invalid (caused by GetRepliesRequest)
I also have similar doubts. Can somebody please look into this issue https://github.com/LonamiWebs/Telethon/issues/3837 and help to fix it.
Closing since I find the current documentation of the reply_to
parameter in iter_messages
sufficient, but I'm open to merging a PR which improves it.