【发布时间】:2020-02-08 18:47:36
【问题描述】:
Telegram Bot API 4.5 带有新的解析模式 MarkdownV2。同时这些_ * [ ] ( ) ~ > # + - = | { } . ! 字符必须用前面的字符\ 进行转义。
.replace(/[-.+?^$[\](){}\\]/g, '\\$&') 用作添加转义字符的解决方案,效果很好,但不幸的是,此解决方案确实影响超链接方法[inline URL](http://www.example.com/),因为它替换了\[inline URL\]\(http://www.example\.com/\)
解决方案
bot.on('text', (ctx) => {
const { chat } = ctx.message;
const msgs = `Here is the [rules](https://telegra.ph/rules-05-06) Please read carefully and give the details which mentioned below.
*Name:*
*Place:*
*Education:*
*Experience:*
You can also call me on (01234567890)
__For premium service please contact with admin__`;
const msgmsgWithEscape = msgs.replace(/[-.+?^$[\](){}\\]/g, '\\$&')
ctx.telegram.sendMessage(
chat.id,
msgmsgWithEscape,
{
parse_mode: 'MarkdownV2',
}
)
});
结果
【问题讨论】:
标签: regex replace escaping telegram telegram-bot