Skip to content
Advertisement

Telegram API MarkdownV2 Escape Characters Bug

I am new to this. I am writing a telegram bot that interacts with Google Sheets in Google App Script. I am having an issue with MarkdownV2.

My code has one before the ‘!’ as Telegram said in their document:

‘!’ must be escaped with the preceding character

var send = url + "/sendMessage?chat_id=" + id + "&text=" + "Hey! New item" + "&parse_mode=MarkdownV2";

However, it says I need to use two \ instead.

Exception: Request failed for https://api.telegram.org returned code 400. Truncated server response: {"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: Character '!' is reserved and must be escaped with the preceding '\'"} (use muteHttpExceptions option to examine full response)

But when I use two \ I still have a problem running.

Exception: Invalid argument: https://api.telegram.org/botxyz:abc/sendMessage?chat_id=xyz&text=Hey!%20New%20item&parse_mode=MarkdownV2

When I run the link https://api.telegram.org/botxyz:abc/sendMessage?chat_id=xyz&text=Hey!%20New%20item&parse_mode=MarkdownV2 in the error, it connects to Telegram normally.

Thank you.

Advertisement

Answer

In your script, how about the following modification?

Modified script:

var url = "https://api.telegram.org/botxyz:abc";
var id = "xyz";
var send = encodeURI(url + "/sendMessage?chat_id=" + id + "&text=" + "Hey\! New item" + "&parse_mode=MarkdownV2");
console.log(send)
  • I thought that in your situation ! might be \!.

  • When this script is run, https://api.telegram.org/botxyz:abc/sendMessage?chat_id=xyz&text=Hey%5C!%20New%20item&parse_mode=MarkdownV2 can be seen in the log. This value is the same as the URL you tested at the bottom of your question.

Reference:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement