How to get Telegram channel users list with Telegram Bot API

后端 未结 7 1764
旧时难觅i
旧时难觅i 2020-12-28 17:53

Anybody give a starter on how may I get information about users from my telegram bot. Imagine my bot in an admin user in my channel and I want to get my channel user list or

7条回答
  •  生来不讨喜
    2020-12-28 18:44

    As others already mentioned, you can`t list channel users via Bot API.

    But you can use MTProto API to login as a plain user and have programmatic access to everything you can see in desktop or mobile application.

    To use MTProto, you need to login to https://my.telegram.org/ with your existing Telegram account and get credentials: api_id and api_hash.

    Here is a working example of how to use Telethon python library to get list of Telegram channel/group users.

    from telethon import TelegramClient, sync
    
    api_id = 'FILL REAL VALUES HERE'
    api_hash = 'FILL REAL VALUES HERE'
    
    client = TelegramClient('xxx', api_id, api_hash).start()
    
    # get all the channels that I can access
    channels = {d.entity.username: d.entity
                for d in client.get_dialogs()
                if d.is_channel}
    
    # choose the one that I want list users from
    channel = channels[channel_name]
    
    # get all the users and print them
    for u in client.get_participants(channel):
        print(u.id, u.first_name, u.last_name, u.username)
    

    It is easy to search channels/users by name/phone/URL with client.get_entity().

提交回复
热议问题