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

Skip to content

Commit 5a37af6

Browse files
jsmnbomtsnoam
authored andcommitted
NOTE: Currently not testing StickerSet in terms of bot methods (interaction with telegrams servers) as there's no delete operations.
1 parent 881da40 commit 5a37af6

File tree

6 files changed

+455
-25
lines changed

6 files changed

+455
-25
lines changed

docs/source/telegram.sticker.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
telegram.sticker module
22
=======================
33

4-
.. automodule:: telegram.sticker
4+
.. automodule:: telegram.files.sticker
55
:members:
6-
:undoc-members:
76
:show-inheritance:

telegram/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .files.audio import Audio
2828
from .files.voice import Voice
2929
from .files.document import Document
30-
from .files.sticker import Sticker
30+
from .files.sticker import Sticker, StickerSet, MaskPosition
3131
from .files.video import Video
3232
from .files.contact import Contact
3333
from .files.location import Location
@@ -120,5 +120,6 @@
120120
'MAX_FILESIZE_DOWNLOAD', 'MAX_FILESIZE_UPLOAD', 'MAX_MESSAGES_PER_SECOND_PER_CHAT',
121121
'MAX_MESSAGES_PER_SECOND', 'MAX_MESSAGES_PER_MINUTE_PER_GROUP', 'WebhookInfo', 'Animation',
122122
'Game', 'GameHighScore', 'VideoNote', 'LabeledPrice', 'SuccessfulPayment', 'ShippingOption',
123-
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto'
123+
'ShippingAddress', 'PreCheckoutQuery', 'OrderInfo', 'Invoice', 'ShippingQuery', 'ChatPhoto',
124+
'StickerSet', 'MaskPosition'
124125
]

telegram/bot.py

+219-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from datetime import datetime
2727

2828
from telegram import (User, Message, Update, Chat, ChatMember, UserProfilePhotos, File,
29-
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore)
29+
ReplyMarkup, TelegramObject, WebhookInfo, GameHighScore, StickerSet)
3030
from telegram.error import InvalidToken, TelegramError
3131
from telegram.utils.helpers import to_timestamp
3232
from telegram.utils.request import Request
@@ -2369,6 +2369,218 @@ def unpin_chat_message(self, chat_id, timeout=None, **kwargs):
23692369

23702370
return result
23712371

2372+
def get_sticker_set(self, name, timeout=None, **kwargs):
2373+
"""
2374+
Use this method to get a sticker set.
2375+
2376+
Args:
2377+
name (:obj:`str`): Short name of the sticker set that is used in t.me/addstickers/
2378+
URLs (e.g., animals)
2379+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2380+
the read timeout from the server (instead of the one specified during
2381+
creation of the connection pool).
2382+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2383+
2384+
Returns:
2385+
:class:`telegram.StickerSet`
2386+
2387+
Raises:
2388+
:class:`telegram.TelegramError`
2389+
"""
2390+
2391+
url = '{0}/getStickerSet'.format(self.base_url)
2392+
2393+
data = {'name': name}
2394+
2395+
result = self._request.post(url, data, timeout=timeout)
2396+
2397+
return StickerSet.de_json(result, self)
2398+
2399+
def upload_sticker_file(self, user_id, png_sticker, timeout=None, **kwargs):
2400+
"""
2401+
Use this method to upload a .png file with a sticker for later use in
2402+
:attr:`create_new_sticker_set` and :attr:`add_sticker_to_set` methods (can be used multiple
2403+
times).
2404+
2405+
Note:
2406+
The png_sticker argument can be either a file_id, an URL or a file from disk
2407+
``open(filename, 'rb')``
2408+
2409+
Args:
2410+
user_id (:obj:`int`): User identifier of sticker file owner.
2411+
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker,
2412+
must be up to 512 kilobytes in size, dimensions must not exceed 512px,
2413+
and either width or height must be exactly 512px.
2414+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2415+
the read timeout from the server (instead of the one specified during
2416+
creation of the connection pool).
2417+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2418+
2419+
Returns:
2420+
:class:`telegram.File`: The uploaded File
2421+
2422+
Raises:
2423+
:class:`telegram.TelegramError`
2424+
"""
2425+
2426+
url = '{0}/uploadStickerFile'.format(self.base_url)
2427+
2428+
data = {'user_id': user_id, 'png_sticker': png_sticker}
2429+
2430+
result = self._request.post(url, data, timeout=timeout)
2431+
2432+
return File.de_json(result, self)
2433+
2434+
def create_new_sticker_set(self, user_id, name, title, png_sticker, emojis, is_masks=None,
2435+
mask_position=None, timeout=None, **kwargs):
2436+
"""
2437+
Use this method to create new sticker set owned by a user.
2438+
The bot will be able to edit the created sticker set.
2439+
2440+
Note:
2441+
The png_sticker argument can be either a file_id, an URL or a file from disk
2442+
``open(filename, 'rb')``
2443+
2444+
Args:
2445+
user_id (:obj:`int`): User identifier of created sticker set owner.
2446+
name (:obj:`str`): Short name of sticker set, to be used in t.me/addstickers/ URLs
2447+
(e.g., animals). Can contain only english letters, digits and underscores.
2448+
Must begin with a letter, can't contain consecutive underscores and
2449+
must end in "_by_<bot username>". <bot_username> is case insensitive.
2450+
1-64 characters.
2451+
title (:obj:`str`): Sticker set title, 1-64 characters.
2452+
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker, must be up
2453+
to 512 kilobytes in size, dimensions must not exceed 512px,
2454+
and either width or height must be exactly 512px. Pass a file_id as a String to
2455+
send a file that already exists on the Telegram servers, pass an HTTP URL as a
2456+
String for Telegram to get a file from the Internet, or upload a new one
2457+
using multipart/form-data.
2458+
emojis (:obj:`str`): One or more emoji corresponding to the sticker.
2459+
is_masks (:obj:`bool`, optional): Pass True, if a set of mask stickers should be
2460+
created.
2461+
mask_position (:class:`telegram.MaskPosition`, optional): Position where the mask
2462+
should be placed on faces.
2463+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2464+
the read timeout from the server (instead of the one specified during
2465+
creation of the connection pool).
2466+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2467+
2468+
Returns:
2469+
:obj:`bool`: On success, ``True`` is returned.
2470+
2471+
Raises:
2472+
:class:`telegram.TelegramError`
2473+
"""
2474+
2475+
url = '{0}/createNewStickerSet'.format(self.base_url)
2476+
2477+
data = {'user_id': user_id, 'name': name, 'title': title, 'png_sticker': png_sticker,
2478+
'emojis': emojis}
2479+
2480+
if is_masks is not None:
2481+
data['is_masks'] = is_masks
2482+
if mask_position is not None:
2483+
data['mask_position'] = mask_position
2484+
2485+
result = self._request.post(url, data, timeout=timeout)
2486+
2487+
return result
2488+
2489+
def add_sticker_to_set(self, user_id, name, png_sticker, emojis, mask_position=None,
2490+
timeout=None, **kwargs):
2491+
"""
2492+
Use this method to add a new sticker to a set created by the bot.
2493+
2494+
Note:
2495+
The png_sticker argument can be either a file_id, an URL or a file from disk
2496+
``open(filename, 'rb')``
2497+
2498+
Args:
2499+
user_id (:obj:`int`): User identifier of created sticker set owner.
2500+
name (:obj:`str`): Sticker set name.
2501+
png_sticker (:obj:`str` | `filelike object`): Png image with the sticker, must be up
2502+
to 512 kilobytes in size, dimensions must not exceed 512px,
2503+
and either width or height must be exactly 512px. Pass a file_id as a String to
2504+
send a file that already exists on the Telegram servers, pass an HTTP URL as a
2505+
String for Telegram to get a file from the Internet, or upload a new one
2506+
using multipart/form-data.
2507+
emojis (:obj:`str`): One or more emoji corresponding to the sticker.
2508+
mask_position (:class:`telegram.MaskPosition`, optional): Position where the mask
2509+
should beplaced on faces.
2510+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2511+
the read timeout from the server (instead of the one specified during
2512+
creation of the connection pool).
2513+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2514+
2515+
Returns:
2516+
:obj:`bool`: On success, ``True`` is returned.
2517+
2518+
Raises:
2519+
:class:`telegram.TelegramError`
2520+
"""
2521+
2522+
url = '{0}/addStickerToSet'.format(self.base_url)
2523+
2524+
data = {'user_id': user_id, 'name': name, 'png_sticker': png_sticker, 'emojis': emojis}
2525+
2526+
if mask_position is not None:
2527+
data['mask_position'] = mask_position
2528+
2529+
result = self._request.post(url, data, timeout=timeout)
2530+
2531+
return result
2532+
2533+
def set_sticker_position_in_set(self, sticker, position, timeout=None, **kwargs):
2534+
"""
2535+
Use this method to move a sticker in a set created by the bot to a specific position.
2536+
2537+
Args:
2538+
sticker (:obj:`str`): File identifier of the sticker.
2539+
position (:obj:`int`): New sticker position in the set, zero-based.
2540+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2541+
the read timeout from the server (instead of the one specified during
2542+
creation of the connection pool).
2543+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2544+
2545+
Returns:
2546+
:obj:`bool`: On success, ``True`` is returned.
2547+
2548+
Raises:
2549+
:class:`telegram.TelegramError`
2550+
"""
2551+
url = '{0}/setStickerPositionInSet'.format(self.base_url)
2552+
2553+
data = {'sticker': sticker, 'position': position}
2554+
2555+
result = self._request.post(url, data, timeout=timeout)
2556+
2557+
return result
2558+
2559+
def delete_sticker_from_set(self, sticker, timeout=None, **kwargs):
2560+
"""
2561+
Use this method to delete a sticker from a set created by the bot.
2562+
2563+
Args:
2564+
sticker (:obj:`str`): File identifier of the sticker.
2565+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
2566+
the read timeout from the server (instead of the one specified during
2567+
creation of the connection pool).
2568+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
2569+
2570+
Returns:
2571+
:obj:`bool`: On success, ``True`` is returned.
2572+
2573+
Raises:
2574+
:class:`telegram.TelegramError`
2575+
"""
2576+
url = '{0}/deleteStickerFromSet'.format(self.base_url)
2577+
2578+
data = {'sticker': sticker}
2579+
2580+
result = self._request.post(url, data, timeout=timeout)
2581+
2582+
return result
2583+
23722584
@staticmethod
23732585
def de_json(data, bot):
23742586
data = super(Bot, Bot).de_json(data, bot)
@@ -2436,3 +2648,9 @@ def __reduce__(self):
24362648
setChatDescription = set_chat_description
24372649
pinChatMessage = pin_chat_message
24382650
unpinChatMessage = unpin_chat_message
2651+
getStickerSet = get_sticker_set
2652+
uploadStickerFile = upload_sticker_file
2653+
createNewStickerSet = create_new_sticker_set
2654+
addStickerToSet = add_sticker_to_set
2655+
setStickerPositionInSet = set_sticker_position_in_set
2656+
deleteStickerFromSet = delete_sticker_from_set

telegram/files/inputfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
DEFAULT_MIME_TYPE = 'application/octet-stream'
3737
USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)'
3838
FILE_TYPES = ('audio', 'document', 'photo', 'sticker', 'video', 'voice', 'certificate',
39-
'video_note')
39+
'video_note', 'png_sticker')
4040

4141

4242
class InputFile(object):

0 commit comments

Comments
 (0)