In python-telegram-bot how to get all participants of the group?
Asked Answered
P

3

21

In Python-telegram-bot how to get, if possible, the complete list of all participants of the group at which the bot was added?

Para answered 2/1, 2016 at 16:16 Comment(0)
S
21

You can't with current API but you could the join/exit of user members via it's API.

If you check the Message object you find :

  • new_chat_participant: A new member was added to the group, information about them (this member may be the bot itself)
  • left_chat_participant: A member was removed from the group, information about them (this member may be the bot itself)

So with this two information you can track the total number of users in your chat and who they are.

The basic strategy would be to store somewhere (like a database) the occurrences of joining and exiting of users from the group.

When a user join the chat store the object User to the storage. When a user exit the chat delete the object User from the storage.

Well then do the logic as you need.

Setiform answered 8/1, 2016 at 12:36 Comment(2)
can i get all users id in group, without using store ?Jungle
Telegram does not allow thatSetiform
R
21

Also, latest API update allows you to:

  • telegram.get_chat_members_count(chat_id): Use this method to get the number of members in a chat.

  • telegram.get_chat_member(chat_id, user_id): Use this method to get information about a member of a chat.

You can combine with new_chat_participant and left_chat_participant strategy, to build information about a group.

More information here:

Raze answered 29/5, 2016 at 4:25 Comment(4)
The links posted are failing with 404.Freezing
but how it's can help me to get all users id in group ?Jungle
Supergroup does not show user left when number of members is large. BOT API has no way to get chat members. The only way is to use Telegram TDLib API.Eider
As Vadim said, you don't get information about all users (including their IDs) without having the ID of the respective user.Delimitate
N
3

As stated by the others before, it's not possible with the bot API (for now) hence you have to go the telegram API way. Start via https://core.telegram.org/api/obtaining_api_id

I had the same problem and solved it via telethon. A snipped for you to start from:

from telethon import TelegramClient
import asyncio

api_id = 1234 # Your API_ID
api_hash = "1a2b3c456" # Your APP_ID
bot_token = "87319a123b12e321ab1cd" # (via botfather, you can alternatively sign in as a user)
goupid = -120304101020

async def get_users(client, group_id):
    async for user in client.iter_participants(group_id):
        if not user.deleted:
            print("id:", user.id, "username:", user.username) 

bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)

with bot:
    asyncio.get_event_loop().run_until_complete(get_users(bot, group_id))

(Example via https://gist.github.com/rokibhasansagar/d727fb30ef5a274cf536bea73260887c)

Nedranedrah answered 2/1, 2023 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.