linuxbashtelegram-bot

Adding line break to text message using Telegram bot


im trying to send telegram messages from Linux bash using curl. My current problem is that i am unable to send line breaks in the message.

My code is something like this:

msg="<a href=\"${img}\">&#160;</a><b>${title}</b><a href=\"${lnk}\">MORE INFO</a>"

curl --data chat_id=$chatID --data-urlencode "text=${msg}" "https://api.telegram.org/bot${apik}/sendMessage?parse_mode=HTML"

i tried with </br> \n %0D%0A, and none work.


Solution

  • Maybe try setting your $msg variable as a heredoc:

    #!/bin/bash
    
    img="one"
    title="two"
    lnk="three"
    
    read -r -d '' msg <<EOT
    <a href="${img}"></a>
    <b>"${title}"</b>
    <a href="${lnk}">MORE INFO</a>
    EOT
    
    curl --data chat_id="$chatID" --data-urlencode "text=${msg}" "https://api.telegram.org/bot${apik}/sendMessage?parse_mode=HTML"
    

    Wikipedia : Here Documents