Delete message sent to Telegram Bot with Nodered

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": ""
    }
]

similar use-case, but using whatsapp instead of Telegram, the flow used should be pretty similar and is on the video description:

Not tried this, but you should be able to send a http request to the bot to delete messages, there are time limits and chat admin rights restrictions
https://api.telegram.org/bot<TOKEN>/deleteMessage?chat_id=<CID>&message_id=<MID>

Yes. I'm doing the same and it works. I receive the inline keyboard as you can see in my picture and everything works fine if the code is correct.

What I need to do when I press the confirm button (the check in green) is to cancel the message received shown in this photo.

Should I change the CID with my id chat and the MID with the code I get from the answer of my bot?

Yes, you would need to save the message id of the inline message. put a debug on the sender and see what the message id is after the inline message is sent.

Ok. I will get the messageId from the object once I get the answer and will replace in that string. I'll let you know If it works or not

yes, there're may ways to implement what you're trying, it's a classic One-Time-Psw use case. Just grab the msg_id used by Telegram and you'll get it done.

I tried in this way.
I get the msg_id from the receiver node of my telegrambot in Nodered and I compile it into my http string in this way

https://api.telegram.org/"MyToken"/deleteMessage?chat_id="MyChatID"&message_id={{{query}}}

In the {{{query}}} I'm going to compile it with the msg_id I got it from the node (check it if it works properly with the debug node) and at the end what I'm receiving is this:

"{"ok":false,"error_code":404,"description":"Not Found"}"

It gives me this error and I can't figure it out how to solve it.
The bot into my Telegram group is an administrator because what I read from the link you sent me is that it must be an admin to delete the messages.

Did you add a debug to the node that send the inline keyboard message, if so what does it return?
And what is the contents of msg.query? from {{{query}}}

This is what the debug returns to me from the inline message once I press on the inject node:

If I press any options from my keypad I receive this:

Schermata 2022-04-08 alle 19.56.11

I'm trying manually now to set the message_id=16972 but still gives me the error

{"ok":false,"error_code":404,"description":"Not Found"}

the https string I'm sending is as following:

https://api.telegram.org/"MyTOKEN"/deleteMessage?chat_id="-673503089"&message_id=16972

Nothing works. Still persist that error

Please show the whole objects, there are collapsed objects in those images. Best use copy value in the debug panel and post the text. Obviously edit security info.

You need to find the msd id of the inline message.
[edit]
After some reading you can define the inlineMessageId in the inline msg object. try adding the property to the object and give it an id. then try using that in the http request MID

here it is.
Now the id is increased

I just kept closed the options given that are the numbers of the keypad.
Mow I'm using the id 16974 as written in the debug

You could also try this method Telegram Bot API
try sending reply mark up as null.

this worked for me
https://api.telegram.org/bot1234567-dddh4h4g3mrl-ilr5EfE/editMessageReplyMarkup?chat_id=-456777777082&message_id=1264&reply_markup={}
and removed key board.

the error you where getting, i believe was due to not having bot infront of botkey

and the delete works to
https://api.telegram.org/bot1234567-dddh4h4g3mrl-ilr5EfE/deleteMessage?chat_id=-456777777082&message_id=1264
which removes message and keyboard.

1 Like

I did it thanks. it was about the bot at the beginning just before the token.

Thanks so much for your help. I appreciate.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.