I’m having trouble adding line breaks to Telegram messages sent from my Linux bash script using curl. I’ve tried various methods like <br>, \n, and %0D%0A, but none seem to work. Here’s a simplified version of my code:
message="<b>Hello</b>\nThis is a new line"
curl -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" \
-d "chat_id=${CHAT_ID}" \
-d "text=${message}" \
-d "parse_mode=HTML"
The message appears as one continuous line in Telegram. Any suggestions on how to properly insert line breaks in these messages? I’m using HTML parsing mode, if that matters. Thanks for your help!
I’ve encountered this issue before when working with Telegram bots. The solution that worked for me was using URL encoding for the newline character. Instead of ‘\n’, try ‘%0A’ in your message string. Here’s how you can modify your code:
message=“Hello%0AThis is a new line”
curl -X POST “https://api.telegram.org/bot${TOKEN}/sendMessage”
-d “chat_id=${CHAT_ID}”
-d “text=${message}”
-d “parse_mode=HTML”
This should properly encode the newline for transmission via HTTP and result in the desired line break in your Telegram message. Let me know if you need any further clarification.
hey mate, try using the actual newline character in ur message string instead of \n. like this:
message="Hello
This is a new line"
shud work fine with HTML parsing. lemme kno if it helps!
I’ve worked on several Telegram bot projects, and I’ve found that using the literal newline character in a heredoc format works consistently. Here’s what I suggest:
message=$(cat <<EOF
Hello
This is a new line
EOF
)
curl -X POST “https://api.telegram.org/bot${TOKEN}/sendMessage”
-d “chat_id=${CHAT_ID}”
-d “text=${message}”
-d “parse_mode=HTML”
This approach preserves the newlines without needing special encoding. It’s also more readable when you have multiple lines or complex formatting. Just remember to escape any backticks or dollar signs in your message if you’re using them.