Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 11 commits into from
Jul 22, 2017
3 changes: 1 addition & 2 deletions docs/source/telegram.sticker.rst
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:
5 changes: 3 additions & 2 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .files.audio import Audio
from .files.voice import Voice
from .files.document import Document
from .files.sticker import Sticker
from .files.sticker import Sticker, StickerSet, MaskPosition
from .files.video import Video
from .files.contact import Contact
from .files.location import Location
Expand Down Expand Up @@ -120,5 +120,6 @@
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo', 'Animation',
'Game', 'GameHighScore', 'VideoNote', 'LabeledPrice', 'SuccessfulPayment', 'ShippingOption',
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto'
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto',
'StickerSet', 'MaskPosition'
]
220 changes: 219 additions & 1 deletion telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Copy link
Member

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.

Copy link
Member

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 the data 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).

Copy link
Member

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.


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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same note about **kwargs.


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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion telegram/files/inputfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
DEFAULT_MIME_TYPE = 'application/octet-stream'
USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)'
FILE_TYPES = ('audio', 'document', 'photo', 'sticker', 'video', 'voice', 'certificate',
'video_note')
'video_note', 'png_sticker')


class InputFile(object):
Expand Down
Loading