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

Moh*_*ein 26 html sendmessage telegram telegram-bot

我用电报创建了一个机器人

我想将带有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>
Run Code Online (Sandbox Code Playgroud)

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

Maa*_*aak 55

有两种可能性:大胆

  1. 设置parse_modemarkdown并发送*bold*
  2. 设置parse_modehtml并发送<b>bold</b>

  • 斜体字的_italic_。 (6认同)
  • 为了使其不那么抽象: sendMessage(userid, message, { parse_mode: "HTML" }); (3认同)
  • @穆拉德阿尔姆。这些是 telegram 支持的格式选项:https://core.telegram.org/bots/api#formatting-options (2认同)

Rez*_*hek 11

如果你使用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;
Run Code Online (Sandbox Code Playgroud)

以下是您可以使用的所有标记的列表

<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>
Run Code Online (Sandbox Code Playgroud)


And*_*res 11

根据文档,您可以将该parse_mode字段设置为:

  • MarkdownV2
  • 超文本标记语言

Markdown模式仍然有效但现在被认为是一种传统模式。请改用MarkdownV2

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

https://api.telegram.org/bot[yourBotKey]/sendMessage?chat_id=[yourChatId]&parse_mode=MarkdownV2&text=[yourMessage]
Run Code Online (Sandbox Code Playgroud)

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

*bold text*
_italic text_
Run Code Online (Sandbox Code Playgroud)

对于 HTML:

<b>bold</b> or <strong>bold</strong>
<i>italic</I> or <em>italic</em>
Run Code Online (Sandbox Code Playgroud)

无论您选择哪种格式,请务必对查询字符串参数进行编码。例如:

val message = "*bold text*";
val encodedMsg = URLEncoder.encode(message, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
var message = "*bold text*"
var encodedMsg = encodeURIComponent(message)
Run Code Online (Sandbox Code Playgroud)
$message = "*bold text*";
$encodedMsg = urlencode($message);
Run Code Online (Sandbox Code Playgroud)


小智 5

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

$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");
Run Code Online (Sandbox Code Playgroud)

如果我们的消息中有 html 标签,我们添加“parse_mode”,这样我们的 url 就变成:

$response = file_get_content($url."\sendMessage?chat_id=$chat_id&text=$text&parse_mode=html")
Run Code Online (Sandbox Code Playgroud)

解析模式可以是“HTML”或“markdown”