Skip to content

Commit a2d2a86

Browse files
Added the method sendGift, allowing bots to send gifts to users.
1 parent 3d740ff commit a2d2a86

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

telebot/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6249,6 +6249,32 @@ def delete_sticker_set(self, name:str) -> bool:
62496249
:rtype: :obj:`bool`
62506250
"""
62516251
return apihelper.delete_sticker_set(self.token, name)
6252+
6253+
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:
6254+
"""
6255+
Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success.
6256+
6257+
Telegram documentation: https://core.telegram.org/bots/api#sendgift
6258+
6259+
:param user_id: Unique identifier of the target user that will receive the gift
6260+
:type user_id: :obj:`int`
6261+
6262+
:param gift_id: Identifier of the gift
6263+
:type gift_id: :obj:`str`
6264+
6265+
:param text: Text that will be shown along with the gift; 0-255 characters
6266+
:type text: :obj:`str`
6267+
6268+
: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.
6269+
:type text_parse_mode: :obj:`str`
6270+
6271+
: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.
6272+
:type text_entities: :obj:`list` of :obj:`types.MessageEntity`
6273+
6274+
:return: Returns True on success.
6275+
:rtype: :obj:`bool`
6276+
"""
6277+
return apihelper.send_gift(self.token, user_id, gift_id, text=text, text_parse_mode=text_parse_mode, text_entities=text_entities)
62526278

62536279
def get_available_gifts(self) -> types.Gifts:
62546280
"""

telebot/apihelper.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,17 @@ def get_available_gifts(token):
19291929
return _make_request(token, method_url)
19301930

19311931

1932+
def send_gift(token, user_id, gift_id, text=None, text_parse_mode=None, text_entities=None):
1933+
method_url = 'sendGift'
1934+
payload = {'user_id': user_id, 'gift_id': gift_id}
1935+
if text:
1936+
payload['text'] = text
1937+
if text_parse_mode:
1938+
payload['text_parse_mode'] = text_parse_mode
1939+
if text_entities:
1940+
payload['text_entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(text_entities))
1941+
return _make_request(token, method_url, params=payload, method='post')
1942+
19321943
def set_sticker_emoji_list(token, sticker, emoji_list):
19331944
method_url = 'setStickerEmojiList'
19341945
payload = {'sticker': sticker, 'emoji_list': json.dumps(emoji_list)}

telebot/async_telebot.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7706,6 +7706,32 @@ async def delete_sticker_set(self, name:str) -> bool:
77067706
"""
77077707

77087708
return await asyncio_helper.delete_sticker_set(self.token, name)
7709+
7710+
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:
7711+
"""
7712+
Sends a gift to the given user. The gift can't be converted to Telegram Stars by the user. Returns True on success.
7713+
7714+
Telegram documentation: https://core.telegram.org/bots/api#sendgift
7715+
7716+
:param user_id: Unique identifier of the target user that will receive the gift
7717+
:type user_id: :obj:`int`
7718+
7719+
:param gift_id: Identifier of the gift
7720+
:type gift_id: :obj:`str`
7721+
7722+
:param text: Text that will be shown along with the gift; 0-255 characters
7723+
:type text: :obj:`str`
7724+
7725+
: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.
7726+
:type text_parse_mode: :obj:`str`
7727+
7728+
: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.
7729+
:type text_entities: :obj:`list` of :obj:`types.MessageEntity`
7730+
7731+
:return: Returns True on success.
7732+
:rtype: :obj:`bool`
7733+
"""
7734+
return await asyncio_helper.send_gift(self.token, user_id, gift_id, text, text_parse_mode, text_entities)
77097735

77107736
async def get_available_gifts(self) -> types.Gifts:
77117737
"""

telebot/asyncio_helper.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,17 @@ async def delete_sticker_set(token, name):
19161916
payload = {'name': name}
19171917
return await _process_request(token, method_url, params=payload, method='post')
19181918

1919+
async def send_gift(token, user_id, gift_id, text=None, text_parse_mode=None, text_entities=None):
1920+
method_url = 'sendGift'
1921+
payload = {'user_id': user_id, 'gift_id': gift_id}
1922+
if text:
1923+
payload['text'] = text
1924+
if text_parse_mode:
1925+
payload['text_parse_mode'] = text_parse_mode
1926+
if text_entities:
1927+
payload['text_entities'] = json.dumps(types.MessageEntity.to_list_of_dicts(text_entities))
1928+
return await _process_request(token, method_url, params=payload, method='post')
1929+
19191930
async def get_available_gifts(token):
19201931
method_url = 'getAvailableGifts'
19211932
return await _process_request(token, method_url)

0 commit comments

Comments
 (0)