T
T
Ternick2021-06-16 18:14:05
Python
Ternick, 2021-06-16 18:14:05

How to restore markup from entities?

Hello !

An example of a message object from the telebot library:

spoiler
{
    "content_type":"text",
    "message_id":15718,
    "from_user":{
        "id":429037951,
        "is_bot":false,
        "first_name":"Ternick",
        "username":"Ternick",
        "last_name":"None",
        "language_code":"ru"
    },
    "date":1623856179,
    "chat":{
        "type":"group",
        "last_name":"None",
        "first_name":"None",
        "username":"None",
        "id":-598817293,
        "title":"TEst",
        "all_members_are_administrators":true,
        "photo":"None",
        "description":"None",
        "invite_link":"None",
        "pinned_message":"None",
        "sticker_set_name":"None",
        "can_set_sticker_set":"None"
    },
    "forward_from_chat":"None",
    "forward_from":"None",
    "forward_date":"None",
    "reply_to_message":"None",
    "edit_date":"None",
    "media_group_id":"None",
    "author_signature":"None",
    "text":"/set_news CLICK",
    "entities":[
        <telebot.types.MessageEntity object at 0x0000029698813A58>,
        <telebot.types.MessageEntity object at 0x00000296988138D0>
    ],
    "caption_entities":"None",
    "audio":"None",
    "document":"None",
    "photo":"None",
    "sticker":"None",
    "video":"None",
    "video_note":"None",
    "voice":"None",
    "caption":"None",
    "contact":"None",
    "location":"None",
    "venue":"None",
    "new_chat_member":"None",
    "new_chat_members":"None",
    "left_chat_member":"None",
    "new_chat_title":"None",
    "new_chat_photo":"None",
    "delete_chat_photo":"None",
    "group_chat_created":"None",
    "supergroup_chat_created":"None",
    "channel_chat_created":"None",
    "migrate_to_chat_id":"None",
    "migrate_from_chat_id":"None",
    "pinned_message":"None",
    "invoice":"None",
    "successful_payment":"None",
    "connected_website":"None",
    "json":{
        "message_id":15718,
        "from":{
            "id":429037951,
            "is_bot":false,
            "first_name":"Ternick",
            "username":"Ternick",
            "language_code":"ru"
        },
        "chat":{
            "id":-598817293,
            "title":"TEst",
            "type":"group",
            "all_members_are_administrators":true
        },
        "date":1623856179,
        "text":"/set_news CLICK",
        "entities":[
            {
                "offset":0,
                "length":9,
                "type":"bot_command"
            },
            {
                "offset":10,
                "length":5,
                "type":"text_link",
                "url":"https://google.com/"
            }
        ]
    }
}


Everything would be fine, but in entities the offset and length parameters are considered very strange, with plain text everything is fine, but when emoji appear in the text, they are counted as 2 characters, is there an easy way to restore markup by entities, without your bicycles?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question