Search code examples
phptelegramtelegram-bot

Telegram REST API, send newline in message text?


I want to send an message via the Telegram API in a <pre> block or ``` (HTML or markdown parse mode, I have no preference).

The text is a long string with some line breaks. To make it easy to read I want to send it as code. The new lines are in the \n format, so the Telegram API can handle that.

But in the code block I can't see the newlines. I've used other bots that can send me some lines in a code block, so I'm reasobaly sure it's possible.

Can somebody help me with this?

This is the code that I'm currently using:

$url = "https://api.telegram.org/$telegram_apikey/sendMessage?chat_id=$telegram_chatid&parse_mode=Markdown&text=```" . $message ."```";
        $telegramResult = file_get_contents($url
);

Where message is something like this:

-------------------------------------------- \n
------------ IMPORT RESULTS ---------------- \n
-------------------------------------------- \n
Product count: 12345 \n
Created: 1234 \n
Total time:  200 \n
-------------------------------------------- \n

Solution

  • I got it working.

    Instead of sending \n to Telegram, you need to send %0A.