skip to Main Content

I am working on a project and I would like to fetch first 10 messages from a specific group topic in telegram, unfortunately I can’t find any way for this in documentation.
I am using telethon 1.30.3.

I’ve tried to use standard TelegramClient.get_enitty for this with a direct link to forum topic channel = await client.get_entity('https://t.me/ddostup/1118'), but telethon failed to work with it.

2

Answers


  1. Chosen as BEST ANSWER

    The solution lays in internal implementation of topics by telegram. Topics are implemented as message threads, so all messages in group(forum) topic have reply_id being set to topic id

    Thus this code will iterate over all messages in a group topic

    async for message in client.iter_messages(channel, reply_to=topic_id, limit=10)
    

  2. simply to do it you can follow up my code :
    Make sure you create a APP in the dev section and repalce the app_id as well api_hash

    from telethon import TelegramClient, events
    from loguru import logger
    
    
    # Replace YOUR_API_ID and YOUR_API_HASH with the values from your Telegram API
    api_id = ''
    api_hash = ''
    channelsNames = ["prdscloud"]
    client = TelegramClient('session_name', api_id, api_hash)
    
    async def main():
        await client.start()
        for channel_username in channelsNames:
            try:
                logger.info("Checking for logs in channel: 🔥🔥 " + channel_username)
                messages = await client.get_messages(channel_username, limit=10, offset_id=0)
                if not messages:
                    break
                logger.info("Found " + str(len(messages)) + " messages.")
            except Exception as e:
                print(e)
                pass
        print("All messages have been fetched.")
    
    client.loop.run_until_complete(main())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search