send bold & italic text on telegram bot with html
Asked Answered
B

8

74

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

Brandie answered 30/6, 2016 at 9:46 Comment(3)
Are you sure putting your token inside a form?Worldbeater
1. You have a typo: parse_modE. 2. You shouldn't use such code on public websites, because any client visiting a website downloads its HTML source code, so everybody will see your telegram bot tokenDoctorate
A detail blog: sforsuresh.in/telegram-bot-message-formattingUpu
R
134

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>
Renascent answered 30/6, 2016 at 10:48 Comment(3)
what about underlined text for markdown parsing please?Neoplatonism
@MuradAlm. these are the formatting options supported by telegram: core.telegram.org/bots/api#formatting-optionsJohann
To make it less abstract: sendMessage(userid, message, { parse_mode: "HTML" });Amitosis
P
35

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>
Prudenceprudent answered 24/6, 2017 at 7:22 Comment(2)
@Mo. it does work only if you set the parse mode into "html".Inventive
@MarcoConcas That is correct, as these are HTML tags!Prudenceprudent
J
12

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);
Johann answered 9/2, 2021 at 15:4 Comment(0)
U
5

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"

Unerring answered 30/10, 2018 at 6:7 Comment(0)
T
4

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

Tailrace answered 16/12, 2022 at 15:19 Comment(0)
P
3

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

Phylis answered 3/3, 2020 at 9:35 Comment(4)
what library to install for this? python-telegram-bot ? that didn't work.Caucasia
pip install python-telegram-botPhylis
This works for me even today,try again if you have problem then I will tell versionPhylis
perhaps it depends on how you access the API. i was using api.telegram.org/bot<api token>/"sendMessage?<different methods>. hence parse_mode=MARKDOWN&chat_id={}&text={}".format(chat_id, msg) in the method worked for me when posting the message.Caucasia
S
2

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

    <i> italic </i>
    <b> bold </b>
Snafu answered 30/6, 2016 at 9:49 Comment(3)
please read this page: core.telegram.org/bots/api#markdown-styleBrandie
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: <b>bold</b>, <strong>bold</strong> <i>italic</i>, <em>italic</em> <a href="URL">inline URL</a> <code>inline fixed-width code</code> <pre>pre-formatted fixed-width code block</pre>"Snafu
i change parse_mod to HTML and i send <b>bold</b> <bold>bold</bold> output is: <b>bold</b> <bold>bold</bold> i want output is boldBrandie
M
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.

Myasthenia answered 22/9, 2020 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.