Send premium-emoji with Pyrogram

Question:

I need to send a premium emoji on the user’s account using Pyrogram. I tried to send with send_message() a list of MessageEntityCustomEmoji and MessageEntity. The first one gave the error 'MessageEntityCustomEmoji' object has no attribute '_client', and the second one sent a message without an emoji. How do I send them without errors?

Asked By: otobrazhaemoe_imya

||

Answers:

After lots of research and suffering, the answer was:

...

my_emoji_str = "<emoji id=5310129635848103696>✅</emoji> And this is custom emoji in the text"
await app.send_message(message.chat.id, my_emoji_str)

This is basically a text formatting, here it is in HTML ParseMode of Pyrogram, but Pyrogram supports both Markdown and HTML formatting simultaneously out-of-the-box, and you also don’t need to specify it in the parse_mode argument.

And the id is the custom_emoji_id that you can find in the message object if you don’t know it yet. To know what to look for:

...
"text": "✅ Hello",
"entities": [
    {
        "_": "MessageEntity",
        "type": "MessageEntityType.CUSTOM_EMOJI",
        "offset": 0,
        "length": 1,
        "custom_emoji_id": 5310129635848103696 # <-- here you go
    }
],
...

And.. That’s it!

Answered By: Daniil_VV
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.