Skip to content
Advertisement

Telegram bot sendMessage parse_mode generates HTTP/1.1 400 Bad Request

Using the HTTP API of telegram to send message in PHP like the following:

$msg = "New Comment is **added** from:n".$domain;
    file_get_contents('https://api.telegram.org/botTOKEN:CODE/sendMessage?chat_id=@MYCHANNEL&text='.$msg.'&parse_mode=markdown');

Just adding the last parameter &parse_mode=markdown causes 400 bad request. Removing it, everything works fine.

However, removing n from the $msg string makes it works fine.

Now, I don’t know why the new line n leads to 400 bad request. Also, I don’t know how to add new line to the message body!

Advertisement

Answer

urlencode() the msg;

$msg = urlencode("New Comment is **added** from:n" . "mydomain");

enter image description here

The url is ended after the newline was found, therefore PHP cant send the request, resulting in a 400.

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