[html] send bold & italic text on telegram bot with html

I've created a bot in telegram

I want to send bold and italic text with HTML page to bot

My HTML code is:

<html>
<head><title>Telegram</title></head>
<body>
    <form method="GET" action="https://api.telegram.org/bot(token)/sendMessage">
        <input type="hidden" name="chat_id" value="@testadminch">
        <input type="hidden" name="parse_mod" value="markdown">
        <textarea name="text"></textarea>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

If I send *bold* the output should be bold but it doesn't work

This question is related to html sendmessage telegram telegram-bot

The answer is


So when sending the message to telegram you use:

$token = <Enter Your Token Here>
$url = "https://api.telegram.org/bot".$token;

$chat_id = <The Chat Id Goes Here>;
$test = <Message goes Here>;

//sending Message normally without styling
$response = file_get_content($url."\sendMessage?chat_id=$chat_id&text=$text");

If our message has html tags in it we add "parse_mode" so that our url becomes:

$response = file_get_content($url."\sendMessage?chat_id=$chat_id&text=$text&parse_mode=html")

parse mode can be "HTML" or "markdown"


To Send bold,italic,fixed width code you can use this :

# Sending a HTML formatted message
bot.send_message(chat_id=@yourchannelname, 
             text="*boldtext* _italictext_ `fixed width font` [link]   (http://google.com).", 
             parse_mode=telegram.ParseMode.MARKDOWN)

make sure you have enabled the bot as your admin .Then only it can send message


For italic you can use the 'i' tag, for bold try the 'b' tag

_x000D_
_x000D_
    <i> italic </i>_x000D_
    <b> bold </b>
_x000D_
_x000D_
_x000D_


For JS:

First, if you haven't installed telegram bot just install with the command

npm i messaging-api-telegram

Now, initialize its client with

const client = new TelegramClient({
    accessToken: process.env.<TELEGRAM_ACCESS_TOKEN>
});

Then, to send message use sendMessage() async function like below -

    const resp = await client.sendMessage(chatId, msg, {
        disableWebPagePreview: false,
        disableNotification: false,
        parseMode: "HTML"
    });

Here parse mode by default would be plain text but with parseOptions parseMode we can do 1. "HTML" and "MARKDOWN" to let use send messages in stylish way. Also get your access token of bot from telegram page and chatId or group chat Id from same.


If you are using PHP you can use this, and I'm sure it's almost similar in other languages as well

$WebsiteURL = "https://api.telegram.org/bot".$BotToken;
$text = "<b>This</b> <i>is some Text</i>";
$Update = file_get_contents($WebsiteURL."/sendMessage?chat_id=$chat_id&text=$text&parse_mode=html);

echo $Update;

Here is the list of all tags that you can use

<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<a href="http://www.example.com/">inline URL</a>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>

According to the docs you can set the parse_mode field to:

  • MarkdownV2
  • HTML

Markdown still works but it's now considered a legacy mode.

You can pass the parse_mode parameter like this:

https://api.telegram.org/bot[yourBotKey]/sendMessage?chat_id=[yourChatId]&parse_mode=MarkdownV2&text=[yourMessage]

For bold and italic using MarkdownV2:

*bold text*
_italic text_

And for HTML:

<b>bold</b> or <strong>bold</strong>
<i>italic</I> or <em>italic</em>

Make sure to encode your query-string parameters regardless the format you pick. For example:

val message = "*bold text*";
val encodedMsg = URLEncoder.encode(message, "UTF-8");
  • Javascript (ref)
var message = "*bold text*"
var encodedMsg = encodeURIComponent(message)
$message = "*bold text*";
$encodedMsg = urlencode($message);