Telegram button problem

Hello,

I have Telegram running and I can send and receive text messages.
I can also send buttons, but nothing comes back to the Telegram receiver when a button is clicked.
I have no idea where else to look or what to check.

This is my test to send the button

[{"id":"callback_handler","type":"function","z":"716b40efe6f3823e","name":"Callback auswerten","func":"if (msg.payload.type === \"callback_query\") {\n  const aktion = msg.payload.content;\n  const antwort = (aktion === \"heizung_an\")\n    ? \"Heizung wurde eingeschaltet!\"\n    : (aktion === \"heizung_aus\")\n    ? \"Heizung wurde ausgeschaltet!\"\n    : \"Unbekannte Aktion!\";\n\n  // Telegram erwartet IMMER eine Antwort bei callback_query\n  return {\n    payload: {\n      chatId: msg.payload.chatId,\n      type: 'message',\n      content: antwort\n    }\n  };\n}\n\nreturn null;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":,"x":550,"y":200,"wires":[["90dbcc0849fea872","20787d840b66999b"]]},{"id":"188773d7aebf5c10","type":"telegram event","z":"716b40efe6f3823e","name":"","bot":"8d8ccc97ab1d3524","event":"callback_query","autoanswer":false,"x":240,"y":180,"wires":[["38a2ea75151eb98b","callback_handler"]]},{"id":"90dbcc0849fea872","type":"debug","z":"716b40efe6f3823e","name":"debug 80","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":200,"wires":},{"id":"20787d840b66999b","type":"telegram sender","z":"716b40efe6f3823e","name":"","bot":"8d8ccc97ab1d3524","haserroroutput":false,"outputs":1,"x":890,"y":240,"wires":[]},{"id":"38a2ea75151eb98b","type":"debug","z":"716b40efe6f3823e","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":140,"wires":},{"id":"f45ac2b76240b9d4","type":"function","z":"716b40efe6f3823e","name":"function 13","func":"msg.payload = {\n  chatId: 44444444, // Ersetze hier deine Telegram Chat-ID\n  type: \"message\",\n  content: \"Was soll mit der Heizung geschehen?\",\n  options: {\n    reply_markup: {\n      inline_keyboard: [\n        [\n          { text: \"Heizung AN\", callback_data: \"heizung_an\" },\n          { text: \"Heizung AUS\", callback_data: \"heizung_aus\" }\n        ]\n      ]\n    }\n  }\n};\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":,"x":530,"y":240,"wires":[["20787d840b66999b"]]},{"id":"94781efeec2338e6","type":"inject","z":"716b40efe6f3823e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":240,"wires":[["f45ac2b76240b9d4"]]},{"id":"8d8ccc97ab1d3524","type":"telegram bot","botname":"Testtobienbot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Your flow json is corrupt due to no following forum guidelines on posting code.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil: icon.

See this post for more details - How to share code or flow json

Saying that you are using a inline keyboard, so you should receive the call back using a event node set to callback not a receiver node, Check the node examples in
ctrli i > examples > telegrambot.

There are working examples you can import and adjust (CTRL+I -> examples -> node-red-contrib-telegrambot)

1 Like

I have cleaned up the code for someone to test.

[{"id":"38a2ea75151eb98b","type":"debug","z":"9cd9fa2d0d2e7202","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":3220,"wires":[]},{"id":"90dbcc0849fea872","type":"debug","z":"9cd9fa2d0d2e7202","name":"debug 80","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":3280,"wires":[]},{"id":"callback_handler","type":"function","z":"9cd9fa2d0d2e7202","name":"Callback auswerten","func":"if (msg.payload.type === \"callback_query\") {\n  const aktion = msg.payload.content;\n  const antwort = (aktion === \"heizung_an\")\n    ? \"Heizung wurde eingeschaltet!\"\n    : (aktion === \"heizung_aus\")\n    ? \"Heizung wurde ausgeschaltet!\"\n    : \"Unbekannte Aktion!\";\n\n  // Telegram erwartet IMMER eine Antwort bei callback_query\n  return {\n    payload: {\n      chatId: msg.payload.chatId,\n      type: 'message',\n      content: antwort\n    }\n  };\n}\n\nreturn null;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":3280,"wires":[["90dbcc0849fea872","20787d840b66999b"]]},{"id":"80c15cbff93d494c","type":"telegram receiver","z":"9cd9fa2d0d2e7202","name":"","bot":"8d8ccc97ab1d3524","saveDataDir":"","filterCommands":true,"x":170,"y":3280,"wires":[["callback_handler","38a2ea75151eb98b"],["38a2ea75151eb98b"]]},{"id":"20787d840b66999b","type":"telegram sender","z":"9cd9fa2d0d2e7202","name":"","bot":"8d8ccc97ab1d3524","haserroroutput":false,"outputs":1,"x":830,"y":3320,"wires":[[]]},{"id":"f45ac2b76240b9d4","type":"function","z":"9cd9fa2d0d2e7202","name":"function 13","func":"msg.payload = {\n  chatId: 44444444, // Ersetze hier deine Telegram Chat-ID\n  type: \"message\",\n  content: \"Was soll mit der Heizung geschehen?\",\n  options: {\n    reply_markup: {\n      inline_keyboard: [\n        [\n          { text: \"Heizung AN\", callback_data: \"heizung_an\" },\n          { text: \"Heizung AUS\", callback_data: \"heizung_aus\" }\n        ]\n      ]\n    }\n  }\n};\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":3320,"wires":[["20787d840b66999b"]]},{"id":"94781efeec2338e6","type":"inject","z":"9cd9fa2d0d2e7202","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":3320,"wires":[["f45ac2b76240b9d4"]]},{"id":"8d8ccc97ab1d3524","type":"telegram bot","botname":"Testtobienbot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]

Tried to get some solution, please see if this works.

used an event node instead of a reciever node.

[{"id":"callback_handler","type":"function","z":"716b40efe6f3823e","name":"Callback auswerten","func":"if (msg.payload.type === \"callback_query\") {\n  const aktion = msg.payload.content;\n  const antwort = (aktion === \"heizung_an\")\n    ? \"Heizung wurde eingeschaltet!\"\n    : (aktion === \"heizung_aus\")\n    ? \"Heizung wurde ausgeschaltet!\"\n    : \"Unbekannte Aktion!\";\n\n  // Telegram erwartet IMMER eine Antwort bei callback_query\n  return {\n    payload: {\n      chatId: msg.payload.chatId,\n      type: 'message',\n      content: antwort\n    }\n  };\n}\n\nreturn null;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":200,"wires":[["90dbcc0849fea872","20787d840b66999b"]]},{"id":"188773d7aebf5c10","type":"telegram event","z":"716b40efe6f3823e","name":"","bot":"8d8ccc97ab1d3524","event":"callback_query","autoanswer":false,"x":240,"y":180,"wires":[["38a2ea75151eb98b","callback_handler"]]},{"id":"90dbcc0849fea872","type":"debug","z":"716b40efe6f3823e","name":"debug 80","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":200,"wires":[]},{"id":"20787d840b66999b","type":"telegram sender","z":"716b40efe6f3823e","name":"","bot":"8d8ccc97ab1d3524","haserroroutput":false,"outputs":1,"x":890,"y":240,"wires":[[]]},{"id":"38a2ea75151eb98b","type":"debug","z":"716b40efe6f3823e","name":"debug 79","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":520,"y":140,"wires":[]},{"id":"f45ac2b76240b9d4","type":"function","z":"716b40efe6f3823e","name":"function 13","func":"msg.payload = {\n  chatId: 44444444, // Ersetze hier deine Telegram Chat-ID\n  type: \"message\",\n  content: \"Was soll mit der Heizung geschehen?\",\n  options: {\n    reply_markup: {\n      inline_keyboard: [\n        [\n          { text: \"Heizung AN\", callback_data: \"heizung_an\" },\n          { text: \"Heizung AUS\", callback_data: \"heizung_aus\" }\n        ]\n      ]\n    }\n  }\n};\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":240,"wires":[["20787d840b66999b"]]},{"id":"94781efeec2338e6","type":"inject","z":"716b40efe6f3823e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":240,"wires":[["f45ac2b76240b9d4"]]},{"id":"8d8ccc97ab1d3524","type":"telegram bot","botname":"Testtobienbot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksprotocol":"socks5","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","botpath":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
1 Like

Although probably not related and dependent on which Telegram node you are using, I have had situations similar to this where some Telegram nodes appear to stop responding (usually after long periods of NR running without intervention). Doing a full Deploy of NR fixes the issue. There is a little bug somewhere.

2 Likes

Well, talk about on queue. I just sent a command via the Telegram Command node, a command that I use periodically to initiate a Keyboard. No keyboard and no output. I have an inject node wired to the keyboard bypassing the above Command node. Injecting works as expected.

Performing a Full Deploy fixes the above.

This using `node-red-contrib-telegrambot v16.3.2`

Hello,

Many thanks :smiling_face_with_three_hearts: — parallel to your approach, I realized that I should have been using an event instead of the receiver.

What’s been troubling me is that once I press the button, everything behaves as expected. However, the buttons remain active even after dozens of subsequent messages. How do you prevent this and ensure that only the button relevant to the current situation can be pressed?

In my testing setup, I ended up in a messy state: I turned something on even though it was already active, and the dashboard’s toggle state no longer matched what was displayed in Telegram. I can handle all of these cases in the logic, but I’d like to prevent the root cause: once the command is sent via Telegram, a second press shouldn't be allowed—until a new request with buttons is sent.