'Get sticker set by link/shot name[pyrogram]
I want to send a random sticker form sticker pack link.
To send a sticker I need file_id
I want to get a every sticker file_id form sticker set, but I don't know how to get a sticker set by a short name/link
So basically I need a function that gets short name/link and return a random file_id of sticker in sticker set
sticker_sets = [] # empty set of stickers sets that I will fill in
sticker_set_names = set() # set or list of stickers set shot names
with Client("session", api_id, api_hash) as app:
for sticker_name in sticker_set_names:
sticker_set = app.send(
GetStickerSet(
stickerset=InputStickerSetShortName(short_name=sticker_name)
)
)
sticker_sets.append(sticker_set)
Main question
So how do I get random file_id of sticker from sticker set.
Solution 1:[1]
This is the solution I found, this code will send a random sticker to the saved messages.
import random
from pyrogram import Client
from pyrogram.raw.functions.messages import GetStickerSet, SendMedia
from pyrogram.raw.types import InputStickerSetShortName, InputDocument, InputMediaDocument, InputPeerSelf
sticker_sets = [] # empty set of stickers sets that I will fill in
sticker_set_names = set() # set or list of stickers set shot names
with Client("session", api_id, api_hash) as app:
for sticker_name in sticker_set_names:
sticker_set = app.invoke(
GetStickerSet(
stickerset=InputStickerSetShortName(short_name=sticker_name),
hash=0
)
)
sticker_sets.append(sticker_set)
sticker = random.choice(random.choice(sticker_sets).documents)
input_document = InputDocument(id=sticker.id,
access_hash=sticker.access_hash,
file_reference=sticker.file_reference)
input_media_document = InputMediaDocument(id=input_document)
app.invoke(SendMedia(
peer=InputPeerSelf(),
media=input_media_document,
message="",
random_id=random.randint(-(2 ^ 63), 2 ^ 63 - 1)
))
You can change who you send the message to by filling in the SendMedia options. Maybe changing InputPeerSelf to InputPeerChat will be more suitable for you. You can find other InputPeer types in the documentation.
PS: I am using pyrogram version 2.0.19. The send method has now been renamed to invoke.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BatMan |