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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38119481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:29:34  来源:igfitidea点击:

send bold & italic text on telegram bot with html

htmlsendmessagetelegramtelegram-bot

提问by Mohammad Hossein

I've created a bot in telegram

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

I want to send bold and italic text with HTML page to bot

我想将带有 HTML 页面的粗体和斜体文本发送到机器人

My HTML code is:

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

If I send *bold*the output should be boldbut it doesn't work

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

回答by Maak

There are two possibilities to get: bold

有两种可能得到:粗体

  1. Set the parse_modeto markdownand send *bold*
  2. Set the parse_modeto htmland send <b>bold</b>
  1. 设置parse_modemarkdown并发送*bold*
  2. 设置parse_modehtml并发送<b>bold</b>

回答by Reza Shek

If you are using PHP you can use this, and I'm sure it's almost similar in other languages as well

如果您使用的是 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;

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>

回答by Oliver Manyasa

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:

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

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

parse mode can be "HTML" or "markdown"

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

回答by Robbin van der Jagt

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

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

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

回答by Sourabh Sinha

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

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