Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Append/Update on ChatPermissions #1834

Closed
AyraHikari opened this issue Mar 18, 2020 · 6 comments
Closed

[QUESTION] Append/Update on ChatPermissions #1834

AyraHikari opened this issue Mar 18, 2020 · 6 comments

Comments

@AyraHikari
Copy link

Issue I am facing

I am facing issue when trying to set permission for send poll to True
chat.set_permissions(permission=ChatPermissions(can_send_polls=True))
Then send media, send stickers and gifs, and embed links are turned off.
Because it should not, so i am going to check current permission and replace to new permission and set new one.
And now i am stuck at ChatPermission, since it wasn't dict. So i can't merge or append at ChatPermission.

Traceback to the issue

chat.set_permissions(permission=ChatPermissions(can_send_polls=True))

changed default permissions

+Send polls
−Send media
−Send stickers & GIFs
−Embed links

Related part of your code

current = context.bot.getChat(chat_id).permissions
new = {'can_send_polls': True}

permissions = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}
permissions.update(current)
permissions.update(new)
# ???

doing chat.set_permissions(permission=permissions) will got error since set_permissions is converting to_dict() at

data = {'chat_id': chat_id, 'permissions': permissions.to_dict()}

@Bibo-Joshi
Copy link
Member

Related to #1803 (see this comment): TL;DR: right now, you need to set all the permissions to True manually. This is only implicitly clear from the official API docs, unfortunately. I'll make sure to add a note to the docs about that.

About "merging" ChatPermissions: You can just do

current = context.bot.getChat(chat_id).permissions
current.can_send_polls = True
current.can_send_media = False 
...
context.bot.set_chat_permission(chat_id = chat_id, permissions = current)

Alternatively, if you want to stick with the dictionary approach:

current = context.bot.getChat(chat_id).permissions
new = {'can_send_polls': True}

permissions_kwargs = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}
permissions_kwargs .update(current)
permissions_kwargs .update(new)
permissions = ChatPermission(**permissions_kwargs) # creates a ChatPermissions instance based on your kwargs

context.bot.set_chat_permission(chat_id = chat_id, permissions = current)

@AyraHikari
Copy link
Author

Thanks for your explain and example code, now everything was fine, here is my final code

current = eval(str(context.bot.getChat(chat_id).permissions)) # make sure it was dict
new = {'can_send_polls': False}

permissions = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}
permissions.update(current)
permissions.update(new)
new_permissions = ChatPermissions(**permissions)

context.bot.set_chat_permissions(chat_id=chat_id, permissions=new_permissions)

Result are

changed default permissions

−Send polls

@AyraHikari
Copy link
Author

Also it seems disable can_send_media_messages was doesn't work.
Trying with {'can_send_media_messages': False} is not going to enable
Also do {'can_send_media_messages': False, 'can_send_other_messages': False, 'can_send_polls': False} media permission not disabled, just sticker & gifs and poll only
But {'can_send_media_messages': True} is works for enable media permission 🤔

@Bibo-Joshi
Copy link
Member

Bibo-Joshi commented Mar 18, 2020

Can confirm that something is wrong there. However, this is a Telegram issue. All PTB can do, is pass the values to the API. I get the same results when making simple http requests in the browser. Maybe contact @botsupport about it … Also, keep in mind that e.g. can_send_messages will be True automatically, when switching can_send_media_messages to True.

@Bibo-Joshi Bibo-Joshi mentioned this issue Mar 19, 2020
@jesvijonathan
Copy link

jesvijonathan commented Aug 13, 2020

I can't restrict users from messaging ("an_send_messages":False) just as mentioned by @AyraHikari, how then are other telegram bots able to restrict users ? or is there any other way ?

But un-restricting ("an_send_messages":True) restricted users works fine...

My function contains

` msg = update.message.reply_to_message

user_id = msg.from_user.id
chat_id = update.effective_chat.id
msg_text = update.message.text.split(None, 1)

chat_id = update.effective_chat.id
current = eval(str(context.bot.getChat(chat_id).permissions))
new = {'an_send_messages': False}

permissions = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}

permissions.update(current)
permissions.update(new)
new_permissions = ChatPermissions(**permissions)

context.bot.restrict_chat_member(chat_id, user_id,permissions=new_permissions)
update.message.reply_text("Muted!")`

@jesvijonathan
Copy link

Ok, so it looks like to restrict a member from sending a message, we have to restrict 4 other permissions along with it,

so previous code :

new = {'an_send_messages': False}

permissions = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}

updated code :

new = {'can_send_messages': False, 'can_send_media_messages': False,'can_send_polls': False,'can_send_other_messages': False, 'can_add_web_page_previews': False,}

permissions = {'can_send_messages': None, 'can_send_media_messages': None, 'can_send_polls': None, 'can_send_other_messages': None, 'can_add_web_page_previews': None, 'can_change_info': None, 'can_invite_users': None, 'can_pin_messages': None}

the updated code works like charm 👍🏽

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants