28

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.

max taldykin
  • 12,459
  • 5
  • 45
  • 64
M.Shahrokhi
  • 385
  • 1
  • 7
  • 19

6 Answers6

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.

apadana
  • 13,456
  • 15
  • 82
  • 98
  • For PHP, I used Madeline Proto and this get_pwr_chat function: https://docs.madelineproto.xyz/docs/CHAT_INFO.html#get_pwr_chat-now-fully-async – rockstardev Jan 03 '20 at 06:08
  • 4
    hi.how can we use this method (get-full-channel)? i mean i cant access to this method – Mohammad Dec 06 '20 at 13:54
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.

Stas Parshin
  • 7,973
  • 3
  • 24
  • 43
  • I agree, but: in case of channels: Telegram Bot APIs are NOT related to channel management at all. The news (see: https://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: http://telegram.wiki/tips:channels – Giorgio Robino Dec 01 '15 at 08:16
  • 5
    @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 bug – Stas Parshin Dec 01 '15 at 08:37
  • 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.. – Giorgio Robino Dec 01 '15 at 09:33
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))
Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
Amir Bashiri
  • 129
  • 1
  • 5
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().

max taldykin
  • 12,459
  • 5
  • 45
  • 64
  • what is that 'xxx' in ```TelegramClient('xxx', api_id, api_hash).start() ??``` – Lalit Vavdara Oct 28 '20 at 16:01
  • @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. – max taldykin Oct 29 '20 at 07:28
  • 1
    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 ? – Lalit Vavdara Oct 29 '20 at 11:07
  • If you use the same name you don't need to login. If you delete the file you'll need to login again – max taldykin Oct 29 '20 at 15:17
1

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

Belegnar
  • 721
  • 10
  • 24
  • 2
    Could you elaborate how using https://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. – Maximo Dominguez May 11 '17 at 17:46
  • The first theoretical solution dose not work. because there is no "XXX Joined the channel" information in returned update objects. – Vahid Ghadiri Oct 12 '17 at 06:08
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

Hadi.A
  • 51
  • 1
  • 6