发送大胆&带有html的电报机器人上的斜体文字

时间:2016-06-30 09:46:07

标签: 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>

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

7 个答案:

答案 0 :(得分:46)

有两种可能性:粗体

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

答案 1 :(得分:7)

如果你使用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>

答案 2 :(得分:3)

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

  • MarkdownV2
  • HTML

Markdown 仍然有效,但现在被认为是一种传统模式

您可以像这样传递 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>

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

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

答案 3 :(得分:2)

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

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

请确保您已将漫游器设为管理员。然后只有它可以发送消息

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

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

$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”,以便我们的网址变为:

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

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

答案 6 :(得分:0)

For JS:

首先,如果您尚未安装电报bot,则只需使用命令进行安装

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”允许以时尚的方式使用发送消息。还可以从电报页面获取您的漫游器访问令牌,并从中获取chatId或群聊ID。

相关问题