Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 649
Thomas8 Avatar asked Jan 02 '16 16:01

Thomas8


People also ask

Can read all group messages Telegram bot?

Another note is that the bot MUST be an admin on that group, regardless of the privacy settings. Otherwise, it will NOT be able to read any of the group messages. No, either the bot needs to be an admin OR privacy mode needs to be disabled.


2 Answers

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.

like image 161
unnikked Avatar answered Sep 23 '22 01:09

unnikked


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
like image 30
Leandro Toledo Avatar answered Sep 20 '22 01:09

Leandro Toledo