How to Get All Users in a Telegram Channel Using Telethon

How to get all users in a telegram channel using telethon?

Sean answer won't make any difference.

Your code works for older Telethon versions. In the new versions, a new argument hash is added to GetParticipantsRequest method. Therefore, you need to pass hash as an argument too. Add hash=0 like this:

result = client(GetParticipantsRequest(InputChannel(channel.chats[0].id, channel.chats[0].access_hash), filter, num, 100, 0))

Note that the hash of the request is not the channel hash. It's a special hash calculated based on the participants you already know about, so Telegram can avoid resending the whole thing. You can just leave it to 0.

Here is an up-to-date example from official Telethon wiki.

How to get the list of (more than 200) members of a Telegram channel

Have you looked at the telethon documentation? It explains that Telegram has a server-side limit of only collecting the first 200 participants of a group. From what I see, you can use the iter_participants function with aggressive = True to subvert this problem:

https://telethon.readthedocs.io/en/latest/telethon.client.html?highlight=200#telethon.client.chats.ChatMethods.iter_participants

I haven't used this package before, but it looks like you can just do this:

from telethon import TelegramClient

# Use your own values here
api_id = 'xxx'
api_hash = 'xxx'
name = 'xxx'
channel = 'xxx'

client = TelegramClient('Lista_Membri2', api_id, api_hash)

client.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]

# get all the users and print them
for u in client.iter_participants(channel, aggressive=True):
print(u.id, u.first_name, u.last_name, u.username)

#fino a qui il codice
client.disconnect()


Related Topics



Leave a reply



Submit