pythontelegramemojitelethonaiogram

Using premium emoji on telethon


I have a telegram bot with which the user enters text for sending, and the telethon client sends messages with this text (not spam). But, I encountered a problem: premium emoji are converted into regular ones (even if the client has Telegram Premium on his account). I came across a post in the documentation:

from telethon.extensions import markdown
from telethon import types

class CustomMarkdown:
    @staticmethod
    def parse(text):
        text, entities = markdown.parse(text)
        for i, e in enumerate(entities):
            if isinstance(e, types.MessageEntityTextUrl):
                if e.url == 'spoiler':
                    entities[i] = types.MessageEntitySpoiler(e.offset, e.length)
                elif e.url.startswith('emoji/'):
                    entities[i] = types.MessageEntityCustomEmoji(e.offset, e.length, int(e.url.split('/')[1]))
        return text, entities
    @staticmethod
    def unparse(text, entities):
        for i, e in enumerate(entities or []):
            if isinstance(e, types.MessageEntityCustomEmoji):
                entities[i] = types.MessageEntityTextUrl(e.offset, e.length, f'emoji/{e.document_id}')
            if isinstance(e, types.MessageEntitySpoiler):
                entities[i] = types.MessageEntityTextUrl(e.offset, e.length, 'spoiler')
        return markdown.unparse(text, entities)

client.parse_mode = CustomMarkdown()
client.send_message('me', 'hello this is a [hidden text](spoiler), with custom emoji [❤️](emoji/10002345) !')

Accordingly, when using such emoji, the user must enter them in the form [❤️](emoji/10002345), which in my opinion is a big bummer.

I would like to automatically convert the premium emoji entered into the bot, but I don’t want to use something like:

emojies = {
    "❤️": 10002345,
    . . .
}
my_emodji = emojies['❤️']

A large number of emoji packs will be used and this method is not the most effective. Tell me, how can I automate receiving emoji id?


I tried to use <emoji id=айди_эмодзи>any emoji</emoji> but again, the question arises with getting emoji ID. There are similar threads on Stackoverflow, but they don't answer my question.


Solution

  • I'll leave this post as an answer in case anyone needs this information in the future.

    text = "[🔹](emoji/5388790256772331442)"
    
    me = await client.get_me()
    access_hash = me.access_hash
    
    # search premium emoji in your text
    prem_emoji = await client(functions.messages.SearchCustomEmojiRequest(
        emoticon=text,
        hash=access_hash
    ))
    emoji_ids = prem_emoji.document_id # Premium emoji ids
        
    # return EmojiObject
    emojies = await client(functions.messages.GetCustomEmojiDocumentsRequest(
        document_id=emoji_ids
    ))
    logging.info(emojies)