How to get Telegram channel users list with Telegram Bot API
In order to get user list, you need to use telegram API.
Telegram API is fairly complicated. There are some clients which can get the job done much faster.
For python, there is Telethon, and the method to get channel users is:
get_full_channel
.
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().
In order to get user list, you need to use telegram API.
Telegram API is fairly complicated. There are some clients which can get the job done much faster.
For python, there is Telethon, and the code to get channel users is:
from telethon import TelegramClient
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.functions.channels import GetAdminLogRequest
from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelAdminLogEventsFilter
from telethon.tl.types import InputUserSelf
from telethon.tl.types import InputUser
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = ****** # Your api_id
api_hash = '********************************' # Your api_hash
phone_number = '+989122594574' # Your phone number
client = TelegramClient(phone_number, api_id, api_hash)
client.session.report_errors = False
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone_number)
client.sign_in(phone_number, input('Enter the code: '))
channel = client(ResolveUsernameRequest('tabe_eshgh')) # Your channel username
user = client(ResolveUsernameRequest('amir2b')) # Your channel admin username
admins = [InputUserSelf(), InputUser(user.users[0].id, user.users[0].access_hash)] # admins
admins = [] # No need admins for join and leave and invite filters
filter = None # All events
# param: (join, leave, invite, ban, unban, kick, unkick, promote, demote, info, settings, pinned, edit, delete)
filter = ChannelAdminLogEventsFilter(True, True, True, False, False, False, False, False, False, False, False, False, False, False)
result = client(GetAdminLogRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), '', 0, 0, 10, filter, admins))
##print(result)
for _user in result.users:
##print(_user.id)
with open(''.join(['users/', str(_user.id)]), 'w') as f:
f.write(str(_user.id))
Telegram Bot doesn't keep anything about your users. You should save by yourself all users which communicate with your bot. For example, store their IDs in database.
In case of Channel - you can get this information from Channel's members list.
If you need to be notified - your bot should store users somewhere and check if user is a new one.