I use the telegram binding with dsl code including questions with answers. I tried to build something with java scripting but can’t get it to work. In my understanding based on what I install as automation
automation = jsscripting
this must be based on graaljs in OH 5
Ik have a file called test.js with the following code
var { rules, triggers, actions } = require('openhab');
rules.JSRule({
name: "Send telegram Goed/Fout bij start",
triggers: [triggers.ItemCommandTrigger("carBMW_Token_Request")],
execute: function () {
console.log("========kort testscript=========");
var telegramAction = actions.get("telegram", "telegram:telegramBot:Botxxxx");
if (!telegramAction) {
console.log("=====Telegram action is NULL – check Thing UID en status!");
return;
}
console.log("=====Telegram bericht versturen");
telegramAction.sendTelegramQuery(
"Testbericht bij laden van het script",
"Reply_Test",
"Goed",
"Fout"
);
}
});
rules.JSRule({
name: "Telegram Callback Debugger (clean)",
triggers: [
triggers.ChannelEventTrigger("telegram:telegramBot:BotHennie:callbackRawEvent")
],
execute(event) {
try {
console.log("===== Telegram callback event ontvangen =====");
const payload = typeof event.receivedEvent === "string" ? JSON.parse(event.receivedEvent) : event.receivedEvent;
console.log("Payload geparsed:", payload);
const callbackId = payload.id;
console.log("Callback ID:", callbackId, "type:", typeof callbackId);
const answer = payload.data ? payload.data.split(" ")[1] : "geen antwoord";
console.log("Antwoord bepalen:", answer);
const telegramAction = actions.get("telegram", "telegram:telegramBot:BotHennie");
if (!telegramAction) {
console.error("Telegram action is NULL!");
return;
}
const JavaString = Java.type("java.lang.String");
const callbackIdJava = new JavaString(callbackId); // <-- hier!
const responseText = answer === "Goed" ? "Prima, actie geregistreerd." : "Oké, actie geregistreerd.";
const success = telegramAction.sendTelegramAnswer(callbackIdJava, responseText);
console.log("sendTelegramAnswer returned:", success);
} catch (err) {
console.error("Exception in Telegram callback handler:", err, err.stack || "no stack");
}
}
});
and in the logfile I get (I modified some names)
2025-11-18 21:44:20.602 [INFO ] [enhab.automation.script.file.test.js] - ========kort testscript=========
2025-11-18 21:44:20.603 [INFO ] [enhab.automation.script.file.test.js] - =====Telegram bericht versturen
2025-11-18 21:44:21.750 [INFO ] [enhab.automation.script.file.test.js] - ===== Telegram callback event ontvangen =====
2025-11-18 21:44:21.752 [INFO ] [enhab.automation.script.file.test.js] - Payload geparsed: {
"id": "6789276462570019503",
"from": {
"id": 1580751609,
"is_bot": false,
"first_name": "xxx",
"language_code": "nl"
},
"message": {
"from": {
"id": 2137109059,
"is_bot": true,
"first_name": "xxxOHBot",
"username": "xxxOH_bot"
},
"text": "Testbericht bij laden van het script",
"reply_markup": {
"inline_keyboard": [
[
{
"text": "Goed",
"callbackData": "Reply_Test Goed"
},
{
"text": "Fout",
"callbackData": "Reply_Test Fout"
}
]
]
},
"chat": {
"id": 1580751609,
"type": "private",
"first_name": "xxxx"
},
"message_id": 14850,
"date": 1763498660
},
"chat_instance": "2487401512688388006",
"data": "Reply_Test Fout"
}
2025-11-18 21:44:21.752 [INFO ] [enhab.automation.script.file.test.js] - Callback ID (String): 6789276462570019503
2025-11-18 21:44:21.753 [INFO ] [enhab.automation.script.file.test.js] - Data: Reply_Test Fout
2025-11-18 21:44:21.753 [INFO ] [enhab.automation.script.file.test.js] - Antwoord bepalen: Fout
2025-11-18 21:44:21.753 [WARN ] [gram.internal.action.TelegramActions] - messageId not defined; action skipped.
2025-11-18 21:44:21.758 [INFO ] [enhab.automation.script.file.test.js] - sendTelegramAnswer returned: false
Who knows how this callback should work?