Skip to content

Retrieving all chat members

Sergey edited this page Sep 25, 2017 · 6 revisions

In order to get all the members from a mega-group or channel, you need to use GetParticipantsRequest. As we can see it needs an InputChannel, (passing the mega-group or channel you're going to use will work), and a mandatory ChannelParticipantsFilter. The closest thing to "no filter" is to simply use ChannelParticipantsSearch with an empty 'q' string.

If we want to get all the members, we need to use a moving offset and a fixed limit:

from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from time import sleep

offset = 0
limit = 50
all_participants = []

while True:
    participants = client.invoke(GetParticipantsRequest(
        channel, ChannelParticipantsSearch(''), offset, limit
    ))
    if not participants.users:
        break
    all_participants.extend(participants.users)
    offset += len(participants.users)
    sleep(1)

Note that GetParticipantsRequest returns ChannelParticipants, which may have more information you need (like the role of the participants, total count of members, etc.)