send bold & italic text on telegram bot with html

HtmlSendmessageTelegramTelegram Bot

Html Problem Overview


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

Html Solutions


Solution 1 - Html

To send bold:

  1. Set the parse_mode to markdown and send *bold*
  2. Set the parse_mode to html and send <b>bold</b>

To send italic:

  1. Set the parse_mode to markdown and send _italic_
  2. Set the parse_mode to html and send <i>italic</i>

Solution 2 - Html

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>

Solution 3 - Html

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

  • MarkdownV2
  • HTML

The Markdown mode still works but it's now considered a legacy one. Use MarkdownV2 instead.

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);

Solution 4 - Html

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"

Solution 5 - Html

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

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

Solution 6 - Html

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

Solution 7 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohammad HosseinView Question on Stackoverflow
Solution 1 - HtmlMaakView Answer on Stackoverflow
Solution 2 - HtmlReza ShekView Answer on Stackoverflow
Solution 3 - HtmlAndresView Answer on Stackoverflow
Solution 4 - HtmlOliver ManyasaView Answer on Stackoverflow
Solution 5 - HtmlRobbin van der JagtView Answer on Stackoverflow
Solution 6 - HtmlSourabh SInhaView Answer on Stackoverflow
Solution 7 - HtmlHimanshu JoshiView Answer on Stackoverflow