Hi there.
I'm creating an alarm system with Nodered and OpenHAB.
What I want to do is to disarm the alarm system using Telegram.
Once I press to disarm the alarm on my openhab sitemap I'll receive a message on my telegram with telegrambot with a keypad edited with the inline keyboard where I need to insert the right code. If it is correct it will unlock the door, if not I need to retype in.
So til here everything is working fine and I can send the keypad etc. (now I'm using an inject node to send the message that its easier to try and develop).
The problem is here. Once I insert the code, even if it is wrong or correct (I receive the confirmation too) I want to cancel the keypad received from telegram. So what I need to do is cancel the message so that in this way nobody will insert again another code and keep receiving the message if it is correct or not.
I don't have any idea on how to do it. Can anyone tell me the best way to cancel the message received on my chat?
Hoping in the meantime that my code can be useful to someone else.
I'm sharing here my flow.
Thanks to everyone that will help me.
Mattia
[
{
"id": "3ee327e453366d79",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "8cca455974459408",
"type": "telegram sender",
"z": "3ee327e453366d79",
"name": "Telegram Casa",
"bot": "da102b4031f40868",
"haserroroutput": false,
"outputs": 1,
"x": 600,
"y": 140,
"wires": [
[]
]
},
{
"id": "21df2bb72f0a8496",
"type": "function",
"z": "3ee327e453366d79",
"name": "Codice Allarme Telegram",
"func": "context.global.keyboard = { messageId : msg.payload.messageId };\n\nvar opts = {\n reply_markup: JSON.stringify({\n \"inline_keyboard\": \n [\n [{\n \"text\": \"1\",\n \"callback_data\": \"1\" \n }, \n {\n \"text\": \"2\",\n \"callback_data\": \"2\" \n },\n {\n \"text\": \"3\",\n \"callback_data\": \"3\" \n }],\n [{\n \"text\": \"4\",\n \"callback_data\": \"4\" \n }, \n {\n \"text\": \"5\",\n \"callback_data\": \"5\" \n },\n {\n \"text\": \"6\",\n \"callback_data\": \"6\" \n }],\n [{\n \"text\": \"7\",\n \"callback_data\": \"7\" \n }, \n {\n \"text\": \"8\",\n \"callback_data\": \"8\" \n },\n {\n \"text\": \"9\",\n \"callback_data\": \"9\" \n }],\n [{\n \"text\": \"✅\",\n \"callback_data\": \"attivo\" \n }, \n {\n \"text\": \"0\",\n \"callback_data\": \"0\" \n },\n {\n \"text\": \"❌\",\n \"callback_data\": \"cancello\" \n }],\n ]\n })\n};\n\nmsg.payload.content = \"Inserisci il codice\";\nmsg.payload.options = opts;\nmsg.payload.chatId = -673503089\nmsg.payload.type = \"message\";\n\n\nreturn [ msg ];\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 350,
"y": 140,
"wires": [
[
"8cca455974459408"
]
]
},
{
"id": "54c97f76ca4c39eb",
"type": "inject",
"z": "3ee327e453366d79",
"name": "",
"props": [
{
"p": "payload.content",
"v": "",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payloadType": "str",
"x": 120,
"y": 140,
"wires": [
[
"21df2bb72f0a8496"
]
]
},
{
"id": "c301091e8e748cbf",
"type": "telegram event",
"z": "3ee327e453366d79",
"name": "Risp Telegram",
"bot": "da102b4031f40868",
"event": "callback_query",
"autoanswer": false,
"x": 110,
"y": 260,
"wires": [
[
"96b4de300da85011",
"16ad9f72af981a4d"
]
]
},
{
"id": "8d9cb96466bec6e6",
"type": "function",
"z": "3ee327e453366d79",
"name": "",
"func": "//flow.set('CodeAllarme', undefined);\ncodice = msg.payload\n\nif (codice === undefined){\n flow.set('CodiceAllarme',codice);\n}\nelse {\n var up_codice = flow.get('CodiceAllarme');\n var tot = up_codice + codice;\n flow.set('CodiceAllarme',tot);\n}\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 480,
"y": 240,
"wires": [
[
"3bfe5f325a501dcc"
]
]
},
{
"id": "3bfe5f325a501dcc",
"type": "delay",
"z": "3ee327e453366d79",
"name": "",
"pauseType": "delay",
"timeout": "5",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "10",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": true,
"allowrate": false,
"outputs": 1,
"x": 620,
"y": 240,
"wires": [
[
"ee6e47cb32fe1330"
]
]
},
{
"id": "ee6e47cb32fe1330",
"type": "function",
"z": "3ee327e453366d79",
"name": "Cancello Temp",
"func": "flow.set('CodiceAllarme', undefined);\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 600,
"y": 300,
"wires": [
[]
]
},
{
"id": "c28c7115a7ba73e6",
"type": "change",
"z": "3ee327e453366d79",
"name": "codice split",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "$substring(payload,9,4)",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 490,
"y": 360,
"wires": [
[
"4867593a203e4243"
]
]
},
{
"id": "4867593a203e4243",
"type": "function",
"z": "3ee327e453366d79",
"name": "Criptatura codice",
"func": "var ok_crypto = crypto\n .createHash(\"md5\")\n .update(msg.payload)\n .digest(\"hex\");\n\nif (ok_crypto == undefined){\n msg.payload = msg.timestamp\n return [msg, null]\n}\nelse {\n flow.set('CodiceAllarme',ok_crypto);\n var db = \"SELECT `Password` FROM `Allarme` WHERE `Nome` = 'Codice';\";\n msg.topic = db;\n return [null, msg]\n}",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [
{
"var": "crypto",
"module": "crypto"
}
],
"x": 690,
"y": 360,
"wires": [
[],
[
"4146e78d1652eae5"
]
]
},
{
"id": "4146e78d1652eae5",
"type": "mysql",
"z": "3ee327e453366d79",
"mydb": "3f2bf6a5.047302",
"name": "Db Mysql",
"x": 880,
"y": 360,
"wires": [
[
"be7f8c165a7b8727"
]
]
},
{
"id": "be7f8c165a7b8727",
"type": "function",
"z": "3ee327e453366d79",
"name": "Cancello Temp",
"func": "var temp = flow.get('CodiceAllarme')\nvar str = msg.payload;\ncodice = str[0]['Password']\n\nif (temp == codice){\n msg.payload = \"Ok! Allarme disattivato\"\n flow.set('CodiceAllarme', undefined);\n}\nelse if (temp != codice){\n msg.payload = \"Codice errato!\"\n flow.set('CodiceAllarme', undefined);\n}\nelse return\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1060,
"y": 360,
"wires": [
[]
]
},
{
"id": "96b4de300da85011",
"type": "function",
"z": "3ee327e453366d79",
"name": "",
"func": "input = msg.payload\nnum = input['content']\nif (num === \"1\"){\n msg.payload = \"1\"\n return[msg,null,null,null,null,null,null,null,null,null,null,null] \n}\nif (num === \"2\"){\n msg.payload = \"2\"\n return[null,msg,null,null,null,null,null,null,null,null,null,null] \n}\nif (num === \"3\"){\n msg.payload = \"3\"\n return[null,null,msg,null,null,null,null,null,null,null,null,null] \n}\nif (num === \"4\"){\n msg.payload = \"4\"\n return[null,null,null,msg,null,null,null,null,null,null,null,null] \n}\nif (num === \"5\"){\n msg.payload = \"5\"\n return[null,null,null,null,msg,null,null,null,null,null,null,null] \n}\nif (num === \"6\"){\n msg.payload = \"6\"\n return[null,null,null,null,null,msg,null,null,null,null,null,null] \n}\nif (num === \"7\"){\n msg.payload = \"7\"\n return[null,null,null,null,null,null,msg,null,null,null,null,null] \n}\nif (num === \"8\"){\n msg.payload = \"8\"\n return[null,null,null,null,null,null,null,msg,null,null,null,null] \n}\nif (num === \"9\"){\n msg.payload = \"9\"\n return[null,null,null,null,null,null,null,null,msg,null,null,null] \n}\nif (num === \"0\"){\n msg.payload = \"0\"\n return[null,null,null,null,null,null,null,null,null,msg,null,null] \n}\nif (num === \"cancello\"){\n msg.payload = msg.timestamp\n return[null,null,null,null,null,null,null,null,null,null,msg,null] \n}\nif (num === \"attivo\"){\n msg.payload = flow.get('CodiceAllarme');\n return[null,null,null,null,null,null,null,null,null,null,null,msg] \n}\nreturn msg;",
"outputs": 12,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 300,
"y": 260,
"wires": [
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"8d9cb96466bec6e6",
"ac7514fc11d5d969"
],
[
"ee6e47cb32fe1330"
],
[
"c28c7115a7ba73e6"
]
]
},
{
"id": "ac7514fc11d5d969",
"type": "debug",
"z": "3ee327e453366d79",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 490,
"y": 180,
"wires": []
},
{
"id": "16ad9f72af981a4d",
"type": "function",
"z": "3ee327e453366d79",
"name": "salvo messageId",
"func": "// We store the messageId to be able to edit this reply in the callback query. \ncontext.global.messageId = msg.payload.messageId;\nreturn [ msg ];\n",
"outputs": "1",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 270,
"y": 380,
"wires": [
[]
]
},
{
"id": "da102b4031f40868",
"type": "telegram bot",
"botname": "CasaPreveBot",
"usernames": "",
"chatids": "",
"baseapiurl": "",
"updatemode": "polling",
"pollinterval": "300",
"usesocks": false,
"sockshost": "",
"socksport": "6667",
"socksusername": "anonymous",
"sockspassword": "",
"bothost": "",
"botpath": "",
"localbotport": "8443",
"publicbotport": "8443",
"privatekey": "",
"certificate": "",
"useselfsignedcertificate": false,
"sslterminated": false,
"verboselogging": false
},
{
"id": "3f2bf6a5.047302",
"type": "MySQLdatabase",
"name": "",
"host": "192.168.1.101",
"port": "3306",
"db": "domotic_home",
"tz": "",
"charset": ""
}
]