Escaping underline in telegram api when parse_mode = Markdown

Viewed 5596

How can I send this text correctly:

$parameters['text'] = 'you must see [example](example.com) or contact with @exmaple_com';

if I don't use "Markdown", telegram don't show the above link if I use "Markdown", telegram can't handle underline.

2 Answers

When you set your parse_mode on Markdown or MarkdownV2, you can't use these characters directly: ()._-

You should escape them using backslash,

Also, you should escape backslash itself. for example, in Golang I wrote this function to solve my problem:

func FmtTelegram(input string) string {
  return strings.NewReplacer(
    "(", "\\(", // ()_-. are reserved by telegram.
    ")", "\\)",
    "_", "\\_",
    ".", "\\.",
    "-", "\\-",
  ).Replace(input)
}

And in PHP you should escape like this:

$parameters['text'] = '\\_com';
# or
$parameters['text'] = '\\.com';
# or
$parameters['text'] = '\\-com';
# or
$parameters['text'] = '\\(com\\)';
Related