Telegram Get All Group Members with Telethon
In the previous post, we obtained the list of IDs of active group members by querying the most recent 1000 messages in a group.

If I want to be especially kind to the members of this group, as long as they are currently in the group, I don't want any of them to be disturbed by bots.
Then I need to get all the members within the group.
Developed with GPT.
Don't forget, we just finished the project for querying recent messages in a group. For ease of reading and learning, copy and paste all the code from the previous project, and add the following requirements. Send it to GPT.
Query all members in a telegram group
Output format is yaml
telegram account userid: uesrname: telegram account username
full_name: telegram account full name
For example
6611601789:
username: skypatch
full_name: 蛙 女
This group may be a public group or a non-public group
After some simple debugging, the code ran normally. But the users obtained included users in the exceptions. This is not what we want; the user list we want should also exclude this portion of users.
This step took a relatively long time to figure out, and the results returned by GPT had issues.
I used Claude, Gemini, and ChatGPT at the same time.
Combined with the official Telethon documentation. Finally got it done.
Uploaded to Github
Running result
Fetching group member list: FuckGFW-Newbie 翻墙新手村
Skipping user ID in Exceptions list: 816194782
Skipping user ID in Exceptions list: 6521155977
...omitted
--- Group Member List ---
6617181826:
username: atefatman
full_name: 子 肥
7919966027:
username: crazypeace_anti_bot_bot
full_name: CZ_antibot_bot
...omitted
✅ A total of 1216 members fetched
📄 Saved to file: members_1517821953.yaml
========
Postscript
In Telethon, both ChannelParticipantBanned and ChannelParticipantsBanned are "things" that are valid.
After import, you can use
from telethon.tl.types import ChannelParticipantBanned, ChannelParticipantsBanned
ChannelParticipantsBanned
is a filter, used as a parameter for the client.iter_participants() function
See the official documentation
When ChannelParticipantsBanned is used as a filter, calling the client.iter_participants() function returns such users:Exceptions in the group's Permissions
ChannelParticipantBanned
is a possible value of a user's "attribute" participant, used to determine whether the user is in this list:
Exceptions in the group's Permissions (see image above)
The usage is like this:
isinstance(user.participant, ChannelParticipantBanned)

What needs to be noted is
Both of these "things" are called *****Banned. But actually, Exceptions means a setting that takes priority over the group's default permission settings.
For example, in my group, the default user permission does not allow sending stickers. However, a certain user can be set in Exceptions to be able to send stickers. Do you think this counts as "Banned"?
========

