在 Telegram Bot API 中,可以使用 HTML 标记来格式化消息。要在 HTML 中插入换行符,请使用 '
' 标记。例如,以下代码将在消息中插入两个换行符:

import telegram

bot = telegram.Bot(token='YOUR_TOKEN_HERE')

message = 'This is the first line.<br>This is the second line.'
bot.send_message(chat_id='YOUR_CHAT_ID', text=message, parse_mode=telegram.ParseMode.HTML)

在上面的示例中,message 变量包含两个行,每个行之间有一个 '
' 标记。parse_mode 参数设置为 telegram.ParseMode.HTML,以告诉 Telegram 解析 HTML 标记。这将导致 Telegram 将 '
' 标记解释为换行符。

请注意,如果你尝试在 Markdown 模式下使用 '
' 标记,则将会被视为普通文本而不是换行符。因此,确保在 parse_mode 参数中正确设置模式。

Telegram Bot 发送 HTML 消息:如何实现换行

原文地址: https://www.cveoy.top/t/topic/oQpn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录