[SOLVED] Sending item.name via Telegram resolves in Error 400 Bad Request from Telegram

Want to send Telegram Message if Window is open.
Problem: Whenever I add i.name to the Message (equal if only one or a List through a String) in openhab.log

[ab.action.telegram.internal.Telegram] Method failed: HTTP/1.1 400 Bad Request

and the Message isn’t send.

The Part of the Rule:

var String temp

gWindow.members.filter[ i|i.state == OPEN ].forEach [i| 
	temp = temp + "\n" + i.name
logInfo("FB.rules", "Offen sind "+temp)
sendTelegram("Bot", "Folgende Fenster oder Türen sind noch offen:\n"+temp.toString)
sendMail("BLA@localhost", "Offene Fenster", "Offen sind "+temp)

That sendMail I added to see what should be send via Telegram for debugging only.
The Mail shows all right:

Offen sind 

Don’t find the reason for that Bad-Request

While testing have found the Reason:

If sending a Underscore ( _ ) via Telegram, that causes that Error.

Hmmm, Shouldn’t the Binding/Action correct such things?

Is there a Way to replace _ by a - for example?

You add an \n, but the mail shows _. Are you sure this is the right code/mail content?

I’m using \n in telegram messages and have no problems. I can’t remember, but I’m sure that I also had _ in telegram messages. I can’t believe that this is the error.

Update: you use toString on a String. That’s not necessary.

Update 2: And you can write

var String temp = ""

instead of

var String temp

Its the right code! The \n does a Line Feed before the Item-Names to list everyone in a different row. It the richt code / mail content!
I tried with “Bot”,“Text_Test” and got the same error.

After I used temp=temp.replace("_","-") it works!

It’s definitly the Underscore in the Item-Names

That .toString was while testing, after +temp didn’t work, but thanks for the hints.

1 Like

Well, I thought about mark as solution, but think binding should change such little things on its own, ow should recode it. :wink:

Will you create an issue on GitHub? Otherwise I will.

Googling a bit shows that underscore _ and asterisk * have a markup/highlight function in Telegram.
Any system that wants a visible _ in telegram text seems to need to escape it in some way.
It’s arguable whether that should be up to the binding or the user in OH - you might want to invoke the markup function.

I think maybe you need to look into the telegram action parseMode configuration parameter - does that sort all this out?

It had allready been discussed on github.

A hint in openhab Doku could be helpful then

You can edit the documentation also on GitHub.

I did my very best! Others have to check now.

1 Like

With new snapshot replace(„_“, „_“) didn’t work anymore, throws an error only \d … allowed