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

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.


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:

  • https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.get_chat_members_count
  • https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.get_chat_member