How to send stickers using the id (string) in telethon

Question:

Is there any method to send stickers using the id attribute of event.file? The documentation points out to send it using the id and the access_hash of a particular sticker set and then finally using index with sticker_set to send the sticker. Since there’s unique id for particular sticker stored in telegram servers, I was wondering if there’s any method of using that to send stickers?

Asked By: Dominic

||

Answers:

from telethon.tl.functions.messages import GetAllStickersRequest
sticker_sets = await client(GetAllStickersRequest(0))

# Choose a sticker set
from telethon.tl.functions.messages import GetStickerSetRequest
from telethon.tl.types import InputStickerSetID
sticker_set = sticker_sets.sets[0]

# Get the stickers for this sticker set
stickers = await client(GetStickerSetRequest(
    stickerset=InputStickerSetID(
        id=sticker_set.id, access_hash=sticker_set.access_hash
    )
))

# Stickers are nothing more than files, so send that
await client.send_file('me', stickers.documents[0])

You must send the sticker as an InputDocument, specifying the sticker id, access_hash and file_reference.

from telethon import types

# some code here
await event.reply(
    file = types.InputDocument(
       id = YOUR_STICKER_ID,
       access_hash = YOUR_STICKER_ACCESS_HASH,
       file_reference = YOUR_STICKER_FILE_REFERENCE
     )
)
# more code

For example, reply to "/sticker" new message:

from telethon import events, types

# Reply to new message /sticker
@client.on(events.NewMessage(pattern=r'/sticker'))
   await event.reply(
       file = types.InputDocument(
          id = YOUR_STICKER_ID,
          access_hash = YOUR_STICKER_ACCESS_HASH,
          file_reference = YOUR_STICKER_FILE_REFERENCE
       )    
    )
Answered By: Usuario De Google
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.