如何通过HTTP POST请求设置Telegram消息为等宽字体?
如何通过Telegram API原生HTTP请求发送等宽字体消息
要实现等宽字体,你需要利用Telegram API支持的格式化解析模式,通过指定parse_mode参数并使用对应语法包裹文本即可,以下是两种可行的方案:
方案1:使用MarkdownV2格式
Telegram的MarkdownV2语法中,用单个反引号`包裹的文本会被渲染为等宽字体。你需要在请求中添加parse_mode=MarkdownV2参数。
示例HTTP请求(URL参数方式)
POST /bot123:token123/sendMessage?chat_id=123&text=`hello`&parse_mode=MarkdownV2
注意:如果文本包含MarkdownV2特殊字符(如
_、*、[等),需要用反斜杠\转义,比如要发送hello_world,需将文本写为`hello\_world`。
示例HTTP请求(JSON请求体方式)
POST /bot123:token123/sendMessage Content-Type: application/json { "chat_id": 123, "text": "`hello`", "parse_mode": "MarkdownV2" }
方案2:使用HTML格式
HTML模式下,用<code>标签包裹的文本会被渲染为等宽字体,请求中需指定parse_mode=HTML。
示例HTTP请求(URL参数方式)
POST /bot123:token123/sendMessage?chat_id=123&text=<code>hello</code>&parse_mode=HTML
注意:如果文本包含HTML特殊字符(如
<、>、&),需要转换为HTML实体(比如&转成&)。
示例HTTP请求(JSON请求体方式)
POST /bot123:token123/sendMessage Content-Type: application/json { "chat_id": 123, "text": "<code>hello</code>", "parse_mode": "HTML" }
内容的提问来源于stack exchange,提问作者rare77
相关产品推荐
相关产品推荐

