[SOLVED][Telegram] sendTelegramAnswer works but error in log file 'null in xxx'

I’m a little bit lost :man_shrugging:. There is only one line which is causing the problem, where should a put a delay in?

Hi. Try this code:

val telegramAction = getActions("telegram","telegram:telegramBot:tBot")
Thread::sleep(200)
telegramAction.sendTelegramQuery("No one is at home, but some lights are still on. Do you want me to turn off the lights?", "Reply_Lights2", "Yes", "No")
	

Thanks for the hint! I did this:

rule "test rule"
when
	Item TelegramBot_ReplyId received update ReplyTest
then
	if (TelegramBot_LastMessageText.state.toString == "Yes") {
		logInfo( "TestEntry", "before yes")
		val telegramAction = getActions("telegram","telegram:telegramBot:BOT")
		Thread::sleep(1000)
		logInfo( "TestEntry", "{}", telegramAction )
		telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
		logInfo( "TestEntry", "after yes")
	}
end

log shows this:

2021-09-05 12:18:40.914 [INFO ] [.openhab.core.model.script.TestEntry] - before yes
2021-09-05 12:18:41.920 [INFO ] [.openhab.core.model.script.TestEntry] - org.openhab.binding.telegram.internal.action.TelegramActions@1168a25
2021-09-05 12:18:42.227 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-2' failed: null in test

The after yes doesn’t appear in the log file, so the error line has to be
telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
although the answer appears in Telegram

Well, what’s that for example? You can log that out too.
Although it does look more like your telegramAction object has no .sendTelegramAnswer method at this time.

sorry, still new and sure as hell no programmer… :crazy_face:
added to example from above:

logInfo( "TestEntry2", "{}", telegramAction )
logInfo( "TestEntry3", "{}", TelegramBot_ReplyId )
logInfo( "TestEntry4", "{}", TelegramBot_ReplyId.state.toString)
telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
logInfo( "TestEntry_last", "after yes")

log:

2021-09-05 12:32:17.660 [INFO ] [openhab.core.model.script.TestEntry1] - before yes
2021-09-05 12:32:18.665 [INFO ] [openhab.core.model.script.TestEntry2] - org.openhab.binding.telegram.internal.action.TelegramActions@1168a25
2021-09-05 12:32:18.668 [INFO ] [openhab.core.model.script.TestEntry3] - TelegramBot_ReplyId (Type=StringItem, State=ReplyTest, Label=TelegramBot_ReplyId, Category=, Groups=[g_Telegram])
2021-09-05 12:32:18.671 [INFO ] [openhab.core.model.script.TestEntry4] - ReplyTest
2021-09-05 12:32:18.942 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-2' failed: null in test

That was a silly comment really, you keep telling us that you actually do get the answer message at your target client.
This looks like a binding bug really, but why blow up the rule? Maybe it has something to do with the return.
Just in case, try

...
val results = telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
logInfo( "TestEntry_last", "results {}", results)

now this way:

logInfo( "TestEntry1", "before yes")
val telegramAction = getActions("telegram","telegram:telegramBot:BOT")
Thread::sleep(1000)
logInfo( "TestEntry2", "{}", telegramAction )
logInfo( "TestEntry3", "{}", TelegramBot_ReplyId )
logInfo( "TestEntry4", "{}", TelegramBot_ReplyId.state.toString)
val results = telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
logInfo( "TestEntry6", "results" )
logInfo( "TestEntry7", "results {}", results)
telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
val results1 = telegramAction.sendTelegramAnswer(TelegramBot_ReplyId.state.toString, "Test Yes")
logInfo( "TestEntry8", "results {}", results1)

log:

2021-09-05 13:02:27.375 [INFO ] [openhab.core.model.script.TestEntry1] - before yes
2021-09-05 13:02:28.380 [INFO ] [openhab.core.model.script.TestEntry2] - org.openhab.binding.telegram.internal.action.TelegramActions@1168a25
2021-09-05 13:02:28.382 [INFO ] [openhab.core.model.script.TestEntry3] - TelegramBot_ReplyId (Type=StringItem, State=ReplyTest, Label=TelegramBot_ReplyId, Category=, Groups=[g_Telegram])
2021-09-05 13:02:28.385 [INFO ] [openhab.core.model.script.TestEntry4] - ReplyTest
2021-09-05 13:02:28.756 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-2' failed: null in test

I’m again lost, why is there no TestEntry6 or TestEntry7?
Answer is still getting through to Telegram…

Because it has blown up on executing
val results = telegramAction.sendTelegramAnswer(
so rule execution stops there.

There’s not much else you can with sendTelegramAnswer() apart from give it two strings, as you do.

That suggest both that the two strings are reasonable, and that you taken note of the stuff about bi-directional chat IDs in the binding notes.

Last roll of the dice is to set the binding to DEBUG logging and see if it offers clues.

Thanks, had to read the docs to activate debug mode (still learning) :face_with_monocle:
I think you found the issue:

2021-09-05 13:46:02.542 [DEBUG] [gram.internal.action.TelegramActions] - AnswerCallbackQuery for chatId 11111111 and replyId ReplyTest is the callbackId 12345678901234
2021-09-05 13:46:02.646 [DEBUG] [gram.internal.action.TelegramActions] - remove messageId 111 for chatId 11111111 and replyId ReplyTest
2021-09-05 13:46:02.825 [DEBUG] [gram.internal.action.TelegramActions] - remove messageId null for chatId -222222222 and replyId ReplyTest
2021-09-05 13:46:02.827 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test-2' failed: null in test

There is the error in line 3! The second chatId is the group where my bot is a member of.
Is this my fault? Do I have to change my script?

Don’t know. This is now about your Telegram Thing configuration. I suppose you can’t answer to a group, or something like…

I did some more testing. Currently, my bot has 2 ChatIds, one is a personal chat with myself and one is a ChatID of a group/supergroup.
I tried to alter the Telegram thing config and tested with only one ChatID (tested on time with the personal ID and one time with the group ChatID) → both time → working without a warning or error.
So the problem exists when the Telegram thing has 2 ChatIDs.

I think, I will post this in the mail Telegram channel and will seek advice. Followed up by a issue ticket if necessary and useful.
Thanks to all for taking time and point me to the right direction.

It makes a sort of sense. You can’t acknowledge (answer) a message from someone that didn’t send you a message.

Most actions have more than 1 overloaded form. That means you can use an action specifying the exact chatID to use and if you use the version that misses chatIDs then it try’s to send it to all of them that the bot has. The readme for clarity does not list all versions of the actions and just mentions this in passing.
Feel free to change the docs to make it clearer as a reword or an exact example may help to make this clearer.

1 Like

Yes, after all that here is the last time around this loop

If you do have an idea how it can be improved then please do raise a ticket with the suggestion. I am going through the tickets for this binding and making changes to the binding at the moment. Two extra things to comment are.

  1. Be aware that using Thread::sleep(1000) in a rule is very bad, search this forum for why as it can block or delay all rules running.
  2. Be aware of the bug/issue I linked above that I plan on making the changes soon to have that fixed.

Since your logs miss the extra lines due to the null causing the rule to stop in its tracks, this is an issue that should be looked at to see if it can be handled automatically. Actions should not throw exceptions and should catch and handle them all, since that appears to not be the case this is an issue. I will look at it as there is also another complaint about the binding throwing exceptions, see here.

[Telegram] Rule execution aborted after strange SocketTimeoutException after switch to OH3 · Issue #11102 · openhab/openhab-addons · GitHub

As mentioned I am looking at making some changes over the next few days.

@rossko57: Thank you for providing the link, I didn’t find this during my search :man_facepalming: The answer is included there, at least for my problem.
sendTelegramAnswer accepts a ChatID, this solves my problem. This isn’t covered in the docs, I’ll try to add this info there.
My solution is now:
getActions("telegram","telegram:telegramBot:BOT").sendTelegramAnswer(12345678L,TelegramBot_ReplyId.state.toString, "Test Yes")
Notice the additional ChatID in the sendTelegramAnswer part.

@matt1 Thanks about the info with the sleep command. This was just a testing rule, I do not use it in production.
Currently I’m not able to produce the stop in my rule again. If I revert all back to the beginning, the rule throws a WARN but continues. Before my testing, the rule stopped. There was nothing in the DEBUG log.

It is in the docs but it is easy to overlook it if your not a programmer used to seeing what’s called overloaded functions. This is where it can be very useful for a non programmer to explain it in clear and easy terms. See here I find this pretty clear and there is more mention at the top of the action list.

All the actions can have the chat id’s added as the first arguement, or left out as you did to be sent to the bots group.

You can also specify directions.

@matt1: You are right, sorry about that. It is there and I didn’t catch it. And yes, I’m no programmer at all…

OK I found and fixed a problem in the binding since your debug level logs helped narrow it right down so thank you for reporting this…

Also the only reason you found that it worked was that you had the chatId that worked FIRST in your bot, if you had the second one first, it would not have worked as when it reaches the chatId that fails, it stops trying on the remaining chatId’s.

In the future the logs will get a WARN and the rule will not halt like it was for you. However it will not continue processing the groups ChatIds when one fails and the BOOLEAN result that the action returns will be false when any of them fails. For it to return true all chatIds must work.

If anyone notices an action that halts the rule dead, then please raise a github ticket when your sure that it is not another line in your rule causing it.

1 Like

Thank you for pointing this out!