Search code examples
pythontelegramtelethon

how to get messages from linked discussion group of channels with telethon


I have already successfully retrieved messages from channels. I do that with the iter_messages function However, the message object does not contain the comments, only the users wrote the comments. In the object is a channel_id, this seems to be the linked group. But the group doesn't have a URL like t.me/xxx. Does anyone have an approach for a solution?

Here is an excerpt from the object as JSON.

 "replies": {
  "_": "MessageReplies",
  "replies": 8,
  "replies_pts": 17846,
  "comments": true,
  "recent_repliers": [
    {
      "_": "PeerUser",
      "user_id": 57135752
    },
    {
      "_": "PeerUser",
      "user_id": 564589817
    },
    {
      "_": "PeerUser",
      "user_id": 888542547
    }
  ],
  "channel_id": 1484030956,
  "max_id": 13402,
  "read_max_id": null
},
  

Solution

  • Finally i found a solution which is working fine. When you have the specific message id you can set the flag reply_to. Then you get the comments of the channel posts.

    def get_comments(client: TelegramClient, channel: str, message_id: int):
        async def crawl_comments():
            async for message in client.iter_messages(channel, reply_to=message_id):
                print(message.text)  # only comment
                full_comment_obj = message.to_dict()  # in JSON-Format
                print(full_comment_obj)
    
        with client:
           client.loop.run_until_complete(crawl_comments())