mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +03:00
Show example on how to get all members from a chat
parent
55ae027f2b
commit
6219477721
25
Retrieving-all-chat-members.md
Normal file
25
Retrieving-all-chat-members.md
Normal file
|
@ -0,0 +1,25 @@
|
|||
In order to get all the members from a mega-group or channel, you need to use [`GetParticipantsRequest`](https://lonamiwebs.github.io/Telethon/methods/channels/get_participants.html). As we can see it needs an [`InputChannel`](https://lonamiwebs.github.io/Telethon/methods/channels/get_participants.html), (passing the mega-group or channel you're going to use will work), and a mandatory [`ChannelParticipantsFilter`](https://lonamiwebs.github.io/Telethon/types/channel_participants_filter.html). The closest thing to "no filter" is to simply use [`ChannelParticipantsSearch`](https://lonamiwebs.github.io/Telethon/constructors/channel_participants_search.html) with an empty `'q'` string.
|
||||
|
||||
If we want to get *all* the members, we need to use a moving offset and a fixed limit:
|
||||
|
||||
```python
|
||||
from telethon.tl.functions.channels import GetParticipantsRequest
|
||||
from telethon.tl.types import ChannelParticipantsSearch
|
||||
from time import sleep
|
||||
|
||||
offset = 0
|
||||
limit = 100
|
||||
all_participants = []
|
||||
|
||||
while True:
|
||||
participants = client(GetParticipantsRequest(
|
||||
channel, ChannelParticipantsSearch(''), offset, limit
|
||||
))
|
||||
if not participants.users:
|
||||
break
|
||||
all_participants.extend(participants.users)
|
||||
offset += limit
|
||||
sleep(1)
|
||||
```
|
||||
|
||||
Note that `GetParticipantsRequest` returns [`ChannelParticipants`](https://lonamiwebs.github.io/Telethon/constructors/channels/channel_participants.html), which may have more information you need (like the role of the participants, total count of members, etc.)
|
Loading…
Reference in New Issue
Block a user