From a9b878107cef661ec7acca5abdfc79a5ef94c39d Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 4 Feb 2023 22:24:26 +0400 Subject: [PATCH] Fix can_send_media_messages param, added warnings --- telebot/__init__.py | 31 ++++++++++++++++++++++++++----- telebot/apihelper.py | 30 +++++------------------------- telebot/async_telebot.py | 30 +++++++++++++++++++++++++----- telebot/asyncio_helper.py | 29 ++++------------------------- telebot/types.py | 16 +++++++++++++++- 5 files changed, 75 insertions(+), 61 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 0d6e37b..70daddc 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2918,6 +2918,7 @@ class TeleBot: can_change_info: Optional[bool]=None, can_invite_users: Optional[bool]=None, can_pin_messages: Optional[bool]=None, + permissions: Optional[types.ChatPermissions]=None, use_independent_chat_permissions: Optional[bool]=None) -> bool: """ Use this method to restrict a user in a supergroup. @@ -2926,6 +2927,10 @@ class TeleBot: Telegram documentation: https://core.telegram.org/bots/api#restrictchatmember + .. warning:: + Please pass `telebot.types.ChatPermissions` object to `permissions` parameter instead of + passing all boolean parameters. Those boolean parameters won't be supported soon, so please take it into consideration. + :param chat_id: Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername) :type chat_id: :obj:`int` or :obj:`str` @@ -2972,15 +2977,31 @@ class TeleBot: can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. :type use_independent_chat_permissions: :obj:`bool` + :param permissions: Pass ChatPermissions object to set all permissions at once. Use this param instead of + passing all boolean parameters. + :type permissions: :class:`telebot.types.ChatPermissions` + :return: True on success :rtype: :obj:`bool` """ + + if permissions is None: + permissions = types.ChatPermissions( + can_send_messages=can_send_messages, + can_send_media_messages=can_send_media_messages, + can_send_polls=can_send_polls, + can_send_other_messages=can_send_other_messages, + can_add_web_page_previews=can_add_web_page_previews, + can_change_info=can_change_info, + can_invite_users=can_invite_users, + can_pin_messages=can_pin_messages + ) + logger.warning( + 'Please pass `telebot.types.ChatPermissions` object to `permissions` parameter instead of ' + 'passing all boolean parameters. Those boolean parameters won\'t be supported soon, so please take it into consideration.' + ) return apihelper.restrict_chat_member( - self.token, chat_id, user_id, until_date, - 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, use_independent_chat_permissions) + self.token, chat_id, user_id, permissions, until_date, use_independent_chat_permissions) def promote_chat_member( self, chat_id: Union[int, str], user_id: int, diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 82e1269..2a95eb0 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -968,39 +968,19 @@ def unban_chat_member(token, chat_id, user_id, only_if_banned): def restrict_chat_member( - token, chat_id, user_id, until_date=None, - 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, use_independent_chat_permissions=None): + token, chat_id, user_id, permissions, until_date=None, + use_independent_chat_permissions=None): method_url = 'restrictChatMember' - permissions = {} - if can_send_messages is not None: - permissions['can_send_messages'] = can_send_messages - if can_send_media_messages is not None: - permissions['can_send_media_messages'] = can_send_media_messages - if can_send_polls is not None: - permissions['can_send_polls'] = can_send_polls - if can_send_other_messages is not None: - permissions['can_send_other_messages'] = can_send_other_messages - if can_add_web_page_previews is not None: - permissions['can_add_web_page_previews'] = can_add_web_page_previews - if can_change_info is not None: - permissions['can_change_info'] = can_change_info - if can_invite_users is not None: - permissions['can_invite_users'] = can_invite_users - if can_pin_messages is not None: - permissions['can_pin_messages'] = can_pin_messages + payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions.to_json()} + if use_independent_chat_permissions is not None: permissions['use_independent_chat_permissions'] = use_independent_chat_permissions - - permissions_json = json.dumps(permissions) - payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions_json} if until_date is not None: if isinstance(until_date, datetime): payload['until_date'] = until_date.timestamp() else: payload['until_date'] = until_date + return _make_request(token, method_url, params=payload, method='post') diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 67d7529..ebfaca5 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -3776,6 +3776,7 @@ class AsyncTeleBot: can_change_info: Optional[bool]=None, can_invite_users: Optional[bool]=None, can_pin_messages: Optional[bool]=None, + permissions: Optional[types.ChatPermissions]=None, use_independent_chat_permissions: Optional[bool]=None) -> bool: """ Use this method to restrict a user in a supergroup. @@ -3784,6 +3785,10 @@ class AsyncTeleBot: Telegram documentation: https://core.telegram.org/bots/api#restrictchatmember + .. warning:: + Please pass `telebot.types.ChatPermissions` object to `permissions` parameter instead of + passing all boolean parameters. Those boolean parameters won't be supported soon, so please take it into consideration. + :param chat_id: Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername) :type chat_id: :obj:`int` or :obj:`str` @@ -3830,15 +3835,30 @@ class AsyncTeleBot: can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission. :type use_independent_chat_permissions: :obj:`bool` + :param permissions: Pass ChatPermissions object to set all permissions at once. Use this parameter instead of + passing all boolean parameters to avoid backward compatibility problems in future. + :type permissions: :obj:`types.ChatPermissions` + :return: True on success :rtype: :obj:`bool` """ + if permissions is None: + permissions = types.ChatPermissions( + can_send_messages=can_send_messages, + can_send_media_messages=can_send_media_messages, + can_send_polls=can_send_polls, + can_send_other_messages=can_send_other_messages, + can_add_web_page_previews=can_add_web_page_previews, + can_change_info=can_change_info, + can_invite_users=can_invite_users, + can_pin_messages=can_pin_messages + ) + logger.warning( + 'Please pass `telebot.types.ChatPermissions` object to `permissions` parameter instead of ' + 'passing all boolean parameters. Those boolean parameters won\'t be supported soon, so please take it into consideration.' + ) return await asyncio_helper.restrict_chat_member( - self.token, chat_id, user_id, until_date, - 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, use_independent_chat_permissions) + self.token, chat_id, user_id, permissions, until_date, use_independent_chat_permissions) async def promote_chat_member( self, chat_id: Union[int, str], user_id: int, diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index ddad140..9fa7345 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -960,34 +960,13 @@ async def unban_chat_member(token, chat_id, user_id, only_if_banned): async def restrict_chat_member( - token, chat_id, user_id, until_date=None, - 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, use_independent_chat_permissions=None): + token, chat_id, user_id, permissions, until_date=None, + use_independent_chat_permissions=None): method_url = 'restrictChatMember' - permissions = {} - if can_send_messages is not None: - permissions['can_send_messages'] = can_send_messages - if can_send_media_messages is not None: - permissions['can_send_media_messages'] = can_send_media_messages - if can_send_polls is not None: - permissions['can_send_polls'] = can_send_polls - if can_send_other_messages is not None: - permissions['can_send_other_messages'] = can_send_other_messages - if can_add_web_page_previews is not None: - permissions['can_add_web_page_previews'] = can_add_web_page_previews - if can_change_info is not None: - permissions['can_change_info'] = can_change_info - if can_invite_users is not None: - permissions['can_invite_users'] = can_invite_users - if can_pin_messages is not None: - permissions['can_pin_messages'] = can_pin_messages + payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions.to_json()} + if use_independent_chat_permissions is not None: permissions['use_independent_chat_permissions'] = use_independent_chat_permissions - - permissions_json = json.dumps(permissions) - payload = {'chat_id': chat_id, 'user_id': user_id, 'permissions': permissions_json} if until_date is not None: if isinstance(until_date, datetime): payload['until_date'] = until_date.timestamp() diff --git a/telebot/types.py b/telebot/types.py index 082acf1..b53706e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -2418,6 +2418,8 @@ class KeyboardButton(Dictionaryable, JsonSerializable): json_dict['web_app'] = self.web_app.to_dict() if self.request_user is not None: json_dict['request_user'] = self.request_user.to_dict() + if self.request_chat is not None: + json_dict['request_chat'] = self.request_chat.to_dict() return json_dict @@ -3124,6 +3126,10 @@ class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable): value of can_pin_messages :type can_manage_topics: :obj:`bool` + :param can_send_media_messages: deprecated. True, if the user is allowed to send audios, documents, photos, videos, + video notes and voice notes + :type can_send_media_messages: :obj:`bool` + :return: Instance of the class :rtype: :class:`telebot.types.ChatPermissions` """ @@ -3133,7 +3139,7 @@ class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable): obj = cls.check_json(json_string, dict_copy=False) return cls(**obj) - def __init__(self, can_send_messages=None, can_send_audios=None, + def __init__(self, can_send_messages=None, can_send_media_messages=None,can_send_audios=None, can_send_documents=None, can_send_photos=None, can_send_videos=None, can_send_video_notes=None, can_send_voice_notes=None, can_send_polls=None, can_send_other_messages=None, @@ -3156,6 +3162,14 @@ class ChatPermissions(JsonDeserializable, JsonSerializable, Dictionaryable): self.can_send_video_notes: bool = can_send_video_notes self.can_send_voice_notes: bool = can_send_voice_notes + if can_send_media_messages is not None: + self.can_send_audios = can_send_media_messages + self.can_send_documents = can_send_media_messages + self.can_send_photos = can_send_media_messages + self.can_send_videos = can_send_media_messages + self.can_send_video_notes = can_send_media_messages + self.can_send_voice_notes = can_send_media_messages + def to_json(self): return json.dumps(self.to_dict())