Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 965
NVO Avatar asked Apr 25 '18 09:04

NVO


People also ask

How do I separate paragraphs in Telegram?

Put “\n” inside your string if you want a new line.

What is inline keyboard in Telegram?

Inline keyboards are available for messages sent both in chat mode and inline mode. Unlike with custom reply keyboards, pressing buttons on inline keyboards doesn't result in messages sent to the chat.

Can I send HTML in Telegram?

You can use bold and italic text, as well as inline links and pre-formatted code in your bots' messages. Telegram clients will render them accordingly. You can use either markdown-style or HTML-style formatting.


2 Answers

I got it working.

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

like image 63
NVO Avatar answered Sep 16 '22 22:09

NVO


I see you've found a solution but you might be better off using urlencode to encode your $message.

This should convert your newlines to %0A as well as converting any other illegal or potentially dangerous characters like &, # or ? if they appear in your message.

like image 44
Azquelt Avatar answered Sep 20 '22 22:09

Azquelt