使用 html 在电报机器人上发送粗体和斜体文本

send bold & italic text on telegram bot with html

我在电报中创建了一个机器人

我想将带有 HTML 页面的粗体和斜体文本发送给 bot

我的HTML代码是:

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

如果我发送 *bold*,输出应该是 粗体,但它不起作用

对于斜体,您可以使用 'i' 标签,对于粗体,请尝试使用 'b' 标签

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

加粗:

  1. parse_mode设置为markdown并发送*bold*
  2. parse_mode设置为html并发送<b>bold</b>

发送斜体:

  1. parse_mode设置为markdown并发送_italic_
  2. parse_mode设置为html并发送<i>italic</i>

如果您正在使用 PHP,您可以使用它,我相信它在其他语言中也几乎相似

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

这是您可以使用的所有标签的列表

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

因此,在将消息发送到您使用的电报时:

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

如果我们的消息中有 html 个标签,我们添加 "parse_mode" 以便我们的 url 变为:

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

解析模式可以是"HTML"或"markdown"

要发送粗体、斜体、固定宽度的代码,您可以使用此代码:

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

确保您已将机器人启用为您的管理员。然后只有它可以发送消息

For JS:

首先,如果您还没有安装电报机器人,只需使用命令安装

npm i messaging-api-telegram

现在,用

初始化它的客户端
const client = new TelegramClient({
    accessToken: process.env.<TELEGRAM_ACCESS_TOKEN>
});

然后,使用如下所示的 sendMessage() 异步函数发送消息 -

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

此处的解析模式默认为纯文本,但使用 parseOptions parseMode 我们可以做到 1. "HTML" 和 "MARKDOWN" 让我们以时尚的方式发送消息。还可以从电报页面和聊天 ID 或群聊 ID 获取您的机器人访问令牌。

根据 the docs,您可以将 parse_mode 字段设置为:

  • MarkdownV2
  • HTML

Markdown 模式仍然有效,但它现在被认为是 legacy 模式。请改用 MarkdownV2

您可以像这样传递 parse_mode 参数:

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

对于使用 MarkdownV2 的粗体和斜体:

*bold text*
_italic text_

对于HTML:

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

无论您选择何种格式,请确保对您的查询字符串参数进行编码。例如:

  • Java/Scala(参见note on spaces):
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);