skip to Main Content

I can’t figure out how to format the text in the Telegram bot.

The code is the following:

import telebot

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start', 'help'])
def help(message: telebot.types.Message):
    text = "<b> Test message </b>"
    bot.reply_to(message, text)

Where should I put parse_mode='HTML'?

I tried:

1.bot.reply_to(message, text, parse_mode='HTML')

2.bot = telebot.TeleBot(TOKEN, parse_mode='HTML')

I want the bot to format the message: make individual words or sentences bold, italic or underlined.

2

Answers


  1. You can use the formatting package from Telebot to format your text like this:

    from telebot import formatting
    
    formatting.hbold("hello")
    formatting.hitalic("world")
    formatting.hunderline("!")
    

    See the pytba docs for all formatting styles.

    Login or Signup to reply.
  2. The default parse_mode can be passed to the TeleBot class like so:

    bot = telebot.TeleBot("TOKEN", parse_mode='MARKDOWN')
    

    If you want to set it for a single message, send_message also excepts parse_mode:

    bot.send_message(message.chat.id, '<b>some text</b>', parse_mode='HTML')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search