From 3d740ffd97364ec5fd81c3eb3bba47d356500f60 Mon Sep 17 00:00:00 2001 From: _run Date: Sun, 17 Nov 2024 18:18:37 +0400 Subject: [PATCH] Added the classes Gift and Gifts and the method getAvailableGifts, allowing bots to get all gifts available for sending. --- telebot/__init__.py | 12 ++++++++ telebot/apihelper.py | 5 ++++ telebot/async_telebot.py | 12 ++++++++ telebot/asyncio_helper.py | 4 +++ telebot/types.py | 63 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/telebot/__init__.py b/telebot/__init__.py index 586f44c9a..a274d2272 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -6249,7 +6249,19 @@ def delete_sticker_set(self, name:str) -> bool: :rtype: :obj:`bool` """ return apihelper.delete_sticker_set(self.token, name) + + def get_available_gifts(self) -> types.Gifts: + """ + Returns the list of gifts that can be sent by the bot to users. Requires no parameters. Returns a Gifts object. + + Telegram documentation: https://core.telegram.org/bots/api#getavailablegifts + :return: On success, a Gifts object is returned. + :rtype: :class:`telebot.types.Gifts` + """ + return types.Gifts.de_json( + apihelper.get_available_gifts(self.token) + ) def replace_sticker_in_set(self, user_id: int, name: str, old_sticker: str, sticker: types.InputSticker) -> bool: """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 01a1a77ba..83606f746 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1924,6 +1924,11 @@ def delete_sticker_set(token, name): return _make_request(token, method_url, params=payload, method='post') +def get_available_gifts(token): + method_url = 'getAvailableGifts' + return _make_request(token, method_url) + + def set_sticker_emoji_list(token, sticker, emoji_list): method_url = 'setStickerEmojiList' payload = {'sticker': sticker, 'emoji_list': json.dumps(emoji_list)} diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 659596f59..60719477d 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -7707,6 +7707,18 @@ async def delete_sticker_set(self, name:str) -> bool: return await asyncio_helper.delete_sticker_set(self.token, name) + async def get_available_gifts(self) -> types.Gifts: + """ + Returns the list of gifts that can be sent by the bot to users. Requires no parameters. Returns a Gifts object. + + Telegram documentation: https://core.telegram.org/bots/api#getavailablegifts + + :return: On success, a Gifts object is returned. + :rtype: :class:`telebot.types.Gifts` + """ + + return types.Gifts.de_json(await asyncio_helper.get_available_gifts(self.token)) + async def replace_sticker_in_set(self, user_id: int, name: str, old_sticker: str, sticker: types.InputSticker) -> bool: """ Use this method to replace an existing sticker in a sticker set with a new one. The method is equivalent to calling deleteStickerFromSet, then addStickerToSet, diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 4523e7173..47a1da1dc 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -1916,6 +1916,10 @@ async def delete_sticker_set(token, name): payload = {'name': name} return await _process_request(token, method_url, params=payload, method='post') +async def get_available_gifts(token): + method_url = 'getAvailableGifts' + return await _process_request(token, method_url) + async def set_custom_emoji_sticker_set_thumbnail(token, name, custom_emoji_id=None): method_url = 'setCustomEmojiStickerSetThumbnail' payload = {'name': name} diff --git a/telebot/types.py b/telebot/types.py index 904379dbc..c6386a61b 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -11025,3 +11025,66 @@ def de_json(cls, json_string): obj = cls.check_json(json_string) return cls(**obj) + +class Gift(JsonDeserializable): + """ + This object represents a gift that can be sent by the bot. + + Telegram documentation: https://core.telegram.org/bots/api#gift + + :param id: Unique identifier of the gift + :type id: :obj:`str` + + :param sticker: The sticker that represents the gift + :type sticker: :class:`Sticker` + + :param star_count: The number of Telegram Stars that must be paid to send the sticker + :type star_count: :obj:`int` + + :param total_count: Optional. The total number of the gifts of this type that can be sent; for limited gifts only + :type total_count: :obj:`int` + + :param remaining_count: Optional. The number of remaining gifts of this type that can be sent; for limited gifts only + :type remaining_count: :obj:`int` + + :return: Instance of the class + :rtype: :class:`Gift` + """ + + def __init__(self, id, sticker, star_count, total_count=None, remaining_count=None, **kwargs): + self.id: str = id + self.sticker: Sticker = sticker + self.star_count: int = star_count + self.total_count: Optional[int] = total_count + self.remaining_count: Optional[int] = remaining_count + + @classmethod + def de_json(cls, json_string): + if json_string is None: return None + obj = cls.check_json(json_string) + obj['sticker'] = Sticker.de_json(obj['sticker']) + return cls(**obj) + +class Gifts(JsonDeserializable): + """ + This object represent a list of gifts. + + Telegram documentation: https://core.telegram.org/bots/api#gifts + + :param gifts: The list of gifts + :type gifts: :obj:`list` of :class:`Gift` + + :return: Instance of the class + :rtype: :class:`Gifts` + """ + + def __init__(self, gifts, **kwargs): + self.gifts: List[Gift] = gifts + + @classmethod + def de_json(cls, json_string): + if json_string is None: return None + obj = cls.check_json(json_string) + obj['gifts'] = [Gift.de_json(gift) for gift in obj['gifts']] + return cls(**obj) + \ No newline at end of file