How to implement HTML formatting in C# Telegram bot messages

I’m working on a Telegram bot using C# and I need to send messages with HTML formatting. I want to include things like links, bold text, and other HTML elements in my bot responses. I’m using the TelegramBotSharp library for this project.

Here’s what I’m attempting with my current code:

ChatTarget destination = (ChatTarget)notification.Chat ?? notification.From;
if(userInput.StartsWith("Hi")) {
    botClient.SendTextMessage(destination, "Hi <a href='https://example.com'>friend</a>", true);
}

The HTML tags aren’t rendering properly in the chat. What’s the correct way to enable HTML parsing in Telegram bot messages? Do I need to set specific parameters or use a different method?

Your code’s passing true as the third parameter, but that’s not how you set parse mode. You need to explicitly use parseMode: ParseMode.Html instead:

botClient.SendTextMessage(destination, "Hi <a href='https://example.com'>friend</a>", parseMode: ParseMode.Html);

I ran into this exact same issue when I started with Telegram bots. That boolean you’re using is actually for disableWebPagePreview, not parse mode. Make sure you’re using the right method overload. Also heads up - Telegram only supports basic HTML tags like <b>, <i>, <a>, <code>, and <pre>. Bad HTML will just make your message fail silently.

yeah, you got it wrong. that boolean’s not for parse mode, it’s for web preview. just do this:

botClient.SendTextMessage(destination, "Hi <a href='https://example.com'>friend</a>", parseMode: ParseMode.Html);

i messed this up too lol. you gotta tell telegram to parse the html.

You’re not specifying the parse mode correctly. With TelegramBotSharp, you need to use SendTextMessageAsync and set the parseMode parameter to ParseMode.Html:

await botClient.SendTextMessageAsync(destination, "Hi <a href='https://example.com'>friend</a>", parseMode: ParseMode.Html);

I’ve been building Telegram bots for two years and hit this exact problem early on. The library won’t default to HTML even if you throw HTML tags in your message - you have to explicitly tell it. Also heads up: Telegram’s HTML parser is super strict about tag closure and nesting, so double-check your HTML strings before sending.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.