Skip to content

Commit

Permalink
Merge pull request #1432 from coder2020official/master
Browse files Browse the repository at this point in the history
Bot API 5.7
  • Loading branch information
Badiboy authored Feb 1, 2022
2 parents 723075d + 62fad9c commit 9fa79aa
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<p align="center">A simple, but extensible Python implementation for the <a href="https://core.telegram.org/bots/api">Telegram Bot API</a>.</p>
<p align="center">Supports both sync and async ways.</p>

## <p align="center">Supporting Bot API version: <a href="https://core.telegram.org/bots/api#december-30-2021">5.6</a>!
## <p align="center">Supporting Bot API version: <a href="https://core.telegram.org/bots/api#january-31-2022">5.7</a>!

## Contents

Expand Down Expand Up @@ -690,7 +690,8 @@ Result will be:


## API conformance


*[Bot API 5.7](https://core.telegram.org/bots/api#january-31-2022)
*[Bot API 5.6](https://core.telegram.org/bots/api#december-30-2021)
*[Bot API 5.5](https://core.telegram.org/bots/api#december-7-2021)
*[Bot API 5.4](https://core.telegram.org/bots/api#november-5-2021)
Expand Down
12 changes: 8 additions & 4 deletions telebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2409,8 +2409,9 @@ def upload_sticker_file(self, user_id: int, png_sticker: Union[Any, str]) -> typ
def create_new_sticker_set(
self, user_id: int, name: str, title: str,
emojis: str,
png_sticker: Union[Any, str],
tgs_sticker: Union[Any, str],
png_sticker: Union[Any, str]=None,
tgs_sticker: Union[Any, str]=None,
webm_sticker: Union[Any, str]=None,
contains_masks: Optional[bool]=None,
mask_position: Optional[types.MaskPosition]=None) -> bool:
"""
Expand All @@ -2423,18 +2424,20 @@ def create_new_sticker_set(
:param emojis:
:param png_sticker:
:param tgs_sticker:
:param webm_sticker:
:param contains_masks:
:param mask_position:
:return:
"""
return apihelper.create_new_sticker_set(
self.token, user_id, name, title, emojis, png_sticker, tgs_sticker,
contains_masks, mask_position)
contains_masks, mask_position, webm_sticker)

def add_sticker_to_set(
self, user_id: int, name: str, emojis: str,
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:
"""
Use this method to add a new sticker to a set created by the bot.
Expand All @@ -2445,11 +2448,12 @@ def add_sticker_to_set(
:param emojis:
:param png_sticker: Required if `tgs_sticker` is None
:param tgs_sticker: Required if `png_sticker` is None
:webm_sticker:
:param mask_position:
:return:
"""
return apihelper.add_sticker_to_set(
self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position)
self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker)

def set_sticker_position_in_set(self, sticker: str, position: int) -> bool:
"""
Expand Down
26 changes: 20 additions & 6 deletions telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,17 @@ def upload_sticker_file(token, user_id, png_sticker):

def create_new_sticker_set(
token, user_id, name, title, emojis, png_sticker, tgs_sticker,
contains_masks=None, mask_position=None):
contains_masks=None, mask_position=None, webm_sticker=None):
method_url = 'createNewStickerSet'
payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis}
stype = 'png_sticker' if png_sticker else 'tgs_sticker'
sticker = png_sticker or tgs_sticker
stype = None
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}
Expand All @@ -1547,14 +1553,22 @@ def create_new_sticker_set(
payload['contains_masks'] = contains_masks
if mask_position:
payload['mask_position'] = mask_position.to_json()
if webm_sticker:
payload['webm_sticker'] = webm_sticker
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):
def add_sticker_to_set(token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker):
method_url = 'addStickerToSet'
payload = {'user_id': user_id, 'name': name, 'emojis': emojis}
stype = 'png_sticker' if png_sticker else 'tgs_sticker'
sticker = png_sticker or tgs_sticker
stype = None
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}
Expand Down
12 changes: 8 additions & 4 deletions telebot/async_telebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2996,8 +2996,9 @@ async def upload_sticker_file(self, user_id: int, png_sticker: Union[Any, str])
async def create_new_sticker_set(
self, user_id: int, name: str, title: str,
emojis: str,
png_sticker: Union[Any, str],
tgs_sticker: Union[Any, str],
png_sticker: Union[Any, str]=None,
tgs_sticker: Union[Any, str]=None,
webm_sticker: Union[Any, str]=None,
contains_masks: Optional[bool]=None,
mask_position: Optional[types.MaskPosition]=None) -> bool:
"""
Expand All @@ -3010,19 +3011,21 @@ async def create_new_sticker_set(
:param emojis:
:param png_sticker:
:param tgs_sticker:
:webm_sticker:
:param contains_masks:
:param mask_position:
:return:
"""
return await asyncio_helper.create_new_sticker_set(
self.token, user_id, name, title, emojis, png_sticker, tgs_sticker,
contains_masks, mask_position)
contains_masks, mask_position, webm_sticker)


async def add_sticker_to_set(
self, user_id: int, name: str, emojis: str,
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:
"""
Use this method to add a new sticker to a set created by the bot.
Expand All @@ -3033,11 +3036,12 @@ async def add_sticker_to_set(
:param emojis:
:param png_sticker: Required if `tgs_sticker` is None
:param tgs_sticker: Required if `png_sticker` is None
:webm_sticker:
:param mask_position:
:return:
"""
return await asyncio_helper.add_sticker_to_set(
self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position)
self.token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker)


async def set_sticker_position_in_set(self, sticker: str, position: int) -> bool:
Expand Down
30 changes: 24 additions & 6 deletions telebot/asyncio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,11 +1497,17 @@ async def upload_sticker_file(token, user_id, png_sticker):

async def create_new_sticker_set(
token, user_id, name, title, emojis, png_sticker, tgs_sticker,
contains_masks=None, mask_position=None):
contains_masks=None, mask_position=None, webm_sticker=None):
method_url = 'createNewStickerSet'
payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis}
stype = 'png_sticker' if png_sticker else 'tgs_sticker'
sticker = png_sticker or tgs_sticker
stype = None
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}
Expand All @@ -1511,21 +1517,33 @@ async def create_new_sticker_set(
payload['contains_masks'] = contains_masks
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')


async def add_sticker_to_set(token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position):
async def add_sticker_to_set(token, user_id, name, emojis, png_sticker, tgs_sticker, mask_position, webm_sticker):
method_url = 'addStickerToSet'
payload = {'user_id': user_id, 'name': name, 'emojis': emojis}
stype = 'png_sticker' if png_sticker else 'tgs_sticker'
sticker = png_sticker or tgs_sticker
stype = None
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

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')


Expand Down
6 changes: 4 additions & 2 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2484,10 +2484,11 @@ def de_json(cls, json_string):
obj['thumb'] = None
return cls(**obj)

def __init__(self, name, title, is_animated, contains_masks, stickers, thumb=None, **kwargs):
def __init__(self, name, title, is_animated, is_video, contains_masks, stickers, thumb=None, **kwargs):
self.name: str = name
self.title: str = title
self.is_animated: bool = is_animated
self.is_video: bool = is_video
self.contains_masks: bool = contains_masks
self.stickers: List[Sticker] = stickers
self.thumb: PhotoSize = thumb
Expand All @@ -2507,12 +2508,13 @@ def de_json(cls, json_string):
return cls(**obj)

def __init__(self, file_id, file_unique_id, width, height, is_animated,
thumb=None, emoji=None, set_name=None, mask_position=None, file_size=None, **kwargs):
is_video, thumb=None, emoji=None, set_name=None, mask_position=None, file_size=None, **kwargs):
self.file_id: str = file_id
self.file_unique_id: str = file_unique_id
self.width: int = width
self.height: int = height
self.is_animated: bool = is_animated
self.is_video: bool = is_video
self.thumb: PhotoSize = thumb
self.emoji: str = emoji
self.set_name: str = set_name
Expand Down
4 changes: 2 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def test_json_Message_Audio():


def test_json_Message_Sticker():
json_string = r'{"message_id": 21552, "from": {"id": 590740002, "is_bot": false, "first_name": "⚜️ Ƥυrуα ⚜️", "username": "Purya", "language_code": "en"}, "chat": {"id": -1001309982000, "title": "123", "type": "supergroup"}, "date": 1594068909, "sticker": {"width": 368, "height": 368, "emoji": "🤖", "set_name": "ipuryapack", "is_animated": false, "thumb": {"file_id": "AAMCBAADHQJOFL7mAAJUMF8Dj62hpmDhpRAYvkc8CtIqipolAAJ8AAPA-8cF9yxjgjkLS97A0D4iXQARtQAHbQADHy4AAhoE", "file_unique_id": "AQADwNA-Il0AAx8uAAI", "file_size": 7776, "width": 60, "height": 60}, "file_id": "CAACAgQAAx0CThS-5gACVDBfA4-toaZg4aUQGL5HWerSKoqaJQACArADwPvHBfcsY4I5C3feGgQ", "file_unique_id": "AgADfAADsPvHWQ", "file_size": 14602}}'
json_string = r'{"message_id": 21552, "from": {"id": 590740002, "is_bot": false, "first_name": "⚜️ Ƥυrуα ⚜️", "username": "Purya", "language_code": "en"}, "chat": {"id": -1001309982000, "title": "123", "type": "supergroup"}, "date": 1594068909, "sticker": {"width": 368, "height": 368, "emoji": "🤖", "set_name": "ipuryapack", "is_animated": false, "is_video": true, "thumb": {"file_id": "AAMCBAADHQJOFL7mAAJUMF8Dj62hpmDhpRAYvkc8CtIqipolAAJ8AAPA-8cF9yxjgjkLS97A0D4iXQARtQAHbQADHy4AAhoE", "file_unique_id": "AQADwNA-Il0AAx8uAAI", "file_size": 7776, "width": 60, "height": 60}, "file_id": "CAACAgQAAx0CThS-5gACVDBfA4-toaZg4aUQGL5HWerSKoqaJQACArADwPvHBfcsY4I5C3feGgQ", "file_unique_id": "AgADfAADsPvHWQ", "file_size": 14602}}'
msg = types.Message.de_json(json_string)
assert msg.sticker.height == 368
assert msg.sticker.thumb.height == 60
assert msg.content_type == 'sticker'


def test_json_Message_Sticker_without_thumb():
json_string = r'{"message_id": 21552, "from": {"id": 590740002, "is_bot": false, "first_name": "⚜️ Ƥυrуα ⚜️", "username": "Purya", "language_code": "en"}, "chat": {"id": -1001309982000, "title": "123", "type": "supergroup"}, "date": 1594068909, "sticker": {"width": 368, "height": 368, "emoji": "🤖", "set_name": "ipuryapack", "is_animated": false, "file_id": "CAACAgQAAx0CThS-5gACVDBfA4-toaZg4aUQGL5HWerSKoqaJQACArADwPvHBfcsY4I5C3feGgQ", "file_unique_id": "AgADfAADsPvHWQ", "file_size": 14602}}'
json_string = r'{"message_id": 21552, "from": {"id": 590740002, "is_bot": false, "first_name": "⚜️ Ƥυrуα ⚜️", "username": "Purya", "language_code": "en"}, "chat": {"id": -1001309982000, "title": "123", "type": "supergroup"}, "date": 1594068909, "sticker": {"width": 368, "height": 368, "emoji": "🤖", "set_name": "ipuryapack", "is_animated": false, "is_video": true, "file_id": "CAACAgQAAx0CThS-5gACVDBfA4-toaZg4aUQGL5HWerSKoqaJQACArADwPvHBfcsY4I5C3feGgQ", "file_unique_id": "AgADfAADsPvHWQ", "file_size": 14602}}'
msg = types.Message.de_json(json_string)
assert msg.sticker.height == 368
assert msg.sticker.thumb is None
Expand Down

0 comments on commit 9fa79aa

Please sign in to comment.