【发布时间】:2017-03-30 08:12:57
【问题描述】:
Telegram 不会转义一些 markdown 字符,例如:
-
这很好用
_test\_test_
-
但是这个返回解析错误
*测试\*测试*
我做错了什么?
【问题讨论】:
-
MarkdownV2core.telegram.org/bots/api#markdownv2-style您是否遇到过类似问题
标签: markdown telegram-bot
Telegram 不会转义一些 markdown 字符,例如:
这很好用
_test\_test_
但是这个返回解析错误
*测试\*测试*
我做错了什么?
【问题讨论】:
MarkdownV2core.telegram.org/bots/api#markdownv2-style您是否遇到过类似问题
标签: markdown telegram-bot
String escapedMsg = toEscapeMsg
.replace("_", "\\_")
.replace("*", "\\*")
.replace("[", "\\[")
.replace("`", "\\`");
不要转义] 字符。如果[ 被转义,] 将被视为普通字符。
【讨论】:
. 尝试 .replace(/\./g, "\\.") 为我工作
实际上两者都出错了。
{
"ok": false,
"error_code": 400,
"description": "Bad Request: Can't parse message text: Can't find end of the entity starting at byte offset 11"
}
听起来 Telegram 不支持 markdown 的转义字符,所以我建议您改用 HTML:
<b>test*test</b>
【讨论】:
test\test,不带'*'。但我想显示粗体test*test...
唯一的解决方法是在parse_mode 中使用HTML
【讨论】:
您应该使用'\\' 转义标记标记*_[`,即改为发送此:
*test\\*test*
【讨论】:
使用MarkdownV2。
{
"chat_id": telegram_chat_id,
"parse_mode": "MarkdownV2",
"text": "*test\*test*"
}
只需转义这些字符:
'_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-' , '=', '|', '{', '}', '.', '!'
然后电报将处理其余的。
【讨论】:
具有讽刺意味的是,如果 prase_mode 参数设置为 markdown 而不是在 python 中使用 ParseMode.MARKDOWN_V2 常量而不转义任何字符 send_message 函数工作正常。
【讨论】:
pdenti 的回答仅替换消息中的 第一个 字符。使用带有全局标签的正则表达式来替换它们。
String escapedMsg = toEscapeMsg
.replace(/_/g, "\\_")
.replace(/\*/g, "\\*")
.replace(/\[/g, "\\[")
.replace(/`/g, "\\`");
【讨论】: