Pier Giorgio Liprino
Pier Giorgio Liprino

Reputation: 43

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

I'm working with Telethon to get the entire (more than 200) member list of a Telegram channel.

Trying, trying and trying again, I found that this piece of code is perfect to reach my goal, if it were not that it prints only first 200 members.

from telethon import TelegramClient, sync

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

client = TelegramClient('Lista_Membri2', api_id, api_hash)
try:
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.get_participants(channel):
 print(u.id, u.first_name, u.last_name, u.username)

#fino a qui il codice

finally:
client.disconnect()

Someone has the solution?

Upvotes: 4

Views: 7263

Answers (1)

Jordan Singer
Jordan Singer

Reputation: 573

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

Upvotes: 3

Related Questions