1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

UPG: Add ChatPermissions, set_chat_permissions

This commit is contained in:
Anthony Byuraev 2020-05-11 16:38:09 +03:00
parent a80927baf9
commit a60253bf60
3 changed files with 82 additions and 3 deletions

View File

@ -547,9 +547,11 @@ class TeleBot:
def get_chat_administrators(self, chat_id):
"""
Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects
that contains information about all chat administrators except other bots.
:param chat_id:
Use this method to get a list of administrators in a chat.
On success, returns an Array of ChatMember objects that contains
information about all chat administrators except other bots.
:param chat_id: Unique identifier for the target chat or username
of the target supergroup or channel (in the format @channelusername)
:return:
"""
result = apihelper.get_chat_administrators(self.token, chat_id)
@ -999,6 +1001,19 @@ class TeleBot:
"""
return apihelper.set_chat_administrator_custom_title(self.token, chat_id, user_id, custom_title)
def set_chat_permissions(self, chat_id, permissions):
"""
Use this method to set default chat permissions for all members.
The bot must be an administrator in the group or a supergroup for this to work
and must have the can_restrict_members admin rights.
:param chat_id: Unique identifier for the target chat or username of the target supergroup
(in the format @supergroupusername)
:param permissions: New default chat permissions
:return:
"""
return apihelper.set_chat_permissions(self.token, chat_id, permissions)
def export_chat_invite_link(self, chat_id):
"""
Use this method to export an invite link to a supergroup or a channel. The bot must be an administrator

View File

@ -620,6 +620,14 @@ def set_chat_administrator_custom_title(token, chat_id, user_id, custom_title):
return _make_request(token, method_url, params=payload, method='post')
def set_chat_permissions(token, chat_id, permissions):
method_url = 'setChatPermissions'
payload = {
'chat_id': chat_id,
'permissions': _convert_list_json_serializable(permissions)}
return _make_request(token, method_url, params=payload, method='post')
def export_chat_invite_link(token, chat_id):
method_url = 'exportChatInviteLink'
payload = {'chat_id': chat_id}

View File

@ -1062,6 +1062,62 @@ class ChatMember(JsonDeserializable):
self.can_add_web_page_previews = can_add_web_page_previews
class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable):
def __init__(self, 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):
self.can_send_messages = can_send_messages
self.can_send_media_messages = can_send_media_messages
self.can_send_polls = can_send_polls
self.can_send_other_messages = can_send_other_messages
self.can_add_web_page_previews = can_add_web_page_previews
self.can_change_info = can_change_info
self.can_invite_users = can_invite_users
self.can_pin_messages = can_pin_messages
@classmethod
def de_json(cls, json_string):
if json_string is None:
return json_string
obj = cls.check_json(json_string)
can_send_messages = obj.get('can_send_messages')
can_send_media_messages = obj.get('can_send_media_messages')
can_send_polls = obj.get('can_send_polls')
can_send_other_messages = obj.get('can_send_other_messages')
can_add_web_page_previews = obj.get('can_add_web_page_previews')
can_change_info = obj.get('can_change_info')
can_invite_users = obj.get('can_invite_users')
can_pin_messages = obj.get('can_pin_messages')
return cls(
can_send_messages, can_send_media_messages, can_send_polls,
can_send_other_messages, can_add_web_page_previews,
can_change_info, can_invite_users, can_pin_messages)
def to_json(self):
return json.dumps(self.to_dict())
def to_dict(self):
json_dict = dict()
if self.can_send_messages is not None:
json_dict['can_send_messages'] = self.can_send_messages
if self.can_send_media_messages is not None:
json_dict['can_send_media_messages'] = self.can_send_media_messages
if self.can_send_polls is not None:
json_dict['can_send_polls'] = self.can_send_polls
if self.can_send_other_messages is not None:
json_dict['can_send_other_messages'] = self.can_send_other_messages
if self.can_add_web_page_previews is not None:
json_dict['can_add_web_page_previews'] = self.can_add_web_page_previews
if self.can_change_info is not None:
json_dict['can_change_info'] = self.can_change_info
if self.can_invite_users is not None:
json_dict['can_invite_users'] = self.can_invite_users
if self.can_pin_messages is not None:
json_dict['can_pin_messages'] = self.can_pin_messages
return json_dict
class BotCommand(JsonSerializable):
def __init__(self, command, description):
"""