Skip to content

Commit aad72ed

Browse files
committed
Add get_chat_members.py
1 parent b28f2eb commit aad72ed

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

examples/get_chat_members.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2018 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import time
20+
21+
from pyrogram import Client
22+
from pyrogram.api.errors import FloodWait
23+
24+
app = Client("my_account")
25+
26+
target = "pyrogramchat" # Target channel/supergroup
27+
members = [] # List that will contain all the members of the target chat
28+
offset = 0 # Offset starts at 0
29+
limit = 200 # Amount of users to retrieve for each API call (max 200)
30+
31+
with app:
32+
while True:
33+
try:
34+
chunk = app.get_chat_members(target, offset)
35+
except FloodWait as e: # Very large chats could trigger FloodWait
36+
time.sleep(e.x) # When it happens, wait X seconds and try again
37+
continue
38+
39+
if not chunk.chat_members:
40+
break # No more members left
41+
42+
members.extend(chunk.chat_members)
43+
offset += len(chunk.chat_members)
44+
45+
# Now the "members" list contains all the members of the target chat

0 commit comments

Comments
 (0)