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
Put “\n” inside your string if you want a new line.
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.
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.
I got it working.
Instead of sending \n
to Telegram, you need to send %0A
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With