pyrofork: refactor add_sticker_to_set and create_sticker_set

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-08-25 15:39:16 +07:00
parent 8e95ffcd6c
commit 7a063e4a72
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
2 changed files with 68 additions and 16 deletions

View file

@ -22,13 +22,17 @@ import re
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram import types 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: class AddStickerToSet:
async def add_sticker_to_set( async def add_sticker_to_set(
self: "pyrogram.Client", self: "pyrogram.Client",
set_short_name: str, set_short_name: str,
sticker: str, sticker: str,
user_id: Union[int, str] = None,
emoji: str = "🤔", emoji: str = "🤔",
) -> "types.StickerSet": ) -> "types.StickerSet":
"""Add a sticker to stickerset. """Add a sticker to stickerset.
@ -41,7 +45,16 @@ class AddStickerToSet:
sticker (``str``): sticker (``str``):
sticker to add. 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*): emoji (``str``, *optional*):
Associated emoji. Associated emoji.
@ -55,11 +68,20 @@ class AddStickerToSet:
await app.add_sticker_to_set("mypack1", "AsJiasp") await app.add_sticker_to_set("mypack1", "AsJiasp")
""" """
file = None
if isinstance(sticker, str): 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): 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: else:
decoded = FileId.decode(sticker) decoded = FileId.decode(sticker)
media = raw.types.InputDocument( media = raw.types.InputDocument(
@ -68,7 +90,17 @@ class AddStickerToSet:
file_reference=decoded.file_reference file_reference=decoded.file_reference
) )
else: 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( r = await self.invoke(
raw.functions.stickers.AddStickerToSet( raw.functions.stickers.AddStickerToSet(

View file

@ -23,16 +23,17 @@ from typing import Union, Optional
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram import types from pyrogram import types
from pyrogram.file_id import FileId from pyrogram import utils
from pyrogram.file_id import FileId, FileType
class CreateStickerSet: class CreateStickerSet:
async def create_sticker_set( async def create_sticker_set(
self: "pyrogram.Client", self: "pyrogram.Client",
user_id: Union[int, str],
title: str, title: str,
short_name: str, short_name: str,
sticker: str, sticker: str,
user_id: Union[int, str] = None,
emoji: str = "🤔", emoji: str = "🤔",
masks: bool = None masks: bool = None
) -> Optional["types.Message"]: ) -> Optional["types.Message"]:
@ -41,10 +42,6 @@ class CreateStickerSet:
.. include:: /_includes/usable-by/users-bots.rst .. include:: /_includes/usable-by/users-bots.rst
Parameters: 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``): title (``str``):
Stickerset name, 1-64 chars Stickerset name, 1-64 chars
@ -58,6 +55,12 @@ class CreateStickerSet:
sticker to add. 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 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*): emoji (``str``, *optional*):
Associated emoji. Associated emoji.
default to "🤔" default to "🤔"
@ -71,14 +74,23 @@ class CreateStickerSet:
Example: Example:
.. code-block:: python .. code-block:: python
# Send document by uploading from local file
await app.create_sticker_set("me", "My First Pack", "myfirstpack", "AAjjHjk") 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 isinstance(sticker, str):
if os.path.isfile(sticker) or re.match("^https?://", sticker): 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: else:
decoded = FileId.decode(sticker) decoded = FileId.decode(sticker)
media = raw.types.InputDocument( media = raw.types.InputDocument(
@ -87,11 +99,19 @@ class CreateStickerSet:
file_reference=decoded.file_reference file_reference=decoded.file_reference
) )
else: 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( r = await self.invoke(
raw.functions.stickers.CreateStickerSet( raw.functions.stickers.CreateStickerSet(
user_id=await self.resolve_peer(user_id), user_id=await self.resolve_peer(user_id or "me"),
title=title, title=title,
short_name=short_name, short_name=short_name,
stickers=[ stickers=[