I am using the telegram bot api to make a bot. I have some commands that can only be sent from admins. Like kick and ban commands. How do I check if the sender is an admin or not? I am using the python-telegram-bot
api. I do not want everyone to be able to ban members.

- 1,000
- 1
- 11
- 32
3 Answers

- 7,433
- 1
- 19
- 39
-
What variable is admin stored under? – ICanKindOfCode Jan 07 '18 at 12:05
-
3@KidDoesCodingAndHasNoFriends its the `status` variable. Check if it is `"creator"` or `"administrator"` – Stan James Feb 22 '18 at 23:17
I have found after searching a bit. The admin status is stored under Telegram.ChatMember.status
. It is documented here. It is used by bot.get_chat_member(chat_id, user_id)
. And then getting status in it.

- 1,000
- 1
- 11
- 32
The other answers are correct, but require an additional call to the API. An efficient solution is to cache the list of admins.
A good solution for this is described here, copied below:
Cached Telegram group administrator check
If you want to limit certain bot functions to group administrators, you have to test if a user is an administrator in the group in question. This however requires an extra API request, which is why it can make sense to cache this information for a certain time, especially if your bot is very busy.
This snippet requires this timeout-based cache decorator. (gist mirror)
Save the decorator to a new file named mwt.py
and add this line to your imports:
from mwt import MWT
Then, add the following decorated function to your script. You can change the timeout as required.
@MWT(timeout=60*60)
def get_admin_ids(bot, chat_id):
"""Returns a list of admin IDs for a given chat. Results are cached for 1 hour."""
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
You can then use the function like this:
if update.message.from_user.id in get_admin_ids(bot, update.message.chat_id):
# admin only
Note: Private chats and groups with all_members_are_administrator
flag, are not covered by this snippet. Make sure you handle them.

- 2,535
- 1
- 28
- 35
-
could you explain please, where did u get 'bot' ? Is this bot same that what we import from telegram -> from telegram import Bot ? – Fuad Teymurov Feb 26 '22 at 18:45