[SOLVED] [Telegram-Binding] Conversation with two Chats not working in sendTelegramAnswer

Hi,

in general my TelegramBinding seems to configured well and also works in sending Messages. Now I started to implement some kind of conversation in rules.

I send a question to two chats (me and my girlfriend) via sendTelegramQuery: “Is the trash-can already brought out?”, “YES”, “NO”
When I answer from my Phone the message is received, handled and the sendTelegramAnswer removes the buttons
When my girlfriend answers, the message is received, handled, but the sendTelegramAnswer throws the Invalid-Query error and the buttons are still there.

Here ist the setup

telegram.things

Bridge telegram:telegramBot:TelegramBotHuman "TelegramBot Human" [
    chatIds="myChatId","herChatId",
    botToken="aaaaa:bbbbb"

]

telegram.items

DateTime TelegramBotHuman_Last_Message__DateTime "Telegram Human last Message DateTime" {
        channel="telegram:telegramBot:TelegramBotHuman:lastMessageDate"
}
String TelegramBotHuman_Last_Message__Text "Telegram Human last Message Text" {
        channel="telegram:telegramBot:TelegramBotHuman:lastMessageText"
}
String TelegramBotHuman_Last_Message__Name "Telegram Human last Message Name" {
        channel="telegram:telegramBot:TelegramBotHuman:lastMessageName"
}
String TelegramBotHuman_Last_Message__Username "Telegram Human last Message Username" {
        channel="telegram:telegramBot:TelegramBotHuman:lastMessageUsername"
}
String TelegramBotHuman__ChatId "Telegram Human ChatId" {
        channel="telegram:telegramBot:TelegramBotHuman:chatId"
}
String TelegramBotHuman__ReplyId "Telegram Human ReplyId" {
        channel="telegram:telegramBot:TelegramBotHuman:replyId"
}

trashcan.rules

val telegramAction = getActions("telegram","telegram:telegramBot:TelegramBotHuman")
val notificationDiffHours = 6

rule "CALENDAR_Check_Next_Event"
when 
    Time cron "0 0 * 1/1 * ? *"
then
    logInfo("Abfallkalender", "Check Abfallkalender Events")    
    val event_ts = Abfall_next_event_at.getStateAs(DateTimeType)
    val diff = (event_ts.calendar.timeInMillis - now.millis) / 3600000
    if (Global_Trash_Notification.state == NULL) {
        Global_Trash_Notification.sendCommand(OFF)
    }
    if ((diff <= notificationDiffHours) && (Global_Trash_Notification.state == OFF)) {
        logInfo("Abfallkalender", "Informiere zu " + Abfall_next_event_name.state.toString + " via Telegram")
        telegramAction.sendTelegramQuery("INFO: Ist der/die/das " + Abfall_next_event_name.state.toString + " bereits rausgestellt?", "Reply_TrashCan", "JA", "NEIN")
    } else if ((diff > notificationDiffHours)) {
        Global_Trash_Notification.sendCommand(OFF)
    }   

end

rule "CALENDAR_Check_Next_Event_Reply"
when
    Item TelegramBotHuman__ReplyId received update Reply_TrashCan
then
    if (TelegramBotHuman_Last_Message__Text.state.toString == "JA") {
        Global_Trash_Notification.sendCommand(ON) 
        telegramAction.sendTelegramAnswer(TelegramBotHuman__ReplyId.state.toString, "Vielen Dank")
    } else {
        Global_Trash_Notification.sendCommand(OFF) 
        telegramAction.sendTelegramAnswer(TelegramBotHuman__ReplyId.state.toString, "Na dann aber schnell!")        
    }
end

Any Idea why I get this error? I already searched this forum and reviewed my setup. Everything else is working perfectly with the telegram binding…

What error? You don’t seem to have included it.

Sorry, it was just mentioned in the text.

The complete error i get in the log is:

Failed to send telegram message: Bad Request: query is too old and response timeout expired or query ID is invalid

I had the same problem. My workaround was to send a message to each participant of the group and handle their responses individually. You can do that by passing a ChatId (Long!) as first argument (e.g. myChatId) and modifying the Reply name accordingly (e.g. my_Reply_TrashCan) in the sendTelegramQuery action:


telegramAction.sendTelegramQuery(myChatId,"INFO: Ist der/die/das " + Abfall_next_event_name.state.toString + " bereits rausgestellt?", "my_Reply_TrashCan", "JA", "NEIN")

telegramAction.sendTelegramQuery(herChatId,"INFO: Ist der/die/das " + Abfall_next_event_name.state.toString + " bereits rausgestellt?", "her_Reply_TrashCan", "JA", "NEIN")

Of course that implies checking which participant has sent an answer in the answer handling rule:


rule "CALENDAR_Check_Next_Event_my_Reply"
when
    Item TelegramBotHuman__ReplyId received update my_Reply_TrashCan
then
    if (TelegramBotHuman_Last_Message__Text.state.toString == "JA") {
        Global_Trash_Notification.sendCommand(ON) 
        telegramAction.sendTelegramAnswer(myChatId,TelegramBotHuman__ReplyId.state.toString, "Vielen Dank")
    } else {
        Global_Trash_Notification.sendCommand(OFF) 
        telegramAction.sendTelegramAnswer(myChatId,TelegramBotHuman__ReplyId.state.toString, "Na dann aber schnell!")        
    }
end


rule "CALENDAR_Check_Next_Event_her_Reply"
when
    Item TelegramBotHuman__ReplyId received update her_Reply_TrashCan
then
    if (TelegramBotHuman_Last_Message__Text.state.toString == "JA") {
        Global_Trash_Notification.sendCommand(ON) 
        telegramAction.sendTelegramAnswer(herChatId,TelegramBotHuman__ReplyId.state.toString, "Vielen Dank")
    } else {
        Global_Trash_Notification.sendCommand(OFF) 
        telegramAction.sendTelegramAnswer(herChatId,TelegramBotHuman__ReplyId.state.toString, "Na dann aber schnell!")        
    }
end

To my understanding the first participant answering a group query consumes the queryId, so that an ulterior answer cannot be associated with the original query any longer.

1 Like

@jalm: thanks for that hint - that solution worked. Only restriction: when he answers, her Buttons don’t get removed and vice versa - but I can deal with it :slight_smile:

I had a similar problem, but with the query in a group chat. I could solve it by adding the Chat-ID in front of the reply-id. Maybe this could help you, too, or anybody else.

// Warnung beim Verlassen des Hauses, wenn noch Geräte aktiv sind
rule "Warnung aktive Geräte beim Verlassen"
when
	Item Anwesenheitserkennung changed to OFF or
    Item Test_Bot_Question changed to ON
then
	if ((Aktive_Geraete_WZ.state == ON) || (Group_All_Licht.state == ON)) {
        val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot")
        telegramAction.sendTelegramQuery(-123456789L,"Es ist niemand mehr anwesend, aber noch Geräte oder Leuchten aktiv. Sollen alle Geräte und Leuchten ausgeschaltet werden?", "Warn_active_devices", "Ja", "Nein")
    }
end

rule "Reply handler for Warning active devices"
when
    Item telegramReplyId received update Warn_active_devices
then
    val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot")
    if (telegramMessage.state.toString == "Ja") {
        Scenes_Heimkommen.sendCommand(OFF)
        telegramAction.sendTelegramAnswer(-123456789L,telegramReplyId.state.toString, "Es wurden alle Geräte und Leuchten ausgeschaltet.") 
    }
    if (telegramMessage.state.toString == "Nein") {
        telegramAction.sendTelegramAnswer(-123456789L,telegramReplyId.state.toString, "Ok, es wurde keine Aktion durchgeführt.") 
    }
    else {
        telegramAction.sendTelegramAnswer(-123456789L,telegramReplyId.state.toString, "Keine passende Antwort erhalten.")
    }
end