0

I use a Telegram bot to incorporate weather alerts from a local weatherservice into my homeautomation system. Today I discovered a weird problem because the message containing the weather alert wasn't sent. If I try this in bash on Linux:
output="Nationaal Hitteplan";curl "https://api.telegram.org/botxxxxxxxxx:longsecuritycode/sendMessage?chat_id=xxxxxxxxx&text=$output"
(I removed my personal tokens in the above command of course...)
then I get a 400 Bad Request and the message is not sent. If I change output="Nationaal Hitteplan" to output="Nationaal hitteplan" then the message is send as it is supposed to be.
I don't see what's wrong here. The term Nationaal Hitteplan is basically a set of advisories about what to do in hot weather. It has no negative meaning associated with it but apparently Telegram detects a problem.
Does someone have a solution for this except from changing the term as described above?

wie5Ooma
  • 281
  • 1
  • 4
  • 15

1 Answers1

3

Url's that contains special characters like a space should be url-encoded.

Use the following curl command to let curl let the encoding:

token='xxxxxxxxx:longsecuritycode';
output="Nationaal Hitteplan";
curl -G \
    --data-urlencode 'chat_id=1234567' \
    --data-urlencode "text=${output}" \
    "https://api.telegram.org/bot${token}/sendMessage"

How to urlencode data for curl command?

0stone0
  • 34,288
  • 4
  • 39
  • 64