1

I want to add a function to the bot "change chat permissions" and I can't figure out how to do it. Using an aiogram lib with postgresql database (psycopg2)

Python 3.9.5
aiogram==2.14.3

Callback handler (for example: call.data = "defaultchat-can_send_media_messages")

@dp.callback_query_handler(lambda call: "defaultchat" == call.data.split('-')[0])
async def deafultChatCallbacks(call: types.callback_query):
    chatLang = pg.getChatLang(call.message.chat.id)
    call_data = call.data.split('-')[1]
    curSettings = pg.getChatSettings(call.message.chat.id) # :rtype: dict (in db it's json)

    curSettings[call_data] = not curSettings[call_data]
    
    # Trying change the type from dict to types.ChatPermissions
    curSettings = type('types.ChatPermissions', (type,), (curSettings))
    return await call.message.chat.set_permissions(permissions=curSettings)

Dict (or json in Database):

{
    'can_send_messages': True, 
    'can_send_media_messages': True, 
    'can_send_polls': True, 
    'can_send_other_messages': True, 
    'can_add_web_page_previews': True, 
    'can_change_info': True, 
    'can_invite_users': True, 
    'can_pin_messages': True
}

Log:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-10' coro=<Dispatcher._process_polling_updates() done, defined at D:\Program Files\Python\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=BadRequest("Can't parse permissions json object")>
...
aiogram.utils.exceptions.BadRequest: Can't parse permissions json object

If i remove the line with changing the class # curSettings = type('types.ChatPermissions', (type,), (curSettings)) here is log:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-10' coro=<Dispatcher._process_polling_updates() done, defined at D:\Program Files\Python\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=BadRequest('Chat_not_modified')>
...
aiogram.utils.exceptions.BadRequest: Chat_not_modified
rvbsm
  • 13
  • 6
  • I don't understand why you use `type()` for this. As for mee it is totally wrong idea. Error shows that it needs `JSON` then maybe you should use `json.dumps(curSettings)` – furas Aug 16 '21 at 13:36
  • or maybe you should do it `curSettings = ChatPermission(**curSettings)` like in `telegram-bot` `issues: [Append/Update on ChatPermissions ](https://github.com/python-telegram-bot/python-telegram-bot/issues/1834) – furas Aug 16 '21 at 13:39

1 Answers1

0

I can't test it but based on telegram-bot issues Append/Update on ChatPermissions you should do

curSettings = ChatPermissions(**curSettings)
furas
  • 134,197
  • 12
  • 106
  • 148