Skip to content

Commit

Permalink
pyrofork: refactor add_sticker_to_set and create_sticker_set
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Aug 25, 2024
1 parent 8e95ffc commit 7a063e4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
42 changes: 37 additions & 5 deletions pyrogram/methods/stickers/add_sticker_to_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import pyrogram
from pyrogram import raw
from pyrogram import types
from pyrogram.file_id import FileId
from pyrogram import utils
from pyrogram.file_id import FileId, FileType

from typing import Union

class AddStickerToSet:
async def add_sticker_to_set(
self: "pyrogram.Client",
set_short_name: str,
sticker: str,
user_id: Union[int, str] = None,
emoji: str = "🤔",
) -> "types.StickerSet":
"""Add a sticker to stickerset.
Expand All @@ -41,7 +45,16 @@ async def add_sticker_to_set(
sticker (``str``):
sticker to add.
Pass a file_id as string to send a file that exists on the Telegram servers.
Pass a file_id as string to add a sticker that exists on the Telegram servers,
pass an HTTP URL as a string for Telegram to get a sticker from the Internet,
pass a file path as string to upload a new sticker that exists on your local machine, or
pass a binary file-like object with its attribute ".name" set for in-memory uploads.
user_id (``int`` | ``str``, *optional*):
Unique identifier (int) or username (str) of the Stickerset owner.
For you yourself you can simply use "me" or "self" (users only).
required for bots.
default to "me".
emoji (``str``, *optional*):
Associated emoji.
Expand All @@ -55,11 +68,20 @@ async def add_sticker_to_set(
await app.add_sticker_to_set("mypack1", "AsJiasp")
"""
file = None

if isinstance(sticker, str):
if self.me.is_bot and user_id is None:
raise ValueError("user_id is required for bots")
if os.path.isfile(sticker) or re.match("^https?://", sticker):
raise ValueError(f"file_id is invalid!")
document = await self.send_document(
user_id or "me",
sticker,
force_document=True,
disable_notification=True
)
uploaded_media = utils.get_input_media_from_file_id(document.document.file_id, FileType.DOCUMENT)
media = uploaded_media.id
_ = await document.delete()
else:
decoded = FileId.decode(sticker)
media = raw.types.InputDocument(
Expand All @@ -68,7 +90,17 @@ async def add_sticker_to_set(
file_reference=decoded.file_reference
)
else:
raise ValueError(f"file_id is invalid!")
if self.me.is_bot and user_id is None:
raise ValueError("user_id is required for bots")
document = await self.send_document(
user_id or "me",
sticker,
force_document=True,
disable_notification=True
)
uploaded_media = utils.get_input_media_from_file_id(document.document.file_id, FileType.DOCUMENT)
media = uploaded_media.id
_ = await document.delete()

r = await self.invoke(
raw.functions.stickers.AddStickerToSet(
Expand Down
42 changes: 31 additions & 11 deletions pyrogram/methods/stickers/create_sticker_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@
import pyrogram
from pyrogram import raw
from pyrogram import types
from pyrogram.file_id import FileId
from pyrogram import utils
from pyrogram.file_id import FileId, FileType


class CreateStickerSet:
async def create_sticker_set(
self: "pyrogram.Client",
user_id: Union[int, str],
title: str,
short_name: str,
sticker: str,
user_id: Union[int, str] = None,
emoji: str = "🤔",
masks: bool = None
) -> Optional["types.Message"]:
Expand All @@ -41,10 +42,6 @@ async def create_sticker_set(
.. include:: /_includes/usable-by/users-bots.rst
Parameters:
user_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the Stickerset owner.
For you yourself you can simply use "me" or "self" (users only).
title (``str``):
Stickerset name, 1-64 chars
Expand All @@ -58,6 +55,12 @@ async def create_sticker_set(
sticker to add.
Pass a file_id as string to send a file that exists on the Telegram servers.
user_id (``int`` | ``str``, *optional*):
Unique identifier (int) or username (str) of the Stickerset owner.
For you yourself you can simply use "me" or "self" (users only).
required for bots.
default to "me".
emoji (``str``, *optional*):
Associated emoji.
default to "🤔"
Expand All @@ -71,14 +74,23 @@ async def create_sticker_set(
Example:
.. code-block:: python
# Send document by uploading from local file
await app.create_sticker_set("me", "My First Pack", "myfirstpack", "AAjjHjk")
"""
file = None

if self.me.is_bot and user_id is None:
raise ValueError("user_id is required for bots")

if isinstance(sticker, str):
if os.path.isfile(sticker) or re.match("^https?://", sticker):
raise ValueError(f"file_id is invalid!")
document = await self.send_document(
user_id or "me",
sticker,
force_document=True,
disable_notification=True
)
uploaded_media = utils.get_input_media_from_file_id(document.document.file_id, FileType.DOCUMENT)
media = uploaded_media.id
_ = await document.delete()
else:
decoded = FileId.decode(sticker)
media = raw.types.InputDocument(
Expand All @@ -87,11 +99,19 @@ async def create_sticker_set(
file_reference=decoded.file_reference
)
else:
raise ValueError(f"file_id is invalid!")
document = await self.send_document(
user_id or "me",
sticker,
force_document=True,
disable_notification=True
)
uploaded_media = utils.get_input_media_from_file_id(document.document.file_id, FileType.DOCUMENT)
media = uploaded_media.id
_ = await document.delete()

r = await self.invoke(
raw.functions.stickers.CreateStickerSet(
user_id=await self.resolve_peer(user_id),
user_id=await self.resolve_peer(user_id or "me"),
title=title,
short_name=short_name,
stickers=[
Expand Down

0 comments on commit 7a063e4

Please sign in to comment.