How to add markdown formatting to photo caption using Telegraf (Telegram Bot Framework for Node.js)

Viewed 1515

I'm using Telegraf to build a Node.js Telegram bot.

When I try to send a photo I use:

const bot = new Telegraf(process.env.BOT_TOKEN);
bot.on('text', (ctx) => ctx.replyWithPhoto(
  { url: 'https://i.picsum.photos/id/237/200/300.jpg' },
  { caption: 'This is *Bobby*!' }
));
bot.launch();

The resulting message includes the photo along with a plain text caption:

This is *Bobby*!

How do I make the caption to look formatted with markdown? Like this:

This is Bobby!

1 Answers

You'll need to supply parse_mode in your second parameter. Possible options currently are html, Markdown and MarkdownV2 (for more details, see here).

In your example, this would be:

bot.on('text', (ctx) => ctx.replyWithPhoto(
  { url: 'https://i.picsum.photos/id/237/200/300.jpg' },
  {
    caption: 'This is *Bobby*!',
    parse_mode: 'MarkdownV2'
  }
));
Related