Sina
Sina

Reputation: 49

pytelegrambotapi check bot permissions in groups

I am trying to fix an error where the bot tries to send a message to a chat group but doesn't have administration permissions.

I need an if statement like the one below before sending the message to the chat group, however this code is not working:

if bot.get_chat(Message.chat.id).permissions.can_send_messages:
    bot.reply_to(NewMessage, response, parse_mode="HTML")

Do you know what is the best solution?

Upvotes: 1

Views: 460

Answers (1)

Megasrx
Megasrx

Reputation: 116

Use get_chat_member method to get information about a member of a chat. Returns a ChatMember object on success.

PARAMETERS:

chat_id (int or str) – Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)

user_id (int) – Unique identifier of the target user.

Returns ChatMember object.

EXAMPLE

bot_member = bot.get_chat_member(chat_id, bot.id)

if bot_member.can_send_messages:
    bot.reply_to(NewMessage, response, parse_mode="HTML")

Upvotes: 2

Related Questions