You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Telegram机器人输出monospace等宽格式文本?

Telegram机器人输出Monospace等宽文本的正确方法

你的代码没生效是因为缺少格式解析参数——Telegram Bot默认不会自动解析消息里的Markdown语法,必须明确指定parse_mode参数。

正确实现方式

在bot.reply_to方法中添加parse_mode参数,指定使用Markdown格式解析:

方法1:使用官方推荐的MarkdownV2格式

@bot.message_handler(commands=['test_monospace'])
def test_monospace(message):
    bot.reply_to(message, "```This message should be in monospace```", parse_mode="MarkdownV2")

方法2:使用旧版Markdown格式(兼容部分场景)

@bot.message_handler(commands=['test_monospace'])
def test_monospace(message):
    bot.reply_to(message, "```This message should be in monospace```", parse_mode="Markdown")

注意事项

如果使用MarkdownV2,消息中的特殊字符(如_、*、[等)需要用反斜杠转义,但单纯用三个反引号包裹等宽文本时无需额外转义,直接使用即可。

内容的提问来源于stack exchange,提问作者Robb1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:07:08