skip to Main Content

I am writing a telegram bot that needs to know how the text has been stylized by the user (bold, italicized, strikethrough, spoiler, etc.). But it is not clear to me how this can be done or if it is even possible. I am using python and the python-telegram-bot package. I have figured out how to send stylized messages by having the Dispatcher parse the message being sent to the user in markdown. But how can I do the same thing receiving messages?

To make a very simple example, let’s take this bot that just has a "start" command.

def start_command(update, context):
    update.message.reply_text(context.args) # reply message back to the user

def main():
    defaults = Defaults(parse_mode='MarkdownV2')

    updater = Updater(API_KEY, use_context=True, defaults=defaults)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start_command))

    updater.start_polling(0)
    updater.idle()

How can I make it such that it knows the argument was bolded, italicized, etc. by the user?

To make it clear, I want to be able to distinguish between:

  • /start hello
  • /start hello
  • /start hello

2

Answers


  1. Depending on your use case, there several things that you can use one of the following:

    • Message.entities is a list of MessageEntity objects that tell you how the text is formatted. However, since a MessageEntity only contains info on the offset and length, it’s a bit tricky to get the text that’s formatted by the entity.
    • PTB offers the convenience methods Message.parse_entity and Message.parse_entities. The first accepts one of the elements of Message.entities and returns the text covered by that entity. The second returns a dictionary, where the keys are the elements of Message.enities and the values are the corresponding return values of Message.parse_entity.
    • Finally Message.text_html and Message.text_markdown_v2 are convenience properties that give you the text with the corresponding markup inserted. E.g. message.reply_html(message.text_html) will send a message that’s formatted exactly like message.

    Disclaimer: I’m currently the maintainer of python-telegram-bot.

    Login or Signup to reply.
  2. The method getUpdates used to retrieve messages sent from users allows you to gather all of the mentioned entities inside of a user message.

    Per each Update object containing a Message object you could find easily the related entities like you’re trying to do.

    Following the official Telegram’s Message object representation, each Message object has a field named entities which is an array of MessageEntity.

    Let’s assume that I sent a text message like so:
    "plain text bold text italic text"

    The Update object will be structured as:

    {
    "message": {
        "message_id": 181,
        "from": {
            "id": user_id,
            "is_bot": false,
            "first_name": "first_name",
            "username": "username",
            "language_code": "en"
        },
        "chat": {
            "id": user_id,
            "first_name": "first_name",
            "username": "username",
            "type": "private"
        },
        "date": 1642080625,
        "text": "plain text bold text italic text",
        "entities": [{
            "offset": 11,
            "length": 10,
            "type": "bold"
        },  {
            "offset": 21,
            "length": 11,
            "type": "italic"
        }]
    }
    

    You could associate really easily per each text piece its own entity thanks to the offset, the length of this entity and its type.

    In our case, we could understand that we have a 10 characters bold entity starting from the 11th characters of the entire message.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search