Skip to content

Commit

Permalink
Added the method sendGift, allowing bots to send gifts to users.
Browse files Browse the repository at this point in the history
  • Loading branch information
coder2020official committed Nov 17, 2024
1 parent 3d740ff commit a2d2a86
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
26 changes: 26 additions & 0 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6249,6 +6249,32 @@ def delete_sticker_set(self, name:str) -> bool:
:rtype: :obj:`bool`
"""
return apihelper.delete_sticker_set(self.token, name)

def send_gift(self, user_id: int, gift_id: str, text: Optional[str]=None, text_parse_mode: Optional[str]=None, text_entities: Optional[List[types.MessageEntity]]=None) -> bool:
"""
Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success.
Telegram documentation: https://core.telegram.org/bots/api#sendgift
:param user_id: Unique identifier of the target user that will receive the gift
:type user_id: :obj:`int`
:param gift_id: Identifier of the gift
:type gift_id: :obj:`str`
:param text: Text that will be shown along with the gift; 0-255 characters
:type text: :obj:`str`
:param text_parse_mode: Mode for parsing entities in the text. See formatting options for more details. Entities other than “bold”, “italic”, “underline”, “strikethrough”, “spoiler”, and “custom_emoji” are ignored.
:type text_parse_mode: :obj:`str`
:param text_entities: A JSON-serialized list of special entities that appear in the gift text. It can be specified instead of text_parse_mode. Entities other than “bold”, “italic”, “underline”, “strikethrough”, “spoiler”, and “custom_emoji” are ignored.
:type text_entities: :obj:`list` of :obj:`types.MessageEntity`
:return: Returns True on success.
:rtype: :obj:`bool`
"""
return apihelper.send_gift(self.token, user_id, gift_id, text=text, text_parse_mode=text_parse_mode, text_entities=text_entities)

def get_available_gifts(self) -> types.Gifts:
"""
Expand Down
11 changes: 11 additions & 0 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,17 @@ def get_available_gifts(token):
return _make_request(token, method_url)


def send_gift(token, user_id, gift_id, text=None, text_parse_mode=None, text_entities=None):
method_url = 'sendGift'
payload = {'user_id': user_id, 'gift_id': gift_id}
if text:
payload['text'] = text
if text_parse_mode:
payload['text_parse_mode'] = text_parse_mode
if text_entities:
payload['text_entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(text_entities))
return _make_request(token, method_url, params=payload, method='post')

def set_sticker_emoji_list(token, sticker, emoji_list):
method_url = 'setStickerEmojiList'
payload = {'sticker': sticker, 'emoji_list': json.dumps(emoji_list)}
Expand Down
26 changes: 26 additions & 0 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7706,6 +7706,32 @@ async def delete_sticker_set(self, name:str) -> bool:
"""

return await asyncio_helper.delete_sticker_set(self.token, name)

async def send_gift(self, user_id: int, gift_id: str, text: Optional[str]=None, text_parse_mode: Optional[str]=None, text_entities: Optional[List[types.MessageEntity]]=None) -> bool:
"""
Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success.
Telegram documentation: https://core.telegram.org/bots/api#sendgift
:param user_id: Unique identifier of the target user that will receive the gift
:type user_id: :obj:`int`
:param gift_id: Identifier of the gift
:type gift_id: :obj:`str`
:param text: Text that will be shown along with the gift; 0-255 characters
:type text: :obj:`str`
:param text_parse_mode: Mode for parsing entities in the text. See formatting options for more details. Entities other than “bold”, “italic”, “underline”, “strikethrough”, “spoiler”, and “custom_emoji” are ignored.
:type text_parse_mode: :obj:`str`
:param text_entities: A JSON-serialized list of special entities that appear in the gift text. It can be specified instead of text_parse_mode. Entities other than “bold”, “italic”, “underline”, “strikethrough”, “spoiler”, and “custom_emoji” are ignored.
:type text_entities: :obj:`list` of :obj:`types.MessageEntity`
:return: Returns True on success.
:rtype: :obj:`bool`
"""
return await asyncio_helper.send_gift(self.token, user_id, gift_id, text, text_parse_mode, text_entities)

async def get_available_gifts(self) -> types.Gifts:
"""
Expand Down
11 changes: 11 additions & 0 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,17 @@ async def delete_sticker_set(token, name):
payload = {'name': name}
return await _process_request(token, method_url, params=payload, method='post')

async def send_gift(token, user_id, gift_id, text=None, text_parse_mode=None, text_entities=None):
method_url = 'sendGift'
payload = {'user_id': user_id, 'gift_id': gift_id}
if text:
payload['text'] = text
if text_parse_mode:
payload['text_parse_mode'] = text_parse_mode
if text_entities:
payload['text_entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(text_entities))
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)
Expand Down

0 comments on commit a2d2a86

Please sign in to comment.