How to get Telegram channel users list with Telegram Bot API
Asked Answered
V

6

29

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 to be noticed when a new user joins. How can I do that. Telegram's documents are so unorganized. So far I have looked at these:

But none of these really helps.

Vanderhoek answered 21/11, 2015 at 14:8 Comment(0)
S
27

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.

Sandhurst answered 14/6, 2017 at 18:42 Comment(2)
For PHP, I used Madeline Proto and this get_pwr_chat function: docs.madelineproto.xyz/docs/…Unconformity
hi.how can we use this method (get-full-channel)? i mean i cant access to this methodTraduce
P
15

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.

Poche answered 22/11, 2015 at 15:2 Comment(3)
I agree, but: in case of channels: Telegram Bot APIs are NOT related to channel management at all. The news (see: core.telegram.org/bots/api#recent-changes) is that a channel owner can add (one or more) bot as channel administrator(s). In this case "admin bot" can access to channel users IDs. See: telegram.wiki/tips:channelsTorruella
@GiorgioRobino right now bot can only have access to group's messages, but bot doesn't have access to group or channel users (there is no such API), and even if bot is admin, he doesn't have access to channel's messages. Maybe its a bugPoche
Yes. There is not a specific Bot API to retrieve all IDs (neither for 1. Bot subscribers, neither for 2. bot as admin of a channel). In case 1 bot can collect and store IDs (as you said). In case 2: A channel administrator bot COULD see channel messages. If you tested that's this in not true...(I haven't yet), so I agree with: it's a bug..Torruella
G
9

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))
Geibel answered 10/9, 2017 at 9:35 Comment(0)
R
9

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().

Rainstorm answered 23/11, 2018 at 0:35 Comment(4)
what is that 'xxx' in TelegramClient('xxx', api_id, api_hash).start() ??Banks
@LalitVavdara, xxx is a session name. It can be almost anything you like. Telethon will create a session storage file named xxx.session in the current directory. Btw, this file is an SQLite db actually.Rainstorm
thanks man for replying, I wasn't expecting reply after 2 years of answer posted, by the way if I create a new session a new file will be created so can I delete the old file? Also If I use same name as last session then will it still require me to login ?Banks
If you use the same name you don't need to login. If you delete the file you'll need to login againRainstorm
E
1

Bot can't get access to channel users list via api. One have two possibilities to achieve this:

Eviaevict answered 25/1, 2017 at 9:21 Comment(2)
Could you elaborate how using github.com/vysheng/tgl will answer the question? I don't see any documentation other than the official, which we already determined doesn't support user lists out of the box.Drily
The first theoretical solution dose not work. because there is no "XXX Joined the channel" information in returned update objects.Pitapat
M
0

There is not any api for Telegram bot to access channel or group users. If access to group users is important for you, I suggest you to use Telegram-CLI. You can access to all Telegran user account's API so you have access to all of your group's users data.

https://github.com/vysheng/tg

Mujik answered 30/5, 2017 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.