From 1e450ebd155bfdf8a264e795e397b3619b9dd7e6 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sun, 19 Mar 2023 17:31:53 +0400 Subject: [PATCH] Bot API 6.6 review changes --- telebot/__init__.py | 45 ++++++++++++++++++++++++++++++++----- telebot/apihelper.py | 16 ++++++------- telebot/async_telebot.py | 47 +++++++++++++++++++++++++++++++++------ telebot/asyncio_helper.py | 16 ++++++------- telebot/types.py | 3 +++ 5 files changed, 96 insertions(+), 31 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index a79bbc2..0d36d7b 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -4559,7 +4559,29 @@ class TeleBot: """ return apihelper.set_sticker_set_thumb(self.token, name, user_id, thumb) - set_sticker_set_thumb = set_sticker_set_thumbnail + def set_sticker_set_thumb( + self, name: str, user_id: int, thumb: Union[Any, str]=None): + """ + Use this method to set the thumbnail of a sticker set. + Animated thumbnails can be set for animated sticker sets only. Returns True on success. + + Telegram documentation: https://core.telegram.org/bots/api#setstickersetthumb + + :param name: Sticker set name + :type name: :obj:`str` + + :param user_id: User identifier + :type user_id: :obj:`int` + + :param thumb: + :type thumb: :obj:`filelike object` + + :return: On success, True is returned. + :rtype: :obj:`bool` + """ + # deprecated + logger.warning('set_sticker_set_thumb is deprecated. Use set_sticker_set_thumbnail instead.') + return apihelper.set_sticker_set_thumb(self.token, name, user_id, thumb) def get_sticker_set(self, name: str) -> types.StickerSet: """ @@ -4817,7 +4839,7 @@ class TeleBot: def add_sticker_to_set( - self, user_id: int, name: str, emojis: List[str]=None, + self, user_id: int, name: str, emojis: Union[List[str], str], png_sticker: Optional[Union[Any, str]]=None, tgs_sticker: Optional[Union[Any, str]]=None, webm_sticker: Optional[Union[Any, str]]=None, @@ -4833,7 +4855,7 @@ class TeleBot: Telegram documentation: https://core.telegram.org/bots/api#addstickertoset .. note:: - **_sticker parameters are deprecated, use stickers instead + **_sticker, mask_position, emojis parameters are deprecated, use stickers instead :param user_id: User identifier of created sticker set owner :type user_id: :obj:`int` @@ -4859,15 +4881,26 @@ class TeleBot: :type mask_position: :class:`telebot.types.MaskPosition` :param sticker: A JSON-serialized list of 1-50 initial stickers to be added to the sticker set - :type sticker: :class:`telebot.types.InputSticker` + :type sticker: :obj:`list` of :class:`telebot.types.InputSticker` :return: On success, True is returned. :rtype: :obj:`bool` """ + + # split emojis if string + if isinstance(emojis, str): + emojis = list(emojis) # Replaced the parameters png_sticker, tgs_sticker, webm_sticker, emojis and mask_position if sticker is None: - sticker = png_sticker or tgs_sticker or webm_sticker - sticker = types.InputSticker(sticker, emojis, mask_position) + old_sticker = png_sticker or tgs_sticker or webm_sticker + if old_sticker is not None: + logger.warning( + 'The parameters "png_sticker", "tgs_sticker", "webm_sticker", "emojis" and "mask_position" are deprecated, ' + 'use "sticker" instead' + ) + if not old_sticker: + raise ValueError('You must pass at least one sticker.') + sticker = types.InputSticker(old_sticker, emojis, mask_position) return apihelper.add_sticker_to_set( self.token, user_id, name, sticker) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 0a30fcb..6d59b84 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1157,9 +1157,9 @@ def set_chat_title(token, chat_id, title): def set_my_description(token, description=None, language_code=None): method_url = r'setMyDescription' payload = {} - if description: + if description is not None: payload['description'] = description - if language_code: + if language_code is not None: payload['language_code'] = language_code return _make_request(token, method_url, params=payload, method='post') @@ -1174,9 +1174,9 @@ def get_my_description(token, language_code=None): def set_my_short_description(token, short_description=None, language_code=None): method_url = r'setMyShortDescription' payload = {} - if short_description: + if short_description is not None: payload['short_description'] = short_description - if language_code: + if language_code is not None: payload['language_code'] = language_code return _make_request(token, method_url, params=payload, method='post') @@ -1646,7 +1646,7 @@ def upload_sticker_file(token, user_id, sticker, sticker_format): def set_custom_emoji_sticker_set_thumbnail(token, name, custom_emoji_id=None): method_url = 'setCustomEmojiStickerSetThumbnail' payload = {'name': name} - if custom_emoji_id: + if custom_emoji_id is not None: payload['custom_emoji_id'] = custom_emoji_id return _make_request(token, method_url, params=payload, method='post') @@ -1667,15 +1667,13 @@ def set_sticker_emoji_list(token, sticker, emoji_list): return _make_request(token, method_url, params=payload, method='post') def create_new_sticker_set( - token, user_id, name, title, stickers, sticker_format=None, sticker_type=None, needs_repainting=None): + token, user_id, name, title, stickers, sticker_format, sticker_type=None, needs_repainting=None): method_url = 'createNewStickerSet' - payload = {'user_id': user_id, 'name': name, 'title': title} + payload = {'user_id': user_id, 'name': name, 'title': title, 'sticker_format': sticker_format} if sticker_type: payload['sticker_type'] = sticker_type if needs_repainting: payload['needs_repainting'] = needs_repainting - if sticker_format: - payload['sticker_format'] = sticker_format files = {} lst = [] diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 63f2bdd..f13d1a0 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -5423,7 +5423,29 @@ class AsyncTeleBot: """ return await asyncio_helper.set_sticker_set_thumb(self.token, name, user_id, thumb) - set_sticker_set_thumb = set_sticker_set_thumbnail + async def set_sticker_set_thumb( + self, name: str, user_id: int, thumb: Union[Any, str]=None): + """ + Use this method to set the thumbnail of a sticker set. + Animated thumbnails can be set for animated sticker sets only. Returns True on success. + + Telegram documentation: https://core.telegram.org/bots/api#setstickersetthumb + + :param name: Sticker set name + :type name: :obj:`str` + + :param user_id: User identifier + :type user_id: :obj:`int` + + :param thumb: + :type thumb: :obj:`filelike object` + + :return: On success, True is returned. + :rtype: :obj:`bool` + """ + # deprecated + logger.warning('set_sticker_set_thumb is deprecated, use set_sticker_set_thumbnail instead') + return await asyncio_helper.set_sticker_set_thumb(self.token, name, user_id, thumb) async def get_sticker_set(self, name: str) -> types.StickerSet: """ @@ -5681,7 +5703,7 @@ class AsyncTeleBot: async def add_sticker_to_set( - self, user_id: int, name: str, emojis: List[str]=None, + self, user_id: int, name: str, emojis: Union[List[str], str]=None, png_sticker: Optional[Union[Any, str]]=None, tgs_sticker: Optional[Union[Any, str]]=None, webm_sticker: Optional[Union[Any, str]]=None, @@ -5694,6 +5716,9 @@ class AsyncTeleBot: Static sticker sets can have up to 120 stickers. Returns True on success. + .. note:: + **_sticker, mask_position, emojis parameters are deprecated, use stickers instead + Telegram documentation: https://core.telegram.org/bots/api#addstickertoset :param user_id: User identifier of created sticker set owner @@ -5720,17 +5745,25 @@ class AsyncTeleBot: :type mask_position: :class:`telebot.types.MaskPosition` :param sticker: A JSON-serialized list of 1-50 initial stickers to be added to the sticker set - :type sticker: :class:`telebot.types.InputSticker` + :type sticker: :obj:`list` of :class:`telebot.types.InputSticker` :return: On success, True is returned. :rtype: :obj:`bool` """ + # split emojis if string + if isinstance(emojis, str): + emojis = list(emojis) # Replaced the parameters png_sticker, tgs_sticker, webm_sticker, emojis and mask_position if sticker is None: - sticker = png_sticker or tgs_sticker or webm_sticker - if sticker is None: - raise ValueError('You must pass at least one sticker') - sticker = types.InputSticker(sticker, emojis, mask_position) + old_sticker = png_sticker or tgs_sticker or webm_sticker + if old_sticker is not None: + logger.warning( + 'Parameters "png_sticker", "tgs_sticker", "webm_sticker", "emojis" and "mask_position" are deprecated, ' + 'use "sticker" instead' + ) + if not old_sticker: + raise ValueError('You must pass at least one sticker.') + sticker = types.InputSticker(old_sticker, emojis, mask_position) return await asyncio_helper.add_sticker_to_set( self.token, user_id, name, sticker) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index e00410a..0494f27 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1143,9 +1143,9 @@ async def set_chat_title(token, chat_id, title): async def set_my_description(token, description=None, language_code=None): method_url = r'setMyDescription' payload = {} - if description: + if description is not None: payload['description'] = description - if language_code: + if language_code is not None: payload['language_code'] = language_code return await _process_request(token, method_url, params=payload, method='post') @@ -1159,9 +1159,9 @@ async def get_my_description(token, language_code=None): async def set_my_short_description(token, short_description=None, language_code=None): method_url = r'setMyShortDescription' payload = {} - if short_description: + if short_description is not None: payload['short_description'] = short_description - if language_code: + if language_code is not None: payload['language_code'] = language_code return await _process_request(token, method_url, params=payload, method='post') @@ -1642,7 +1642,7 @@ async def delete_sticker_set(token, name): async def set_custom_emoji_sticker_set_thumbnail(token, name, custom_emoji_id=None): method_url = 'setCustomEmojiStickerSetThumbnail' payload = {'name': name} - if custom_emoji_id: + if custom_emoji_id is not None: payload['custom_emoji_id'] = custom_emoji_id return await _process_request(token, method_url, params=payload, method='post') @@ -1653,15 +1653,13 @@ async def set_sticker_set_title(token, name, title): return await _process_request(token, method_url, params=payload, method='post') async def create_new_sticker_set( - token, user_id, name, title, stickers, sticker_format=None, sticker_type=None, needs_repainting=None): + token, user_id, name, title, stickers, sticker_format, sticker_type=None, needs_repainting=None): method_url = 'createNewStickerSet' - payload = {'user_id': user_id, 'name': name, 'title': title} + payload = {'user_id': user_id, 'name': name, 'title': title, 'sticker_format': sticker_format} if sticker_type: payload['sticker_type'] = sticker_type if needs_repainting: payload['needs_repainting'] = needs_repainting - if sticker_format: - payload['sticker_format'] = sticker_format files = {} lst = [] diff --git a/telebot/types.py b/telebot/types.py index cd7d00f..96d526e 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -7436,7 +7436,10 @@ class InputSticker(Dictionaryable, JsonSerializable): self._sticker_name = '' self._sticker_dic = self.sticker else: + # work like in inputmedia: convert_input_media self._sticker_name = service_utils.generate_random_token() + # uses attach://_sticker_name for sticker param. then, + # actual file is sent using files param of the request self._sticker_dic = 'attach://{0}'.format(self._sticker_name) def to_dict(self) -> dict: