skip to Main Content

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
},
  

2

Answers


  1. Chosen as BEST ANSWER

    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())
    

  2. If you had joined to this group manually, It can work.

    async for message in client.iter_messages(peer):
        if message.replies:
            channel_peer = types.InputChannel(message.replies.channel_id, 0)
            chat = await client.get_entity(channel_peer)
    

    But better way is to get full channel request, so you can get a channel itself and chat as an entities (and maybe join it?), then find if any of them are replies to channel post.

    chat_full = await client(functions.channels.GetFullChannelRequest(peer))
    channel = chat_full.chats[0]
    
    chat = channel_full.linked_chat_id
    if chat:
        chat = chat_full.chats[-1]
    
    async for message in client.iter_messages(peer):
        msg_data = {
            "id": message.id,
            "date": message.date,
            "message": message.message,
        }
        if chat:
            msg_data["replies"] = client.get_messages(chat, reply_to=message.id)
            
    

    This code is untested, but I am working on it o_0

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search