tegularis
tegularis

Reputation: 69

Telethon client doesn`t send premium emojies

I need to send a message with premium emoji to a given entity using Telethon client. Telegram account connected to client has active premium subscription. Below is my python code:

await client.send_message(
        entity="me",
        message='<tg-emoji emoji-id="5301096984617166561">☝️</tg-emoji>',
        parse_mode="html"
    )

When I run it, instead of premium emoji with given id, regular emoji ☝️ is sent. Why? What is the right way to send premium emoji inside message with html parse mode?

Upvotes: 1

Views: 85

Answers (2)

tegularis
tegularis

Reputation: 69

How to send premium emoji with Telethon client.

  1. Create custom parser:
from telethon.extensions import html
from telethon import types


class CustomHtml:
    @staticmethod
    def parse(text):
        text, entities = html.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 html.unparse(text, entities)
  1. Set Telethon client`s parse mode:
client.parse_mode = CustomHtml()
  1. Following HTML tags will be parsed as premium emoji when sending message if account in use has active premium subscription:
emoji_text = '<a href="emoji/{emoji_id}">{emoji_symbol}</a>'
await client.send_message(
    entity=entity,
    message=emoji_text
)

Upvotes: 0

user35538
user35538

Reputation: 1

I am assuming your code is Python, since no mentions of code language where made at the time I read your question.

Telegram interprets the <tg-emoji> tag and simply renders the corresponding default emoji, ignoring the emoji-id attribute.

  1. Send the message initially with the <tg-emoji> tag. This will result in a regular emoji being sent, but it allows you to get the custom_emoji_id.
  2. Retrieve the custom_emoji_id from the sent message.
  3. Construct the correct HTML using the custom_emoji_id you retrieved in the previous step.
  4. Edit the original message with the correct HTML.

From what I could find out, you need to split the code in more steps to keep the <tg-emoji> tag from overriding other code.

(I can't find out if the emoji id you provided is premium or not since I prefer Signal and hence won't pay for/use Telegram)

Upvotes: 0

Related Questions