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
Get Discord User Id from Username
Saving the Output of a Python Program
Finding Out Who Got the Highest Mark Among the Students
Get Only Unique Words from a Sentence in Python
Check If a Key Exists in a Bucket in S3 Using Boto3
How to Delete a Specific Line in a File
How to End Program If Input == "Quit" With Many If Statements
How to Substitute Value for a Variable in a Json in Python
How to Put a Space Between Two String Items in Python
Python: [Errno 10054] an Existing Connection Was Forcibly Closed by the Remote Host
How to Convert Datetime by Removing Nanoseconds
Filter All Rows That Do Not Contain Letters (Alpha) in 'Pandas'
How to Overwrite the Previous Print to Stdout