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
有两种可能性:大胆
parse_mode为markdown并发送*bold*parse_mode为html并发送<b>bold</b>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字段设置为:
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”