Search code examples
pythonpy-telegram-bot-apitelebot

How to make text, that is in variable, bold using pytelegrambotapi


I basically have a variable that contains the string. This is not a fixed string and is being changed by the user, so every time user uses the bot - string changes. My question is how to send a message from the bot to the user making text in variable bold?

For example, my code is

bot.send_message(message.chat.id, "Your number is — " + "<b>" + str(text) + "</b>", parse_mode='HTML')

What I expect to see is

Your number is - 204566234

How can it be done in PyTelegramBotAPI?


Solution

  • For bold: Hello

    from telebot import formatting
    
    bot.send_message(
        message.chat.id,
        formatting.hbold('Hello'),
        parse_mode='HTML'
    )
    

    italic:Hello

    formatting.hitalic('Hello')
    

    underline: H̲e̲l̲l̲o̲:

    formatting.hunderline('Hello')
    

    strikethrough: Hello:

    formatting.hstrikethrough('Hello')
    

    code: Hello

    formatting.hcode('Hello')
    

    If you want parse Markdown, just change parse_mode and replace m with h:

    ...
    formatting.mbold('Hello'),
    parse_mode='MarkdownV2'
    ...
    

    You can see good examples here.