Search code examples
pythontelegramtelegram-bottelethon

How can I access messages in discussions of posts on channels through the Telegram API?


I've read the documentation and couldn't find the answer, has anyone encountered this problem before?

I am using the Telethon library for Python. I only need to retrieve the text of messages from discussions in channels where my bot is a member.


Solution

  • you can use this function to retrieve all comments text from one post:

    def get_comments(client: TelegramClient, channel: str, post_id: int):
        async def crawl_comments():
            async for message in client.iter_messages(channel, reply_to=post_id, reverse=True):
                comment = message.text
                print(comment)
        with client:
           client.loop.run_until_complete(crawl_comments())