Skip to content

Commit af70313

Browse files
committed
New method for v3.2
1 parent aefd666 commit af70313

3 files changed

Lines changed: 195 additions & 10 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def polling(self, none_stop=False, interval=0, timeout=20):
255255
Warning: Do not call this function more than once!
256256
257257
Always get updates.
258+
:param interval:
258259
:param none_stop: Do not stop polling when an ApiException occurs.
259260
:param timeout: Timeout in seconds for long polling.
260261
:return:
@@ -462,6 +463,7 @@ def send_photo(self, chat_id, photo, caption=None, reply_to_message_id=None, rep
462463
disable_notification=None):
463464
"""
464465
Use this method to send photos.
466+
:param disable_notification:
465467
:param chat_id:
466468
:param photo:
467469
:param caption:
@@ -871,6 +873,86 @@ def answer_callback_query(self, callback_query_id, text=None, show_alert=None, u
871873
"""
872874
return apihelper.answer_callback_query(self.token, callback_query_id, text, show_alert, url, cache_time)
873875

876+
def send_sticker(self, chat_id, sticker, disable_notification=None, reply_to_message_id=None, reply_markup=None):
877+
"""
878+
Use this method to send .webp stickers. On success, the sent Message is returned.
879+
:param chat_id:
880+
:param sticker:
881+
:param disable_notification:
882+
:param reply_to_message_id:
883+
:param reply_markup:
884+
:return:
885+
"""
886+
result = apihelper.send_sticker(self.token, chat_id, sticker, disable_notification, reply_markup, reply_markup)
887+
return types.Message.de_json(result)
888+
889+
def get_sticker_set(self, name):
890+
"""
891+
Use this method to get a sticker set. On success, a StickerSet object is returned.
892+
:param token:
893+
:param name:
894+
:return:
895+
"""
896+
result = apihelper.get_sticker_set(self.token, name)
897+
return types.StickerSet.de_json(result)
898+
899+
def upload_sticker_file(self, user_id, png_sticker):
900+
"""
901+
Use this method to upload a .png file with a sticker for later use in createNewStickerSet and addStickerToSet
902+
methods (can be used multiple times). Returns the uploaded File on success.
903+
:param user_id:
904+
:param png_sticker:
905+
:return:
906+
"""
907+
result = apihelper.upload_sticker_file(self.token, user_id, png_sticker)
908+
return types.File.de_json(result)
909+
910+
def create_new_sticker_set(self, user_id, name, title, png_sticker, emojis, contains_masks=None,
911+
mask_position=None):
912+
"""
913+
Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set.
914+
Returns True on success.
915+
:param user_id:
916+
:param name:
917+
:param title:
918+
:param png_sticker:
919+
:param emojis:
920+
:param contains_masks:
921+
:param mask_position:
922+
:return:
923+
"""
924+
return apihelper.create_new_sticker_set(self.token, user_id, name, title, png_sticker, emojis, contains_masks,
925+
mask_position)
926+
927+
def add_sticker_to_set(self, user_id, name, png_sticker, emojis, mask_position):
928+
"""
929+
Use this method to add a new sticker to a set created by the bot. Returns True on success.
930+
:param user_id:
931+
:param name:
932+
:param png_sticker:
933+
:param emojis:
934+
:param mask_position:
935+
:return:
936+
"""
937+
return apihelper.add_sticker_to_set(self.token, user_id, name, png_sticker, emojis, mask_position)
938+
939+
def set_sticker_position_in_set(self, sticker, position):
940+
"""
941+
Use this method to move a sticker in a set created by the bot to a specific position . Returns True on success.
942+
:param sticker:
943+
:param position:
944+
:return:
945+
"""
946+
return apihelper.set_sticker_position_in_set(self.token, sticker, position)
947+
948+
def delete_sticker_from_set(self, sticker):
949+
"""
950+
Use this method to delete a sticker from a set created by the bot. Returns True on success.
951+
:param sticker:
952+
:return:
953+
"""
954+
return apihelper.delete_sticker_from_set(self.token, sticker)
955+
874956
def register_for_reply(self, message, callback):
875957
"""
876958
Registers a callback function to be notified when a reply to `message` arrives.
@@ -1301,3 +1383,31 @@ def answer_inline_query(self, *args, **kwargs):
13011383
@util.async()
13021384
def answer_callback_query(self, *args, **kwargs):
13031385
return TeleBot.answer_callback_query(self, *args, **kwargs)
1386+
1387+
@util.async()
1388+
def send_sticker(self, *args, **kwargs):
1389+
return TeleBot.send_sticker(self, *args, **kwargs)
1390+
1391+
@util.async()
1392+
def get_sticker_set(self, *args, **kwargs):
1393+
return TeleBot.get_sticker_set(self, *args, **kwargs)
1394+
1395+
@util.async()
1396+
def upload_sticker_file(self, *args, **kwargs):
1397+
return TeleBot.upload_sticker_file(self, *args, **kwargs)
1398+
1399+
@util.async()
1400+
def create_new_sticker_set(self, *args, **kwargs):
1401+
return TeleBot.create_new_sticker_set(self, *args, **kwargs)
1402+
1403+
@util.async()
1404+
def add_sticker_to_set(self, *args, **kwargs):
1405+
return TeleBot.add_sticker_to_set(self, *args, **kwargs)
1406+
1407+
@util.async()
1408+
def set_sticker_position_in_set(self, *args, **kwargs):
1409+
return TeleBot.set_sticker_position_in_set(self, *args, **kwargs)
1410+
1411+
@util.async()
1412+
def delete_sticker_from_set(self, *args, **kwargs):
1413+
return TeleBot.delete_sticker_from_set(self, *args, **kwargs)

‎telebot/apihelper.py‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _make_request(token, method_name, method='get', params=None, files=None, bas
5151
if 'timeout' in params: read_timeout = params['timeout'] + 10
5252
if 'connect-timeout' in params: connect_timeout = params['connect-timeout'] + 10
5353
result = _get_req_session().request(method, request_url, params=params, files=files,
54-
timeout=(connect_timeout, read_timeout), proxies=proxy)
54+
timeout=(connect_timeout, read_timeout), proxies=proxy)
5555
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
5656
return _check_result(method_name, result)['result']
5757

@@ -791,6 +791,72 @@ def answer_inline_query(token, inline_query_id, results, cache_time=None, is_per
791791
return _make_request(token, method_url, params=payload, method='post')
792792

793793

794+
def send_sticker(token, chat_id, sticker, disable_notification=None, reply_to_message_id=None, reply_markup=None):
795+
method_url = 'sendSticker'
796+
payload = {'chat_id': chat_id}
797+
if not util.is_string(sticker):
798+
files = {'sticker': sticker}
799+
else:
800+
payload['sticker'] = sticker
801+
if disable_notification:
802+
payload['disable_notification'] = disable_notification
803+
if reply_to_message_id:
804+
payload['reply_to_message_id'] = reply_to_message_id
805+
if reply_markup:
806+
payload['reply_markup'] = _convert_markup(reply_markup)
807+
return _make_request(token, method_url, params=payload, files=files, method='post')
808+
809+
810+
def get_sticker_set(token, name):
811+
method_url = 'getStickerSet'
812+
return _make_request(token, method_url, params={'name': name})
813+
814+
815+
def upload_sticker_file(token, user_id, png_sticker):
816+
method_url = 'uploadStickerFile'
817+
payload = {'user_id': user_id}
818+
files = {'png_sticker', png_sticker}
819+
return _make_request(token, method_url, params=payload, files=files, method='post')
820+
821+
822+
def create_new_sticker_set(token, user_id, name, title, png_sticker, emojis, contains_masks=None, mask_position=None):
823+
method_url = 'createNewStickerSet'
824+
payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis}
825+
if not util.is_string(png_sticker):
826+
files = {'png_sticker': png_sticker}
827+
else:
828+
payload['png_sticker'] = png_sticker
829+
if contains_masks:
830+
payload['contains_masks'] = contains_masks
831+
if mask_position:
832+
payload['mask_position'] = mask_position.to_json()
833+
return _make_request(token, method_url, params=payload, files=files, method='post')
834+
835+
836+
def add_sticker_to_set(token, user_id, name, png_sticker, emojis, mask_position):
837+
method_url = 'addStickerToSet'
838+
payload = {'user_id': user_id, 'name': name, 'emojis': emojis}
839+
if not util.is_string(png_sticker):
840+
files = {'png_sticker': png_sticker}
841+
else:
842+
payload['png_sticker'] = png_sticker
843+
if mask_position:
844+
payload['mask_position'] = mask_position.to_json()
845+
return _make_request(token, method_url, params=payload, files=files, method='post')
846+
847+
848+
def set_sticker_position_in_set(token, sticker, position):
849+
method_url = 'setStickerPositionInSet'
850+
payload = {'sticker': sticker, 'position': position}
851+
return _make_request(token, method_url, params=payload, method='post')
852+
853+
854+
def delete_sticker_from_set(token, sticker):
855+
method_url = 'deleteStickerFromSet'
856+
payload = {'sticker': sticker}
857+
return _make_request(token, method_url, params=payload, method='post')
858+
859+
794860
def _convert_list_json_serializable(results):
795861
ret = ''
796862
for r in results:

‎telebot/types.py‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ def __init__(self, user, status, until_date, can_be_edited, can_change_info, can
921921
self.can_send_other_messages = can_send_other_messages
922922
self.can_add_web_page_previews = can_add_web_page_previews
923923

924+
924925
# InlineQuery
925926

926927
class InlineQuery(JsonDeserializable):
@@ -1860,6 +1861,7 @@ def __init__(self, id, from_user, currency, total_amount, invoice_payload, shipp
18601861
self.shipping_option_id = shipping_option_id
18611862
self.order_info = order_info
18621863

1864+
18631865
# Stickers
18641866

18651867
class StickerSet(JsonDeserializable):
@@ -1869,19 +1871,18 @@ def de_json(cls, json_string):
18691871
name = obj['name']
18701872
title = obj['title']
18711873
contains_masks = obj['contains_masks']
1872-
stickers=[]
1873-
for s in obj['stickers']
1874+
stickers = []
1875+
for s in obj['stickers']:
18741876
stickers.append(Sticker.de_json(s))
18751877
return cls(name, title, contains_masks, stickers)
18761878

1877-
def __init__(self, name, title, contains_masks,stickers):
1879+
def __init__(self, name, title, contains_masks, stickers):
18781880
self.stickers = stickers
18791881
self.contains_masks = contains_masks
18801882
self.title = title
18811883
self.name = name
18821884

18831885

1884-
18851886
class Sticker(JsonDeserializable):
18861887
@classmethod
18871888
def de_json(cls, json_string):
@@ -1898,19 +1899,20 @@ def de_json(cls, json_string):
18981899
if 'mask_position' in obj:
18991900
mask_position = MaskPosition.de_json(obj['mask_position'])
19001901
file_size = obj.get('file_size')
1901-
return cls(file_id, width, height, thumb,emoji, set_name, mask_position, file_size)
1902+
return cls(file_id, width, height, thumb, emoji, set_name, mask_position, file_size)
19021903

1903-
def __init__(self,file_id,width,height,thumb,emoji,set_name,mask_position,file_size):
1904+
def __init__(self, file_id, width, height, thumb, emoji, set_name, mask_position, file_size):
19041905
self.file_id = file_id
19051906
self.width = width
19061907
self.height = height
1907-
self.thumb=thumb
1908+
self.thumb = thumb
19081909
self.emoji = emoji
19091910
self.set_name = set_name
19101911
self.mask_position = mask_position
1911-
self.file_size=file_size
1912+
self.file_size = file_size
1913+
19121914

1913-
class MaskPosition(JsonDeserializable):
1915+
class MaskPosition(JsonDeserializable, JsonSerializable):
19141916
@classmethod
19151917
def de_json(cls, json_string):
19161918
obj = cls.check_json(json_string)
@@ -1925,3 +1927,10 @@ def __init__(self, point, x_shift, y_shift, scale):
19251927
self.x_shift = x_shift
19261928
self.y_shift = y_shift
19271929
self.scale = scale
1930+
1931+
def to_json(self):
1932+
return json.dumps(self.to_dic())
1933+
1934+
def to_dic(self):
1935+
return {'point': self.point, 'x_shift': self.x_shift, 'y_shift': self.y_shift, 'scale': self.scale}
1936+

0 commit comments

Comments
 (0)