From f34212cab8439de4d086e519a1bd36b6bf05f53c Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 8 Apr 2025 11:15:11 +0530 Subject: [PATCH 01/19] (init): refactoring --- pyrogram/types/messages_and_media/sticker.py | 25 +++++++++++--------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pyrogram/types/messages_and_media/sticker.py b/pyrogram/types/messages_and_media/sticker.py index 1e9f641a62..2423d6e5e0 100644 --- a/pyrogram/types/messages_and_media/sticker.py +++ b/pyrogram/types/messages_and_media/sticker.py @@ -37,6 +37,7 @@ class Sticker(Object): Unique identifier for this file, which is supposed to be the same over time and for different accounts. Can't be used to download or reuse the file. + width (``int``): Sticker width. @@ -49,26 +50,28 @@ class Sticker(Object): is_video (``bool``): True, if the sticker is a video sticker + thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*): + Sticker thumbnails in the .webp or .jpg format. + + emoji (``str``, *optional*): + Emoji associated with the sticker. + + set_name (``str``, *optional*): + Name of the sticker set to which the sticker belongs. + + + file_size (``int``, *optional*): + File size in bytes. + file_name (``str``, *optional*): Sticker file name. mime_type (``str``, *optional*): MIME type of the file as defined by sender. - file_size (``int``, *optional*): - File size. - date (:py:obj:`~datetime.datetime`, *optional*): Date the sticker was sent. - emoji (``str``, *optional*): - Emoji associated with the sticker. - - set_name (``str``, *optional*): - Name of the sticker set to which the sticker belongs. - - thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*): - Sticker thumbnails in the .webp or .jpg format. """ # TODO: Add mask position From ce75fe538a173a72923669272c08d5c8dcf495b8 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 8 Apr 2025 11:21:58 +0530 Subject: [PATCH 02/19] minor fixes --- pyrogram/types/messages_and_media/sticker.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/pyrogram/types/messages_and_media/sticker.py b/pyrogram/types/messages_and_media/sticker.py index 2423d6e5e0..c19cb37ca9 100644 --- a/pyrogram/types/messages_and_media/sticker.py +++ b/pyrogram/types/messages_and_media/sticker.py @@ -114,7 +114,7 @@ def __init__( cache = {} @staticmethod - async def _get_sticker_set_name(invoke, input_sticker_set_id): + async def _get_sticker_set_name(client, input_sticker_set_id): try: set_id = input_sticker_set_id[0] set_access_hash = input_sticker_set_id[1] @@ -124,17 +124,13 @@ async def _get_sticker_set_name(invoke, input_sticker_set_id): if name is not None: return name - name = ( - await invoke( - raw.functions.messages.GetStickerSet( - stickerset=raw.types.InputStickerSetID( - id=set_id, - access_hash=set_access_hash - ), - hash=0 - ) + _, _sticker_set = await client._get_raw_stickers( + raw.types.InputStickerSetID( + id=set_id, + access_hash=set_access_hash ) - ).set.short_name + ) + name = _sticker_set.short_name Sticker.cache[(set_id, set_access_hash)] = name @@ -167,7 +163,7 @@ async def _parse( if isinstance(sticker_set, raw.types.InputStickerSetID): input_sticker_set_id = (sticker_set.id, sticker_set.access_hash) # TODO: FIXME! - set_name = await Sticker._get_sticker_set_name(client.invoke, input_sticker_set_id) + set_name = await Sticker._get_sticker_set_name(client, input_sticker_set_id) else: set_name = None From 693e7e7c59fb9c33bd7be884cb4d7640e1e6c455 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 8 Apr 2025 11:41:55 +0530 Subject: [PATCH 03/19] Add delete_sticker_set --- compiler/docs/compiler.py | 1 + pyrogram/methods/stickers/__init__.py | 2 + .../methods/stickers/delete_sticker_set.py | 61 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 pyrogram/methods/stickers/delete_sticker_set.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 0709a28623..b1b3e7e19f 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -387,6 +387,7 @@ def get_title_list(s: str) -> list: get_custom_emoji_stickers get_message_effects get_stickers + delete_sticker_set """, stories=""" Stories diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index f213537d69..d3a7641953 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -16,11 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .delete_sticker_set import DeleteStickerSet from .get_message_effects import GetMessageEffects from .get_stickers import GetStickers class Stickers( + DeleteStickerSet, GetMessageEffects, GetStickers, ): diff --git a/pyrogram/methods/stickers/delete_sticker_set.py b/pyrogram/methods/stickers/delete_sticker_set.py new file mode 100644 index 0000000000..46716ef712 --- /dev/null +++ b/pyrogram/methods/stickers/delete_sticker_set.py @@ -0,0 +1,61 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +import pyrogram +from pyrogram import raw +from pyrogram import types + +log = logging.getLogger(__name__) + + +class DeleteStickerSet: + async def delete_sticker_set( + self: "pyrogram.Client", + name: str + ) -> bool: + """Use this method to delete a sticker set that was created by the current user. + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + name (``str``): + Sticker set name. + + Returns: + ``bool``: True on success. + + Example: + .. code-block:: python + + # Get all stickers by short name + await app.get_stickers("short_name") + + Raises: + RPCError: In case of Telegram RPCError. + + """ + r = await self.invoke( + raw.functions.stickers.DeleteStickerSet( + stickerset=raw.types.InputStickerSetShortName( + short_name=name + ) + ) + ) + return r From 9892a9b2136f0ec9358fd6c5c44e925223cf8438 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 8 Apr 2025 11:42:47 +0530 Subject: [PATCH 04/19] fix examples --- pyrogram/methods/stickers/delete_sticker_set.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pyrogram/methods/stickers/delete_sticker_set.py b/pyrogram/methods/stickers/delete_sticker_set.py index 46716ef712..e45ef2c37e 100644 --- a/pyrogram/methods/stickers/delete_sticker_set.py +++ b/pyrogram/methods/stickers/delete_sticker_set.py @@ -41,12 +41,6 @@ async def delete_sticker_set( Returns: ``bool``: True on success. - Example: - .. code-block:: python - - # Get all stickers by short name - await app.get_stickers("short_name") - Raises: RPCError: In case of Telegram RPCError. From 634c78d469cdd079293975fcdd0a520ef5bca1b2 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:17:35 +0200 Subject: [PATCH 05/19] Rename get_custom_emoji_stickers --- .../methods/{messages => stickers}/get_custom_emoji_stickers.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pyrogram/methods/{messages => stickers}/get_custom_emoji_stickers.py (100%) diff --git a/pyrogram/methods/messages/get_custom_emoji_stickers.py b/pyrogram/methods/stickers/get_custom_emoji_stickers.py similarity index 100% rename from pyrogram/methods/messages/get_custom_emoji_stickers.py rename to pyrogram/methods/stickers/get_custom_emoji_stickers.py From d0f0b5f104074fb6f7ba1ff4983a181babad2c75 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:19:12 +0200 Subject: [PATCH 06/19] Revert ce75fe5 maandan #36 --- pyrogram/types/messages_and_media/sticker.py | 28 ++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pyrogram/types/messages_and_media/sticker.py b/pyrogram/types/messages_and_media/sticker.py index c19cb37ca9..498d8442e9 100644 --- a/pyrogram/types/messages_and_media/sticker.py +++ b/pyrogram/types/messages_and_media/sticker.py @@ -124,13 +124,24 @@ async def _get_sticker_set_name(client, input_sticker_set_id): if name is not None: return name - _, _sticker_set = await client._get_raw_stickers( - raw.types.InputStickerSetID( - id=set_id, - access_hash=set_access_hash + # _, _sticker_set = await client._get_raw_stickers( + # raw.types.InputStickerSetID( + # id=set_id, + # access_hash=set_access_hash + # ) + # ) + # name = _sticker_set.short_name + # TODO: FIXME! + sticker_set = await client.invoke( + raw.functions.messages.GetStickerSet( + stickerset=raw.types.InputStickerSetID( + id=set_id, + access_hash=set_access_hash + ), + hash=0 ) ) - name = _sticker_set.short_name + name = sticker_set.set.short_name Sticker.cache[(set_id, set_access_hash)] = name @@ -163,6 +174,13 @@ async def _parse( if isinstance(sticker_set, raw.types.InputStickerSetID): input_sticker_set_id = (sticker_set.id, sticker_set.access_hash) # TODO: FIXME! + # _, _sticker_set = await client._get_raw_stickers( + # raw.types.InputStickerSetID( + # id=input_sticker_set_id[0], + # access_hash=input_sticker_set_id[1] + # ) + # ) + # set_name = _sticker_set.short_name set_name = await Sticker._get_sticker_set_name(client, input_sticker_set_id) else: set_name = None From e3059f57b68fac92835d464d47061fef8a410d5b Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:19:31 +0200 Subject: [PATCH 07/19] steal https://github.com/LonamiWebs/Telethon/blob/3921914a96101fba990dee39ac7b9e29375b8b33/telethon/utils.py#L317-L339 --- pyrogram/utils.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pyrogram/utils.py b/pyrogram/utils.py index d60ef0d4a4..bf9210b7a6 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -657,3 +657,44 @@ def is_list_like(obj): Ported from https://github.com/LonamiWebs/Telethon/blob/1cb5ff1dd54ecfad41711fc5a4ecf36d2ad8eaf6/telethon/utils.py#L902 """ return isinstance(obj, (list, tuple, set, dict, range)) + + +def _raise_cast_fail(entity, target): + """Ported from https://github.com/LonamiWebs/Telethon/blob/3921914a96101fba990dee39ac7b9e29375b8b33/telethon/utils.py#L130-L132 + """ + raise TypeError( + 'Cannot cast {} to any kind of {}.'.format( + type(entity).__name__, + target + ) + ) + + +def get_input_document(document): + """Similar to ``get_input_peer``, but for documents. + + Ported from https://github.com/LonamiWebs/Telethon/blob/3921914a96101fba990dee39ac7b9e29375b8b33/telethon/utils.py#L317-L339 + """ + try: + if isinstance(document, raw.types.InputDocument): + return document + except AttributeError: + _raise_cast_fail(document, "InputDocument") + + if isinstance(document, raw.types.Document): + return raw.types.InputDocument( + id=document.id, + access_hash=document.access_hash, + file_reference=document.file_reference + ) + + if isinstance(document, raw.types.DocumentEmpty): + return raw.types.InputDocumentEmpty() + + if isinstance(document, raw.types.MessageMediaDocument): + return get_input_document(document.document) + + if isinstance(document, raw.types.Message): + return get_input_document(document.media) + + _raise_cast_fail(document, "InputDocument") From 6fb7b46034f37c9adfc9223252419becb37f8f1a Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:20:02 +0200 Subject: [PATCH 08/19] Add StickerFormat --- pyrogram/enums/__init__.py | 2 ++ pyrogram/enums/sticker_format.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 pyrogram/enums/sticker_format.py diff --git a/pyrogram/enums/__init__.py b/pyrogram/enums/__init__.py index 8a744e7f9c..cc13599412 100644 --- a/pyrogram/enums/__init__.py +++ b/pyrogram/enums/__init__.py @@ -31,6 +31,7 @@ from .parse_mode import ParseMode from .poll_type import PollType from .sent_code_type import SentCodeType +from .sticker_format import StickerFormat from .user_status import UserStatus from .client_platform import ClientPlatform from .accent_color import AccentColor @@ -52,6 +53,7 @@ 'ParseMode', 'PollType', 'SentCodeType', + 'StickerFormat', 'UserStatus', 'ClientPlatform', 'AccentColor', diff --git a/pyrogram/enums/sticker_format.py b/pyrogram/enums/sticker_format.py new file mode 100644 index 0000000000..550afc1d2a --- /dev/null +++ b/pyrogram/enums/sticker_format.py @@ -0,0 +1,34 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from enum import auto + +from .auto_name import AutoName + + +class StickerFormat(AutoName): + """Format of the sticker.""" + + STATIC = auto() + "static" + + ANIMATED = auto() + "animated" + + VIDEO = auto() + "video" From 8687b8bfadb5c21fc70c7e7e1f9ea6db68fdc2f3 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:20:26 +0200 Subject: [PATCH 09/19] Rename get_custom_emoji_stickers --- pyrogram/methods/messages/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyrogram/methods/messages/__init__.py b/pyrogram/methods/messages/__init__.py index 9519a07772..3694917947 100644 --- a/pyrogram/methods/messages/__init__.py +++ b/pyrogram/methods/messages/__init__.py @@ -33,7 +33,6 @@ from .forward_messages import ForwardMessages from .get_chat_history import GetChatHistory from .get_chat_history_count import GetChatHistoryCount -from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_discussion_message import GetDiscussionMessage from .get_discussion_replies import GetDiscussionReplies from .get_discussion_replies_count import GetDiscussionRepliesCount @@ -93,7 +92,6 @@ class Messages( ForwardMessages, GetChatHistory, GetChatHistoryCount, - GetCustomEmojiStickers, GetDiscussionMessage, GetDiscussionReplies, GetDiscussionRepliesCount, From 56a6a83c49eb17691454be4c15330cf8a0fb7be7 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:20:51 +0200 Subject: [PATCH 10/19] Add incomplete upload_sticker_file --- pyrogram/methods/stickers/__init__.py | 4 + .../methods/stickers/upload_sticker_file.py | 89 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 pyrogram/methods/stickers/upload_sticker_file.py diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index d3a7641953..a60900ab35 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -17,13 +17,17 @@ # along with Pyrogram. If not, see . from .delete_sticker_set import DeleteStickerSet +from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_message_effects import GetMessageEffects from .get_stickers import GetStickers +from .upload_sticker_file import UploadStickerFile class Stickers( DeleteStickerSet, + GetCustomEmojiStickers, GetMessageEffects, GetStickers, + UploadStickerFile, ): pass diff --git a/pyrogram/methods/stickers/upload_sticker_file.py b/pyrogram/methods/stickers/upload_sticker_file.py new file mode 100644 index 0000000000..09b5613a16 --- /dev/null +++ b/pyrogram/methods/stickers/upload_sticker_file.py @@ -0,0 +1,89 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging +import os + +import pyrogram +from pyrogram import enums, raw, types, utils + +log = logging.getLogger(__name__) + + +class UploadStickerFile: + async def upload_sticker_file( + self: "pyrogram.Client", + user_id: int, + sticker: str, + sticker_format: "enums.StickerFormat" + ) -> bool: + """Use this method to upload a file with a sticker for later use in the :meth:`~pyrogram.Client.create_new_sticker_set`, :meth:`~pyrogram.Client.add_sticker_to_set`, or :meth:`~pyrogram.Client.replace_sticker_in_set` methods (the file can be used multiple times). + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + user_id (``int``): + User identifier of sticker file owner; ignored for regular users. + + sticker (``str``): + A file with the sticker in .WEBP, .PNG, .TGS, or .WEBM format. + File to upload; must fit in a 512x512 square. For WEBP stickers the file must be in WEBP or PNG format, which will be converted to WEBP server-side. + See https://core.telegram.org/animated_stickers#technical-requirements for technical requirements + + sticker_format (:obj:`~pyrogram.enums.StickerFormat`): + Format of the sticker. + + Returns: + :obj:`~pyrogram.raw.types.InputDocument`: Returns the uploaded :obj:`~pyrogram.types.File` on success. TODO + + Raises: + RPCError: In case of Telegram RPCError. + ValueError: In case of invalid arguments. + + """ + mime_type = None + if sticker_format == enums.StickerFormat.STATIC: + mime_type = "image/png" + elif sticker_format == enums.StickerFormat.ANIMATED: + mime_type = "application/x-tgsticker" + elif sticker_format == enums.StickerFormat.VIDEO: + mime_type = "video/webm" + else: + raise ValueError("Invalid sticker_format") + peer = None + if self.me.is_bot: + peer = await self.resolve_peer(user_id) + else: + peer = await self.resolve_peer("me") + file = await self.save_file(sticker) + media = raw.types.InputMediaUploadedDocument( + mime_type=mime_type, + file=file, + attributes=[ + raw.types.DocumentAttributeFilename( + file_name=os.path.basename(sticker) + ), + ] + ) + uploaded_media = await self.invoke( + raw.functions.messages.UploadMedia( + peer=peer, + media=media + ) + ) + return utils.get_input_document(uploaded_media) From 98747e24159a5f2abd10a35a79c12a615e712735 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Wed, 9 Apr 2025 20:20:55 +0200 Subject: [PATCH 11/19] docs --- compiler/docs/compiler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index b1b3e7e19f..32ca5e4c9e 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -386,6 +386,7 @@ def get_title_list(s: str) -> list: send_sticker get_custom_emoji_stickers get_message_effects + upload_sticker_file get_stickers delete_sticker_set """, @@ -884,6 +885,7 @@ def get_title_list(s: str) -> list: ProfileColor AccentColor SentCodeType + StickerFormat NextCodeType UserStatus """, From daa576309a935d7481b356dd990ed8e8b8c1e6a0 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Thu, 10 Apr 2025 11:51:22 +0530 Subject: [PATCH 12/19] change return of upload_sticker_file --- compiler/docs/compiler.py | 1 + .../methods/stickers/upload_sticker_file.py | 38 ++++++++++-- pyrogram/types/messages_and_media/__init__.py | 2 + pyrogram/types/messages_and_media/file.py | 60 +++++++++++++++++++ pyrogram/utils.py | 41 ------------- 5 files changed, 97 insertions(+), 45 deletions(-) create mode 100644 pyrogram/types/messages_and_media/file.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 32ca5e4c9e..3089bc4341 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -508,6 +508,7 @@ def get_title_list(s: str) -> list: Animation Audio Document + File Story Video VideoNote diff --git a/pyrogram/methods/stickers/upload_sticker_file.py b/pyrogram/methods/stickers/upload_sticker_file.py index 09b5613a16..3ed3226372 100644 --- a/pyrogram/methods/stickers/upload_sticker_file.py +++ b/pyrogram/methods/stickers/upload_sticker_file.py @@ -20,7 +20,8 @@ import os import pyrogram -from pyrogram import enums, raw, types, utils +from pyrogram import enums, raw, types +from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType log = logging.getLogger(__name__) @@ -49,7 +50,7 @@ async def upload_sticker_file( Format of the sticker. Returns: - :obj:`~pyrogram.raw.types.InputDocument`: Returns the uploaded :obj:`~pyrogram.types.File` on success. TODO + :obj:`~pyrogram.types.File`: Returns the uploaded file on success. Raises: RPCError: In case of Telegram RPCError. @@ -80,10 +81,39 @@ async def upload_sticker_file( ), ] ) - uploaded_media = await self.invoke( + media = await self.invoke( raw.functions.messages.UploadMedia( peer=peer, media=media ) ) - return utils.get_input_document(uploaded_media) + media = raw.types.InputMediaDocument( + id=raw.types.InputDocument( + id=media.document.id, + access_hash=media.document.access_hash, + file_reference=media.document.file_reference + ) + ) + sticker = media.document + document_attributes = sticker.attributes + sticker_attributes = ( + document_attributes[raw.types.DocumentAttributeSticker] + if raw.types.DocumentAttributeSticker in document_attributes + else document_attributes[raw.types.DocumentAttributeCustomEmoji] + ) + file_name = getattr(document_attributes.get(raw.types.DocumentAttributeFilename, None), "file_name", None) + return types.File( + file_id=FileId( + file_type=FileType.STICKER, + dc_id=sticker.dc_id, + media_id=sticker.id, + access_hash=sticker.access_hash, + file_reference=sticker.file_reference + ).encode(), + file_unique_id=FileUniqueId( + file_unique_type=FileUniqueType.DOCUMENT, + media_id=sticker.id + ).encode(), + file_name=file_name, + file_size=sticker.size, + ) diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index 54aaada67b..d5b59f53f7 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -23,6 +23,7 @@ from .contact_registered import ContactRegistered from .dice import Dice from .document import Document +from .file import File from .game import Game from .location import Location from .message import Message @@ -80,6 +81,7 @@ "ContactRegistered", "Dice", "Document", + "File", "Game", "PaymentForm", "GiftCode", diff --git a/pyrogram/types/messages_and_media/file.py b/pyrogram/types/messages_and_media/file.py new file mode 100644 index 0000000000..1c6056d22a --- /dev/null +++ b/pyrogram/types/messages_and_media/file.py @@ -0,0 +1,60 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + + +import pyrogram +from pyrogram import raw, utils +from pyrogram import types +from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource +from ..object import Object + + +class File(Object): + """a file. + + Parameters: + file_id (``str``): + Identifier for this file, which can be used to download or reuse the file. + + file_unique_id (``str``): + Unique identifier for this file, which is supposed to be the same over time and for different accounts. + Can't be used to download or reuse the file. + + file_name (``str``, *optional*): + file name. + + file_size (``int``, *optional*): + File size. + + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + file_id: str, + file_unique_id: str, + file_name: str = None, + file_size: int = None, + ): + super().__init__(client) + + self.file_id = file_id + self.file_unique_id = file_unique_id + self.file_name = file_name + self.file_size = file_size diff --git a/pyrogram/utils.py b/pyrogram/utils.py index bf9210b7a6..d60ef0d4a4 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -657,44 +657,3 @@ def is_list_like(obj): Ported from https://github.com/LonamiWebs/Telethon/blob/1cb5ff1dd54ecfad41711fc5a4ecf36d2ad8eaf6/telethon/utils.py#L902 """ return isinstance(obj, (list, tuple, set, dict, range)) - - -def _raise_cast_fail(entity, target): - """Ported from https://github.com/LonamiWebs/Telethon/blob/3921914a96101fba990dee39ac7b9e29375b8b33/telethon/utils.py#L130-L132 - """ - raise TypeError( - 'Cannot cast {} to any kind of {}.'.format( - type(entity).__name__, - target - ) - ) - - -def get_input_document(document): - """Similar to ``get_input_peer``, but for documents. - - Ported from https://github.com/LonamiWebs/Telethon/blob/3921914a96101fba990dee39ac7b9e29375b8b33/telethon/utils.py#L317-L339 - """ - try: - if isinstance(document, raw.types.InputDocument): - return document - except AttributeError: - _raise_cast_fail(document, "InputDocument") - - if isinstance(document, raw.types.Document): - return raw.types.InputDocument( - id=document.id, - access_hash=document.access_hash, - file_reference=document.file_reference - ) - - if isinstance(document, raw.types.DocumentEmpty): - return raw.types.InputDocumentEmpty() - - if isinstance(document, raw.types.MessageMediaDocument): - return get_input_document(document.document) - - if isinstance(document, raw.types.Message): - return get_input_document(document.media) - - _raise_cast_fail(document, "InputDocument") From bfe3fd052d7bf125f4fdc0e9315dfaf3569d8c85 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Mon, 28 Apr 2025 13:53:29 +0530 Subject: [PATCH 13/19] fix imports --- pyrogram/methods/stickers/upload_sticker_file.py | 2 +- pyrogram/types/messages_and_media/file.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pyrogram/methods/stickers/upload_sticker_file.py b/pyrogram/methods/stickers/upload_sticker_file.py index 3ed3226372..7cd84d9f29 100644 --- a/pyrogram/methods/stickers/upload_sticker_file.py +++ b/pyrogram/methods/stickers/upload_sticker_file.py @@ -32,7 +32,7 @@ async def upload_sticker_file( user_id: int, sticker: str, sticker_format: "enums.StickerFormat" - ) -> bool: + ) -> "types.File": """Use this method to upload a file with a sticker for later use in the :meth:`~pyrogram.Client.create_new_sticker_set`, :meth:`~pyrogram.Client.add_sticker_to_set`, or :meth:`~pyrogram.Client.replace_sticker_in_set` methods (the file can be used multiple times). .. include:: /_includes/usable-by/users-bots.rst diff --git a/pyrogram/types/messages_and_media/file.py b/pyrogram/types/messages_and_media/file.py index 1c6056d22a..7f0e73ced5 100644 --- a/pyrogram/types/messages_and_media/file.py +++ b/pyrogram/types/messages_and_media/file.py @@ -18,9 +18,6 @@ import pyrogram -from pyrogram import raw, utils -from pyrogram import types -from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource from ..object import Object From d42a1419a0d061c6391797741d592545153e1c12 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 29 Apr 2025 10:23:40 +0530 Subject: [PATCH 14/19] Add set_sticker_set_title --- compiler/docs/compiler.py | 1 + pyrogram/methods/stickers/__init__.py | 2 + .../methods/stickers/delete_sticker_set.py | 1 - .../methods/stickers/set_sticker_set_title.py | 59 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 pyrogram/methods/stickers/set_sticker_set_title.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index eb71ca545f..c5398ef454 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -387,6 +387,7 @@ def get_title_list(s: str) -> list: get_custom_emoji_stickers get_message_effects upload_sticker_file + set_sticker_set_title get_stickers delete_sticker_set """, diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index a60900ab35..fb79010448 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -20,6 +20,7 @@ from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_message_effects import GetMessageEffects from .get_stickers import GetStickers +from .set_sticker_set_title import SetStickerSetTitle from .upload_sticker_file import UploadStickerFile @@ -28,6 +29,7 @@ class Stickers( GetCustomEmojiStickers, GetMessageEffects, GetStickers, + SetStickerSetTitle, UploadStickerFile, ): pass diff --git a/pyrogram/methods/stickers/delete_sticker_set.py b/pyrogram/methods/stickers/delete_sticker_set.py index e45ef2c37e..f0a50c96ad 100644 --- a/pyrogram/methods/stickers/delete_sticker_set.py +++ b/pyrogram/methods/stickers/delete_sticker_set.py @@ -20,7 +20,6 @@ import pyrogram from pyrogram import raw -from pyrogram import types log = logging.getLogger(__name__) diff --git a/pyrogram/methods/stickers/set_sticker_set_title.py b/pyrogram/methods/stickers/set_sticker_set_title.py new file mode 100644 index 0000000000..dfbf7ea682 --- /dev/null +++ b/pyrogram/methods/stickers/set_sticker_set_title.py @@ -0,0 +1,59 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +import pyrogram +from pyrogram import raw + +log = logging.getLogger(__name__) + + +class SetStickerSetTitle: + async def set_sticker_set_title( + self: "pyrogram.Client", + name: str, + title: str + ) -> bool: + """Use this method to set the title of a created sticker set. + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + name (``str``): + Sticker set name. + + title (``str``): + Sticker set title, 1-64 characters. + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of Telegram RPCError. + + """ + r = await self.invoke( + raw.functions.stickers.RenameStickerSet( + stickerset=raw.types.InputStickerSetShortName( + short_name=name + ), + title=title + ) + ) + return r From d6561014fe83536f6bd125ebf12463bd13028113 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Tue, 29 Apr 2025 10:29:53 +0530 Subject: [PATCH 15/19] Add delete_sticker_from_set --- compiler/docs/compiler.py | 1 + pyrogram/methods/stickers/__init__.py | 2 + .../stickers/delete_sticker_from_set.py | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 pyrogram/methods/stickers/delete_sticker_from_set.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index c5398ef454..7a39412b9f 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -389,6 +389,7 @@ def get_title_list(s: str) -> list: upload_sticker_file set_sticker_set_title get_stickers + delete_sticker_from_set delete_sticker_set """, stories=""" diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index fb79010448..309cea07d3 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from .delete_sticker_from_set import DeleteStickerFromSet from .delete_sticker_set import DeleteStickerSet from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_message_effects import GetMessageEffects @@ -25,6 +26,7 @@ class Stickers( + DeleteStickerFromSet, DeleteStickerSet, GetCustomEmojiStickers, GetMessageEffects, diff --git a/pyrogram/methods/stickers/delete_sticker_from_set.py b/pyrogram/methods/stickers/delete_sticker_from_set.py new file mode 100644 index 0000000000..5a988de6aa --- /dev/null +++ b/pyrogram/methods/stickers/delete_sticker_from_set.py @@ -0,0 +1,55 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +import pyrogram +from pyrogram import raw, types, utils + +log = logging.getLogger(__name__) + + +class DeleteStickerFromSet: + async def delete_sticker_from_set( + self: "pyrogram.Client", + sticker: str + ) -> "types.StickerSet": + """Use this method to delete a sticker from a set created by the current user. + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + sticker (``str``): + File identifier of the sticker. + + Returns: + :obj:`~pyrogram.types.StickerSet`: The updated StickerSet on success. + + Raises: + RPCError: In case of Telegram RPCError. + + """ + stickerdocument = utils.get_input_media_from_file_id(sticker) + stickerid = stickerdocument.id + r = await self.invoke( + raw.functions.stickers.RemoveStickerFromSet( + sticker=stickerid + ) + ) + # TODO: + return r From 01be03d12fb2d01262665b347a0743e85b4090a1 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 8 May 2025 19:19:22 +0200 Subject: [PATCH 16/19] Add set_sticker_position_in_set --- compiler/docs/compiler.py | 1 + pyrogram/methods/stickers/__init__.py | 2 + .../stickers/set_sticker_position_in_set.py | 60 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 pyrogram/methods/stickers/set_sticker_position_in_set.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 7a39412b9f..bc9d72ce62 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -387,6 +387,7 @@ def get_title_list(s: str) -> list: get_custom_emoji_stickers get_message_effects upload_sticker_file + set_sticker_position_in_set set_sticker_set_title get_stickers delete_sticker_from_set diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index 309cea07d3..ed06305de2 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -21,6 +21,7 @@ from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_message_effects import GetMessageEffects from .get_stickers import GetStickers +from .set_sticker_position_in_set import SetStickerPositionInSet from .set_sticker_set_title import SetStickerSetTitle from .upload_sticker_file import UploadStickerFile @@ -31,6 +32,7 @@ class Stickers( GetCustomEmojiStickers, GetMessageEffects, GetStickers, + SetStickerPositionInSet, SetStickerSetTitle, UploadStickerFile, ): diff --git a/pyrogram/methods/stickers/set_sticker_position_in_set.py b/pyrogram/methods/stickers/set_sticker_position_in_set.py new file mode 100644 index 0000000000..7bfbd18f17 --- /dev/null +++ b/pyrogram/methods/stickers/set_sticker_position_in_set.py @@ -0,0 +1,60 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +import pyrogram +from pyrogram import raw, utils + +log = logging.getLogger(__name__) + + +class SetStickerPositionInSet: + async def set_sticker_position_in_set( + self: "pyrogram.Client", + sticker: str, + position: int + ) -> bool: + """Use this method to move a sticker in a set created by the bot to a specific position. + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + sticker (``str``): + File identifier of the sticker. + + position (``str``): + New sticker position in the set, zero-based. + + Returns: + ``bool``: True on success. + + Raises: + RPCError: In case of Telegram RPCError. + + """ + stickerdocument = utils.get_input_media_from_file_id(sticker) + stickerid = stickerdocument.id + r = await self.invoke( + raw.functions.stickers.ChangeStickerPosition( + sticker=stickerid, + position=position + ) + ) + # TODO + return r From 1bfadca898b53823cecf876fef1fc1d1283f6d9a Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 8 May 2025 19:24:56 +0200 Subject: [PATCH 17/19] Add get_suggested_sticker_set_name --- compiler/docs/compiler.py | 1 + pyrogram/methods/stickers/__init__.py | 2 + .../get_suggested_sticker_set_name.py | 52 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 pyrogram/methods/stickers/get_suggested_sticker_set_name.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index bc9d72ce62..cf0b33518a 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -383,6 +383,7 @@ def get_title_list(s: str) -> list: """, stickers=""" Stickers + get_suggested_sticker_set_name send_sticker get_custom_emoji_stickers get_message_effects diff --git a/pyrogram/methods/stickers/__init__.py b/pyrogram/methods/stickers/__init__.py index ed06305de2..292408f255 100644 --- a/pyrogram/methods/stickers/__init__.py +++ b/pyrogram/methods/stickers/__init__.py @@ -21,6 +21,7 @@ from .get_custom_emoji_stickers import GetCustomEmojiStickers from .get_message_effects import GetMessageEffects from .get_stickers import GetStickers +from .get_suggested_sticker_set_name import GetSuggestedStickerSetName from .set_sticker_position_in_set import SetStickerPositionInSet from .set_sticker_set_title import SetStickerSetTitle from .upload_sticker_file import UploadStickerFile @@ -32,6 +33,7 @@ class Stickers( GetCustomEmojiStickers, GetMessageEffects, GetStickers, + GetSuggestedStickerSetName, SetStickerPositionInSet, SetStickerSetTitle, UploadStickerFile, diff --git a/pyrogram/methods/stickers/get_suggested_sticker_set_name.py b/pyrogram/methods/stickers/get_suggested_sticker_set_name.py new file mode 100644 index 0000000000..82940e1ff1 --- /dev/null +++ b/pyrogram/methods/stickers/get_suggested_sticker_set_name.py @@ -0,0 +1,52 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import logging + +import pyrogram +from pyrogram import raw + +log = logging.getLogger(__name__) + + +class GetSuggestedStickerSetName: + async def get_suggested_sticker_set_name( + self: "pyrogram.Client", + title: str + ) -> str: + """Use this method to return a suggested name for a new sticker set with a given title. + + .. include:: /_includes/usable-by/users-bots.rst + + Parameters: + title (``str``): + Sticker set title; 1-64 characters. + + Returns: + ``str``: the suggested name on success. + + Raises: + RPCError: In case of Telegram RPCError. + + """ + r = await self.invoke( + raw.functions.stickers.SuggestShortName( + title=title + ) + ) + return r.short_name From cba2a6e8a3c2aae61235acd61651ca890d814664 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 8 May 2025 19:27:55 +0200 Subject: [PATCH 18/19] fixes --- pyrogram/methods/stickers/get_suggested_sticker_set_name.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/methods/stickers/get_suggested_sticker_set_name.py b/pyrogram/methods/stickers/get_suggested_sticker_set_name.py index 82940e1ff1..5c6af87e59 100644 --- a/pyrogram/methods/stickers/get_suggested_sticker_set_name.py +++ b/pyrogram/methods/stickers/get_suggested_sticker_set_name.py @@ -31,7 +31,7 @@ async def get_suggested_sticker_set_name( ) -> str: """Use this method to return a suggested name for a new sticker set with a given title. - .. include:: /_includes/usable-by/users-bots.rst + .. include:: /_includes/usable-by/users.rst Parameters: title (``str``): From 569ad222536b4cec9941012e2f85706d1eed7f0c Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Fri, 25 Jul 2025 09:45:55 +0200 Subject: [PATCH 19/19] Add 1 unknown_error --- compiler/errors/source/400_BAD_REQUEST.tsv | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/errors/source/400_BAD_REQUEST.tsv b/compiler/errors/source/400_BAD_REQUEST.tsv index a54732bb3a..997413dfe2 100644 --- a/compiler/errors/source/400_BAD_REQUEST.tsv +++ b/compiler/errors/source/400_BAD_REQUEST.tsv @@ -309,6 +309,7 @@ MONOFORUM_FILTER_INVALID The specified filter is invalid. MSG_ID_INVALID Invalid message ID provided. MSG_TOO_OLD [`chat_read_mark_expire_period` seconds](https://core.telegram.org/api/config#chat-read-mark-expire-period) have passed since the message was sent, read receipts were deleted. MSG_WAIT_FAILED A waiting call returned an error. +MTPROTO_CLUSTER_INVALID The mtproto 1049295266_343213 cluster is invalid. MULTI_MEDIA_TOO_LONG Too many media files for album. NEW_SALT_INVALID The new salt is invalid. NEW_SETTINGS_EMPTY No password is set on the current account, and no new password was specified in `new_settings`.