-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Bot api 3.2 #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Bot api 3.2 #732
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9801772
Add set_name and mask_position to Sticker
jsmnbom d4ee12c
Add new sticker types; StickerSet and MaskPosition
jsmnbom bb863dd
Add new sticker types to init
jsmnbom 792546d
Add new sticker methods to bot
jsmnbom acd0dc8
StickerSet.is_masks should be plural
jsmnbom 763abad
Allow png_stickers to be sent as files
jsmnbom 9262c62
is_masks is called contains_masks by telegrams servers!
jsmnbom 79db6d1
Add tests for new sticker stuff
jsmnbom ac0fd6a
Add to_dict to StickerSet
jsmnbom 073fb4f
Update new sticker stuff as per reviews
jsmnbom 247ba69
Added docstring
Eldinnie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
telegram.sticker module | ||
======================= | ||
|
||
.. automodule:: telegram.sticker | ||
.. automodule:: telegram.files.sticker | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ | |
from datetime import datetime | ||
|
||
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File, | ||
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore) | ||
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore, StickerSet) | ||
from telegram.error import InvalidToken, TelegramError | ||
from telegram.utils.helpers import to_timestamp | ||
from telegram.utils.request import Request | ||
|
@@ -2369,6 +2369,218 @@ def unpin_chat_message(self, chat_id, timeout=None, **kwargs): | |
|
||
return result | ||
|
||
def get_sticker_set(self, name, timeout=None, **kwargs): | ||
""" | ||
Use this method to get a sticker set. | ||
|
||
Args: | ||
name (:obj:`str`): Short name of the sticker set that is used in t.me/addstickers/ | ||
URLs (e.g., animals) | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:class:`telegram.StickerSet` | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
|
||
url = '{0}/getStickerSet'.format(self.base_url) | ||
|
||
data = {'name': name} | ||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return StickerSet.de_json(result, self) | ||
|
||
def upload_sticker_file(self, user_id, png_sticker, timeout=None, **kwargs): | ||
""" | ||
Use this method to upload a .png file with a sticker for later use in | ||
:attr:`create_new_sticker_set` and :attr:`add_sticker_to_set` methods (can be used multiple | ||
times). | ||
|
||
Note: | ||
The png_sticker argument can be either a file_id, an URL or a file from disk | ||
``open(filename, 'rb')`` | ||
|
||
Args: | ||
user_id (:obj:`int`): User identifier of sticker file owner. | ||
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker, | ||
must be up to 512 kilobytes in size, dimensions must not exceed 512px, | ||
and either width or height must be exactly 512px. | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:class:`telegram.File`: The uploaded File | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
|
||
url = '{0}/uploadStickerFile'.format(self.base_url) | ||
|
||
data = {'user_id': user_id, 'png_sticker': png_sticker} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same note about |
||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return File.de_json(result, self) | ||
|
||
def create_new_sticker_set(self, user_id, name, title, png_sticker, emojis, is_masks=None, | ||
mask_position=None, timeout=None, **kwargs): | ||
""" | ||
Use this method to create new sticker set owned by a user. | ||
The bot will be able to edit the created sticker set. | ||
|
||
Note: | ||
The png_sticker argument can be either a file_id, an URL or a file from disk | ||
``open(filename, 'rb')`` | ||
|
||
Args: | ||
user_id (:obj:`int`): User identifier of created sticker set owner. | ||
name (:obj:`str`): Short name of sticker set, to be used in t.me/addstickers/ URLs | ||
(e.g., animals). Can contain only english letters, digits and underscores. | ||
Must begin with a letter, can't contain consecutive underscores and | ||
must end in "_by_<bot username>". <bot_username> is case insensitive. | ||
1-64 characters. | ||
title (:obj:`str`): Sticker set title, 1-64 characters. | ||
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker, must be up | ||
to 512 kilobytes in size, dimensions must not exceed 512px, | ||
and either width or height must be exactly 512px. Pass a file_id as a String to | ||
send a file that already exists on the Telegram servers, pass an HTTP URL as a | ||
String for Telegram to get a file from the Internet, or upload a new one | ||
using multipart/form-data. | ||
emojis (:obj:`str`): One or more emoji corresponding to the sticker. | ||
is_masks (:obj:`bool`, optional): Pass True, if a set of mask stickers should be | ||
created. | ||
mask_position (:class:`telegram.MaskPosition`, optional): Position where the mask | ||
should be placed on faces. | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:obj:`bool`: On success, ``True`` is returned. | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
|
||
url = '{0}/createNewStickerSet'.format(self.base_url) | ||
|
||
data = {'user_id': user_id, 'name': name, 'title': title, 'png_sticker': png_sticker, | ||
'emojis': emojis} | ||
|
||
if is_masks is not None: | ||
data['is_masks'] = is_masks | ||
if mask_position is not None: | ||
data['mask_position'] = mask_position | ||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return result | ||
|
||
def add_sticker_to_set(self, user_id, name, png_sticker, emojis, mask_position=None, | ||
timeout=None, **kwargs): | ||
""" | ||
Use this method to add a new sticker to a set created by the bot. | ||
|
||
Note: | ||
The png_sticker argument can be either a file_id, an URL or a file from disk | ||
``open(filename, 'rb')`` | ||
|
||
Args: | ||
user_id (:obj:`int`): User identifier of created sticker set owner. | ||
name (:obj:`str`): Sticker set name. | ||
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker, must be up | ||
to 512 kilobytes in size, dimensions must not exceed 512px, | ||
and either width or height must be exactly 512px. Pass a file_id as a String to | ||
send a file that already exists on the Telegram servers, pass an HTTP URL as a | ||
String for Telegram to get a file from the Internet, or upload a new one | ||
using multipart/form-data. | ||
emojis (:obj:`str`): One or more emoji corresponding to the sticker. | ||
mask_position (:class:`telegram.MaskPosition`, optional): Position where the mask | ||
should beplaced on faces. | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:obj:`bool`: On success, ``True`` is returned. | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
|
||
url = '{0}/addStickerToSet'.format(self.base_url) | ||
|
||
data = {'user_id': user_id, 'name': name, 'png_sticker': png_sticker, 'emojis': emojis} | ||
|
||
if mask_position is not None: | ||
data['mask_position'] = mask_position | ||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return result | ||
|
||
def set_sticker_position_in_set(self, sticker, position, timeout=None, **kwargs): | ||
""" | ||
Use this method to move a sticker in a set created by the bot to a specific position. | ||
|
||
Args: | ||
sticker (:obj:`str`): File identifier of the sticker. | ||
position (:obj:`int`): New sticker position in the set, zero-based. | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:obj:`bool`: On success, ``True`` is returned. | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
url = '{0}/setStickerPositionInSet'.format(self.base_url) | ||
|
||
data = {'sticker': sticker, 'position': position} | ||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return result | ||
|
||
def delete_sticker_from_set(self, sticker, timeout=None, **kwargs): | ||
""" | ||
Use this method to delete a sticker from a set created by the bot. | ||
|
||
Args: | ||
sticker (:obj:`str`): File identifier of the sticker. | ||
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as | ||
the read timeout from the server (instead of the one specified during | ||
creation of the connection pool). | ||
**kwargs (:obj:`dict`): Arbitrary keyword arguments. | ||
|
||
Returns: | ||
:obj:`bool`: On success, ``True`` is returned. | ||
|
||
Raises: | ||
:class:`telegram.TelegramError` | ||
""" | ||
url = '{0}/deleteStickerFromSet'.format(self.base_url) | ||
|
||
data = {'sticker': sticker} | ||
|
||
result = self._request.post(url, data, timeout=timeout) | ||
|
||
return result | ||
|
||
@staticmethod | ||
def de_json(data, bot): | ||
data = super(Bot, Bot).de_json(data, bot) | ||
|
@@ -2436,3 +2648,9 @@ def __reduce__(self): | |
setChatDescription = set_chat_description | ||
pinChatMessage = pin_chat_message | ||
unpinChatMessage = unpin_chat_message | ||
getStickerSet = get_sticker_set | ||
uploadStickerFile = upload_sticker_file | ||
createNewStickerSet = create_new_sticker_set | ||
addStickerToSet = add_sticker_to_set | ||
setStickerPositionInSet = set_sticker_position_in_set | ||
deleteStickerFromSet = delete_sticker_from_set |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definging
**kwargs
in arguments but not adding them to data is useless. I suggest removing them from arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @Eldinnie
kwargs
are automatically passed when using the@message
decorator, but if it's not being used, than they should be pushed in thedata
dictionary.the reason for that, is to allow forward compatibility (lets say telegram adds some arguments t an existing API call, then users can use the new API even before we release an official support).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyway, we can fix that in another PR as it's not the only place where we missed that.