Search code examples
ubuntutelegramdesktop-application

Get list of my Telegram channels I'm currently a member


Is there a way to get a list of channels I'm a member of in Telegram (preferably Telegram Desktop)? I may be able to come up with a solution using JS in the browser or even Selenium, but isn't there a more ergonomic approach that would work in Telegram Desktop (like an API)?

If it matters I'm running Telegram as a snap application on Ubuntu 20.04.


Solution

  • Is there a way to get a list of channels I'm a member of in Telegram (preferably Telegram Desktop)?

    Well, the official Telegram Bot API does not have an option to get members of a channel. So using Telegrams MTPROTO to make your own client that will do this is the only legit way of retreiving this data.

    As said, you could scrape the Telegram Web Version, but thats most likely to change some time soon which you'll need to edit your scraper.

    Telethon seems like a valid/stable solution, continue reading for a custom example script


    Is there a way to get a list of channels I'm a member of in Telegram (preferably Telegram Desktop)?

    If you just want to know what channels you're member of, you can use the dialog.entity to check if the current one is of type Channel

    1. Use client.iter_dialogs() to loop over each channel
    2. Use isinstance to check if it's a Channel
      • This will make sure we don't log for types like:
        • User (chats)
        • ChatForbidden (Chats your kicked out off)

    from telethon import TelegramClient
    from telethon.tl.types import  Channel
    
    import asyncio
    
    async def main():
    
        async with TelegramClient('anon', '2234242', 'e40gte4t63636423424325a57') as client:
    
            # For each dialog
            async for dialog in client.iter_dialogs(limit = None):
    
                # If this is a Channel
                if isinstance(dialog.entity, ( Channel )):
    
                    # Log
                    dialogType = type(dialog.entity)
                    print(f'{dialog.title:<42}\t{dialog.id}')
    
    asyncio.run(main())
    

    Will output something like

    channel_1           <channel_id>