69

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

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Mohammad Hossein
  • 709
  • 1
  • 6
  • 7

8 Answers8

126

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>
Klesun
  • 12,280
  • 5
  • 59
  • 52
Maak
  • 4,720
  • 3
  • 28
  • 39
34

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>
Reza Shek
  • 581
  • 1
  • 8
  • 18
11

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);
Andres
  • 1,090
  • 14
  • 30
4

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"

2

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

    <i> italic </i>
    <b> bold </b>
  • please read this page: https://core.telegram.org/bots/api#markdown-style – Mohammad Hossein Jun 30 '16 at 09:52
  • It shows both html mode, and markdown mode. Since the first option wasn't working for you I suggested the other one "To use this mode, pass HTML in the parse_mode field when using sendMessage. The following tags are currently supported: bold, bold italic, italic inline URL inline fixed-width code
    pre-formatted fixed-width code block
    "
    – Robbin van der Jagt Jun 30 '16 at 09:56
  • i change parse_mod to HTML and i send `bold bold` output is: `bold bold` i want output is **bold** – Mohammad Hossein Jun 30 '16 at 10:01
2

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

nofoobar
  • 2,826
  • 20
  • 24
  • what library to install for this? `python-telegram-bot` ? that didn't work. – Pagol May 30 '20 at 10:31
  • pip install python-telegram-bot – nofoobar May 31 '20 at 11:04
  • This works for me even today,try again if you have problem then I will tell version – nofoobar May 31 '20 at 11:05
  • perhaps it depends on how you access the API. i was using `api.telegram.org/bot/"sendMessage?`. hence `parse_mode=MARKDOWN&chat_id={}&text={}".format(chat_id, msg)` in the method worked for me when posting the message. – Pagol May 31 '20 at 15:25
1

Here you have all options from core.telegram.org/bots/api#markdown-style

<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<span class="tg-spoiler">spoiler</span>, <tg-spoiler>spoiler</tg-spoiler>
<b>bold <i>italic bold <s>italic bold strikethrough <span class="tg-spoiler">italic bold strikethrough spoiler</span></s> <u>underline italic bold</u></i> bold</b>
<a href="http://www.example.com/">inline URL</a>
<a href="tg://user?id=123456789">inline mention of a user</a>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>

And the result looks like: results_telegram

Antoine
  • 1,393
  • 4
  • 20
  • 26
Luis l
  • 43
  • 5
0

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.