How to insert newlines in Telegram bot messages sent via bash?

I’m struggling to add line breaks to Telegram messages I’m sending through a bash script using curl. Here’s what I’ve got so far:

message="<b>Breaking News</b>\nCheck out this story!"

curl -X POST "https://api.telegram.org/botYOUR_BOT_TOKEN/sendMessage" \
     -d "chat_id=YOUR_CHAT_ID" \
     -d "text=$message" \
     -d "parse_mode=HTML"

I’ve tried using \n, <br>, and even %0A, but nothing seems to work. The message always comes out as one long line. Any ideas on how to get those pesky newlines to show up correctly in the Telegram message? I’m pulling my hair out over this!

I encountered a similar issue when working with Telegram bots. The trick that worked for me was using actual newline characters in the message string, not escape sequences.

Try modifying your script like this:

message=$‘Breaking News\nCheck out this story!’

curl -X POST “https://api.telegram.org/botYOUR_BOT_TOKEN/sendMessage
-d “chat_id=YOUR_CHAT_ID”
-d “text=$message”
-d “parse_mode=HTML”

The $‘…’ syntax allows for the interpretation of escape sequences, so \n becomes a real newline. This should solve your problem and provide the correct line breaks in your Telegram messages.

I’ve dealt with this exact issue in my Telegram bot projects. The key is URL encoding. When sending messages via curl, special characters can get mangled. Here’s what worked for me:

message=“Breaking News%0ACheck out this story!”

curl -X POST “https://api.telegram.org/botYOUR_BOT_TOKEN/sendMessage
-d “chat_id=YOUR_CHAT_ID”
-d “text=$message”
-d “parse_mode=HTML”

The %0A is the URL-encoded version of a newline. This approach has been reliable in my experience, even when dealing with complex messages containing multiple line breaks and special characters. Give it a shot and see if it resolves your issue.

hey mate, i feel ur pain. had the same issue b4. try using printf instead of echo when setting ur message. like this:

message=$(printf ‘Breaking News\nCheck out this story!’)

that should do the trick. lemme kno if it works for ya!