Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram does not escape some markdown characters

Telegram does not escape some markdown characters, for example:

  • This works fine

    _test\_test_

  • But this return parse error

    *test\*test*

What I do wrong?

like image 876
user3449979 Avatar asked Nov 16 '16 08:11

user3449979


People also ask

How do you escape special characters in markdown?

The way to escape a special character is to add a backslash before it, e.g., I do not want \_italic text\_ here . Similarly, if # does not indicate a section heading, you may write \# This is not a heading . As mentioned in Section 4.12, a sequence of whitespaces will be rendered as a single regular space.

Does Telegram support markdown?

Telegram supports styled text using message entities. A client that wants to send styled messages would simply have to integrate a Markdown/HTML parser, and generate an array of message entities by iterating through the parsed tags.


2 Answers

String escapedMsg = toEscapeMsg     .replace("_", "\\_")     .replace("*", "\\*")     .replace("[", "\\[")     .replace("`", "\\`"); 

Do not escape ] character. If [ is escaped, ] is treated like a normal character.

like image 154
pdenti Avatar answered Sep 27 '22 21:09

pdenti


The only workaround is to use HTML in the parse_mode

https://core.telegram.org/bots/api#html-style

like image 40
Oleg Avatar answered Sep 27 '22 22:09

Oleg