Telegram Bot API 4.5 Markdown V2上的转义字符给超级链接带来麻烦 [英] Escaped character on Telegram Bot API 4.5 MarkdownV2 gives trouble for hyper link

查看:34
本文介绍了Telegram Bot API 4.5 Markdown V2上的转义字符给超级链接带来麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TelegramBot API 4.5附带了新的解析模式Markdown V2。同时,这些_ * [ ] ( ) ~ > # + - = | { } . !字符必须用前面的字符进行转义。

.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',
    }
  )
});

结果

推荐答案

为了避免转义[...](http...)格式的链接,您可以匹配它们并捕获到一个组中,然后只匹配所有字符以在其他上下文中转义。然后,检查Group 1值,如果不为空,则替换为Group 1值,否则,替换为转义字符:

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
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(/([[^][]*](http[^()]*))|[_*[]()~>#+=|{}.!-]/gi,
    (x,y) => y ? y : '\' + x)

console.log(msgmsgWithEscape);

这篇关于Telegram Bot API 4.5 Markdown V2上的转义字符给超级链接带来麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆