Skip to content

openHAB, Telegram Bot and quoting URLs

A while ago I integrated DWD warnings (Deutscher Wetterdienst) into our home automation system, and receive severe weather notifications ever since then. That works quite good, but it was missing a weblink with more details - or if you want to forward the warning to someone it would be useful to have a link to the warning as well. The DWD binding does not provide that piece of information, but it is easy enough to add - or so I thought.


In my setup I roll out the rules file with Ansible, which lets me loop over all the locations I'm interested in. I expanded the $dwds variable with an additional link:

{% set dwds = [('Ort 1', 'ort1', '111000001', ''),
               ('Ort 2', 'ort2', '111000002', '...'),
               ('Ort 3', 'ort3', '111000003', '...'),
               ('Ort 4', 'ort4', '111000004', '...')
              ] %}

So far this is easy, and I only need to attach the link to the Telegram message. First iteration:

    meldung = meldung + Warning{{ dwd[1] }}Instruction{{ chan }}.state.toString
    meldung = meldung + "\n\n" + "[Details]({{ dwd[3] }})"
    logInfo("DWD", meldung)

The format the Telegram bot uses is "Markdown V2", and the [...](...) format adds a link (...) with a description in [...]. That however does not work, the Telegram API does not add the correct link (one closing parenthesis short), and the resulting link in the group is a 404.

A look into the documentation reveals:

Inside (...) part of inline link definition, all ')' and '\' must be escaped with a preceding '\' character.

Well, ok, let's escape the closing parenthesis and see where this goes. I'm doing that directly in Ansible:

    meldung = meldung + "\n\n" + "[Details]({{ dwd[3] | replace(')', '\\)') }})"

However you can also do it in the rule DSL:

    meldung = meldung + "\n\n" + "[Details](url.replace(')','\\)'))"

The debug output shows that the parenthesis is indeed escaped:\)

But for reasons I'm unable to figure out, Telegram still shows the closing parenthesis. The URL however is still cut short by one parenthesis, and an additional "?" is added.


At this point, and after much debugging, I switched to URL escaping the characters. In Ansible:

meldung = meldung + "\n\n" + "[Details]({{ dwd[3] | replace('(', '%28') | replace(')', '%29') }})"

Or directly in the rule:

meldung = meldung + "\n\n" + "[Details](url.replace('(','%28').replace(')', '%29'))"

The debug output for the link is now:

And the correct link is added in Telegram:


All URLs should be URL quoted, if they come from an external source. In this case I don't have to care about this, because I specify the URLs myself.

I could also add the quoted URLs directly to the $dwds variable - however I don't know if I will have further use cases which need the original URL. So I rather quote and escape it when needed, and don't add already quoted URLs to the initial data.


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options