From f527fc91f6b5e052b0ddf1a5ea6b48dfbb8542b3 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 11 Mar 2023 18:18:07 +0400 Subject: [PATCH] Replaced the parameters png_sticker, tgs_sticker, webm_sticker, emojis and mask_position in the method addStickerToSet with the parameter sticker of the type InputSticker. --- telebot/__init__.py | 19 ++++++++++-- telebot/apihelper.py | 20 +++--------- telebot/async_telebot.py | 15 +++++++-- telebot/asyncio_helper.py | 23 +++----------- telebot/types.py | 64 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 99 insertions(+), 42 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index f4df131..1fbc832 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -4653,12 +4653,14 @@ class TeleBot: self.token, user_id, name, title, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker, sticker_type, needs_repainting) + def add_sticker_to_set( - self, user_id: int, name: str, emojis: str, + self, user_id: int, name: str, emojis: List[str]=None, png_sticker: Optional[Union[Any, str]]=None, tgs_sticker: Optional[Union[Any, str]]=None, webm_sticker: Optional[Union[Any, str]]=None, - mask_position: Optional[types.MaskPosition]=None) -> bool: + mask_position: Optional[types.MaskPosition]=None, + sticker: Optional[List[types.InputSticker]]=None) -> bool: """ Use this method to add a new sticker to a set created by the bot. It's required to pass `png_sticker` or `tgs_sticker`. @@ -4666,6 +4668,9 @@ class TeleBot: Telegram documentation: https://core.telegram.org/bots/api#addstickertoset + .. note:: + **_sticker parameters are deprecated, use stickers instead + :param user_id: User identifier of created sticker set owner :type user_id: :obj:`int` @@ -4689,11 +4694,19 @@ class TeleBot: :param mask_position: A JSON-serialized object for position where the mask should be placed on faces :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` + :return: On success, True is returned. :rtype: :obj:`bool` """ + # 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) + return apihelper.add_sticker_to_set( - self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker) + self.token, user_id, name, sticker) def set_sticker_position_in_set(self, sticker: str, position: int) -> bool: """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 2bf5f9a..82d45aa 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1654,23 +1654,11 @@ def create_new_sticker_set( return _make_request(token, method_url, params=payload, files=files, method='post') -def add_sticker_to_set(token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker): +def add_sticker_to_set(token, user_id, name, sticker): method_url = 'addStickerToSet' - payload = {'user_id': user_id, 'name': name, 'emojis': emojis} - if png_sticker: - stype = 'png_sticker' - elif webm_sticker: - stype = 'webm_sticker' - else: - stype = 'tgs_sticker' - sticker = png_sticker or tgs_sticker or webm_sticker - files = None - if not util.is_string(sticker): - files = {stype: sticker} - else: - payload[stype] = sticker - if mask_position: - payload['mask_position'] = mask_position.to_json() + json_dict, files = sticker.convert_input_sticker() + payload = {'user_id': user_id, 'name': name, 'sticker': json_dict} + return _make_request(token, method_url, params=payload, files=files, method='post') diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index f4669e8..6bb8b9a 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -5518,11 +5518,12 @@ class AsyncTeleBot: async def add_sticker_to_set( - self, user_id: int, name: str, emojis: str, + self, user_id: int, name: str, emojis: List[str]=None, png_sticker: Optional[Union[Any, str]]=None, tgs_sticker: Optional[Union[Any, str]]=None, webm_sticker: Optional[Union[Any, str]]=None, - mask_position: Optional[types.MaskPosition]=None) -> bool: + mask_position: Optional[types.MaskPosition]=None, + sticker: Optional[List[types.InputSticker]]=None) -> bool: """ Use this method to add a new sticker to a set created by the bot. It's required to pass `png_sticker` or `tgs_sticker`. @@ -5553,11 +5554,19 @@ class AsyncTeleBot: :param mask_position: A JSON-serialized object for position where the mask should be placed on faces :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` + :return: On success, True is returned. :rtype: :obj:`bool` """ + # 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) + return await asyncio_helper.add_sticker_to_set( - self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker) + self.token, user_id, name, sticker) async def set_sticker_position_in_set(self, sticker: str, position: int) -> bool: diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 0f9919c..850ab5a 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1641,27 +1641,12 @@ async def create_new_sticker_set( return await _process_request(token, method_url, params=payload, files=files, method='post') -async def add_sticker_to_set(token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker): +async def add_sticker_to_set(token, user_id, name, sticker): method_url = 'addStickerToSet' - payload = {'user_id': user_id, 'name': name, 'emojis': emojis} - if png_sticker: - stype = 'png_sticker' - elif webm_sticker: - stype = 'webm_sticker' - else: - stype = 'tgs_sticker' - files = None - sticker = png_sticker or tgs_sticker or webm_sticker + json_dict, files = sticker.convert_input_sticker() + payload = {'user_id': user_id, 'name': name, 'sticker': json_dict} + - if not util.is_string(sticker): - files = {stype: sticker} - else: - payload[stype] = sticker - if mask_position: - payload['mask_position'] = mask_position.to_json() - - if webm_sticker: - payload['webm_sticker'] = webm_sticker return await _process_request(token, method_url, params=payload, files=files, method='post') diff --git a/telebot/types.py b/telebot/types.py index b7fca70..d789da1 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -7346,4 +7346,66 @@ class BotShortDescription(JsonDeserializable): return cls(**obj) def __init__(self, short_description: str) -> None: - self.short_description: str = short_description \ No newline at end of file + self.short_description: str = short_description + + +class InputSticker(Dictionaryable, JsonSerializable): + """ + This object describes a sticker to be added to a sticker set. + + :param sticker: The added sticker. Pass a file_id as a String to send a file that already exists on the Telegram servers, + pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. + Animated and video stickers can't be uploaded via HTTP URL. + :type sticker: :obj:`str` or :obj:`telebot.types.InputFile` + + :param emoji_list: One or more(up to 20) emoji(s) corresponding to the sticker + :type emoji_list: :obj:`list` of :obj:`str` + + :param mask_position: Optional. Position where the mask should be placed on faces. For “mask” stickers only. + :type mask_position: :class:`telebot.types.MaskPosition` + + :param keywords: Optional. List of 0-20 search keywords for the sticker with total length of up to 64 characters. + For “regular” and “custom_emoji” stickers only. + :type keywords: :obj:`list` of :obj:`str` + + :return: Instance of the class + :rtype: :class:`telebot.types.InputSticker` + """ + + def __init__(self, sticker: Union[str, InputFile], emoji_list: List[str], mask_position: Optional[MaskPosition]=None, keywords: Optional[List[str]]=None) -> None: + self.sticker: Union[str, InputFile] = sticker + self.emoji_list: List[str] = emoji_list + self.mask_position: Optional[MaskPosition] = mask_position + self.keywords: Optional[List[str]] = keywords + + if service_utils.is_string(self.sticker): + self._sticker_name = '' + self._sticker_dic = self.sticker + else: + self._sticker_name = service_utils.generate_random_token() + self._sticker_dic = 'attach://{0}'.format(self._sticker_name) + + def to_dict(self) -> dict: + json_dict = { + 'sticker': self._sticker_dic, + 'emojis': self.emoji_list + } + + if self.mask_position is not None: + json_dict['mask_position'] = self.mask_position.to_dict() + if self.keywords is not None: + json_dict['keywords'] = self.keywords + + return json_dict + + def to_json(self) -> str: + return json.dumps(self.to_dict()) + + def convert_input_sticker(self): + if service_utils.is_string(self.sticker): + return self.to_json(), None + + return self.to_json(), {self._sticker_name: self.sticker} + + + \ No newline at end of file