Checking permissions and restrictions - Kyle2142/PHPBot GitHub Wiki
Often you need to check if your bot has the correct permissions to do something.
The function $bot->getPermissions($chat_id)
will return all the permissions the bot has, intelligently setting the irrelevant permissions to a value that makes sense.
For example, if your bot is an admin, that means it can obviously send messages (you cannot be admin and restricted at the same time), so can_send_messages
will be set to true
.
Note: I use print_r
rather than var_dump
because it looks cleaner, but false
is not printed, so you should remember that
//below is FALSE, not NULL or anything else
[can_pin_messages] =>
//below is TRUE, not int(1)
[can_send_messages] => 1
This is what the output looks like:
print_r($bot->getPermissions('@my_awesome_channel'));
/* Outputs:
stdClass Object
(
[user] => stdClass Object
(
[id] => 987654321
[is_bot] => 1
[first_name] => PHPBot example bot
[username] => Example
)
[status] => administrator //bot is admin
[can_be_edited] => //cannot edit own permissions
[can_change_info] => 1 //can edit group photo/title/etc
[can_delete_messages] => 1 //can delete other's messages
[can_invite_users] => 1 //self-explanatory
[can_restrict_members] => 1 //can kick/mute/etc other members
[can_pin_messages] => //cannot pin messages
[can_promote_members] => //cannot promote other members
[can_send_messages] => 1 //this permission and the rest are not important here
[can_send_media_messages] => 1
[can_send_other_messages] => 1
[can_add_web_page_previews] => 1
)
*/
This may look confusing, but its really simple. Let's say you wanted to make sure your bot has kick permissions before trying to remove a spammer, the code would be as simple as:
$privs = $bot->getPermissions(-100123456789);
if($privs->can_restrict_members){
//code to kick (or mute) here
}
There are other states than "administrator". See below:
member
This is the general situation, where a bot has just been added and has no special permissions or restrictions
/*
[status] => member
[can_change_info] =>
[can_delete_messages] =>
[can_invite_users] =>
[can_restrict_members] =>
[can_pin_messages] =>
[can_promote_members] =>
[can_send_messages] => 1
[can_send_media_messages] => 1
[can_send_other_messages] => 1
[can_add_web_page_previews] => 1
*/
restricted
/*
[status] => restricted
[until_date] => 0
[can_send_messages] => 1
[can_send_media_messages] =>
[can_send_other_messages] =>
[can_add_web_page_previews] =>
[can_change_info] =>
[can_delete_messages] =>
[can_invite_users] =>
[can_restrict_members] =>
[can_pin_messages] =>
[can_promote_members] =>
*/
left
for public groups/channels (with @username), your bot will appear as "left" if it is not part of the group
/*
[status] => left
[can_change_info] =>
[can_delete_messages] =>
[can_invite_users] =>
[can_restrict_members] =>
[can_pin_messages] =>
[can_promote_members] =>
[can_send_messages] =>
[can_send_media_messages] =>
[can_send_other_messages] =>
[can_add_web_page_previews] =>
*/
other
if the bot was kicked or is not part of the group, you will get an error 403, but the function will still tell you about the lack of permissions.
Additionally, the function sets status='left'
for you.
/*
[ok] =>
[error_code] => 403
[description] => Forbidden: bot is not a member of the supergroup chat
[status] => left
[can_change_info] =>
[can_delete_messages] =>
[can_invite_users] =>
[can_restrict_members] =>
[can_pin_messages] =>
[can_promote_members] =>
[can_send_messages] =>
[can_send_media_messages] =>
[can_send_other_messages] =>
[can_add_web_page_previews] =>
*/