skip to Main Content

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.

2

Answers


  1. 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())
    
    Login or Signup to reply.
  2. You can get all messages by following the below steps:

    • initialize a telethon client
    • fetch messages from a channel
    from telethon.sync import TelegramClient
    
    api_id = 'YOUR_API_ID'  # Replace with your API ID
    api_hash = 'YOUR_API_HASH'  # Replace with your API hash
    phone_number = 'YOUR_PHONE_NUMBER'  # Replace with your phone number (used for the login)
    
    # Initialize the Telethon client
    with TelegramClient(phone_number, api_id, api_hash) as client:
        
        # Ensure you are signed in (if not, this will ask for the code Telegram sends you)
        client.start()
    
        # Replace 'SomeChannel' with the username of the channel
        # If the channel doesn't have a username, you'll need its ID
        for message in client.iter_messages('SomeChannel'):
            print(message.text)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search