Patoshi パトシ
Patoshi パトシ

Reputation: 23535

parse_mode with MarkdownV2 for Telegram Bot displaying using PHP

I have the following PHP code with the parse_mode=MarkdownV2 and it isn't posting to the channel I have it linked to. I verified this works, because if I remove the parse_mode it will post to the channel. Do I have some type of formatting error before sending this to the telegram bot?

$realurl = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$encoded = urlencode($realurl ." \n**". $obj->region .' - '. $obj->org .'** - '. $_SERVER['REMOTE_ADDR'] .' Referrer: '. $_SERVER['HTTP_REFERER'] );

file_get_contents($api_path."/sendmessage?chat_id=-152445&parse_mode=MarkdownV2&disable_web_page_preview=1&text=$encoded"); 

Upvotes: 1

Views: 10926

Answers (2)

Patoshi パトシ
Patoshi パトシ

Reputation: 23535

Reason why it doesn't work is because you need to escape the following characters: . - _

Using \. \- \_

This is the error message you get if you submit a message with . in it:

{"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: 
Character '.' is reserved and must be escaped with the preceding '\\'"}

Upvotes: 1

0stone0
0stone0

Reputation: 44172

The Telegram MarkdownV2 Docs shows the following example for bold text:

*bold \*text*

Your code shows:

$realurl ." \n**". $obj->region .' - '. $obj->org .'** - ' .

So you open and close the bold tag right away, therefore nothing is shown in bold.


You need to use a single * instead off the double **!

Upvotes: 1

Related Questions