Update/edit a Telegram message

Dear All,

Some time ago I observed an interesting Telegram chat which is fed from Nodered.
The programmers display a 'realtime' list of devices in a chat, and the device online/offline status.
The interesting is, that the updates are done within the same message, as if the message is edited in some way. There are no new messages which is a kind of relaxed way for feeding the information.

Does anybody know some documentation on how to achieve this way of working with messages to Telegram? I am unable to find what I should be looking for....

Many thanks!

Ronald

the thing you are looking for is type "editMessageText"

The documentation is out there but not in a ready made "how to" for node red nodes I'm afraid.

Using telegram documentation and looking at the source code for this node-red node I cobbled together a basic demo...

  • In your telegram app, send /serverstatus to begin receiving messages.
  • press the injects to fake server status changes - watch the message update
  • send /stop to clear the chat_id/message_id stored and prevent new updates being sent to telegram.

The message you get is just raw JSON (I'll leave that for you to tidy up)
image

Here is the flow...

[{"id":"73a7f6b4.fecb78","type":"telegram command","z":"5d3f964c.fef898","name":"/serverstatus","command":"/serverstatus","bot":"fa7a9376.321ce","strict":false,"hasresponse":false,"x":170,"y":1620,"wires":[["2759079.cd0e4f8","e6e3f9e3.9f4b68"]]},{"id":"db357bb6.47cc98","type":"telegram command","z":"5d3f964c.fef898","name":"/stop","command":"/stop","bot":"fa7a9376.321ce","strict":false,"hasresponse":false,"x":150,"y":1740,"wires":[["ef443902.89a8c8","3d8cc89b.0ce768"]]},{"id":"2759079.cd0e4f8","type":"debug","z":"5d3f964c.fef898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":330,"y":1660,"wires":[]},{"id":"ef443902.89a8c8","type":"debug","z":"5d3f964c.fef898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":330,"y":1780,"wires":[]},{"id":"e6e3f9e3.9f4b68","type":"change","z":"5d3f964c.fef898","name":"save chat_id","rules":[{"t":"set","p":"chat_id","pt":"flow","to":"payload.chatId","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":1620,"wires":[["2978358b.f28f4a"]]},{"id":"3d8cc89b.0ce768","type":"change","z":"5d3f964c.fef898","name":"clear chat_id","rules":[{"t":"delete","p":"chat_id","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":1740,"wires":[["5121be07.154ef"]]},{"id":"5121be07.154ef","type":"change","z":"5d3f964c.fef898","name":"delete message_id","rules":[{"t":"delete","p":"message_id","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1740,"wires":[[]]},{"id":"e49f0f49.68edf","type":"change","z":"5d3f964c.fef898","name":"save message_id","rules":[{"t":"set","p":"message_id","pt":"flow","to":"payload.content.message_id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":970,"y":1660,"wires":[[]]},{"id":"54b22eed.24e99","type":"telegram sender","z":"5d3f964c.fef898","name":"","bot":"fa7a9376.321ce","x":850,"y":1620,"wires":[["e49f0f49.68edf"]]},{"id":"e90adeb2.66434","type":"inject","z":"5d3f964c.fef898","name":"","topic":"server/1/status","payload":"up","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1840,"wires":[["3e9729fc.44be86"]]},{"id":"39f7c105.cbaade","type":"inject","z":"5d3f964c.fef898","name":"","topic":"server/1/status","payload":"down","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1880,"wires":[["3e9729fc.44be86"]]},{"id":"83b303bf.feed4","type":"inject","z":"5d3f964c.fef898","name":"","topic":"server/2/status","payload":"up","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1920,"wires":[["3e9729fc.44be86"]]},{"id":"e11a90b6.db038","type":"inject","z":"5d3f964c.fef898","name":"","topic":"server/2/status","payload":"down","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1960,"wires":[["3e9729fc.44be86"]]},{"id":"3e9729fc.44be86","type":"function","z":"5d3f964c.fef898","name":"save server status","func":"var server_status = flow.get(\"server_status\") || {};\nserver_status[msg.topic] = msg.payload;\nflow.set(\"server_status\", server_status) \nreturn msg;","outputs":1,"noerr":0,"x":450,"y":1900,"wires":[["41fa2e6a.95e93"]],"info":"## New message\n\nHello *markdown* formatted messages!'\n\nwith `inline` code\n\n> and quote\n>> nested\n\n\\`\\`\\`\nvar code = \"cool\";\n\\`\\`\\`\n\n| table | data |\n|---|---|\n| 1 | fred |\n| 2 | bob |\n\n_Italic_ **bold** "},{"id":"41fa2e6a.95e93","type":"switch","z":"5d3f964c.fef898","name":"chat_id exists?","property":"chat_id","propertyType":"flow","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":660,"y":1900,"wires":[["2e44ed20.a2f0c2"]]},{"id":"2e44ed20.a2f0c2","type":"switch","z":"5d3f964c.fef898","name":"message_id exists?","property":"message_id","propertyType":"flow","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":870,"y":1900,"wires":[["43757dc6.981834"]]},{"id":"2978358b.f28f4a","type":"function","z":"5d3f964c.fef898","name":"build server status message","func":"var server_status = flow.get(\"server_status\") || {};\nvar stat = JSON.stringify(server_status);\n\nmsg.payload.content = stat;\nmsg.payload.type = \"message\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":1620,"wires":[["54b22eed.24e99"]],"info":"## New message\n\nHello *markdown* formatted messages!'\n\nwith `inline` code\n\n> and quote\n>> nested\n\n\\`\\`\\`\nvar code = \"cool\";\n\\`\\`\\`\n\n| table | data |\n|---|---|\n| 1 | fred |\n| 2 | bob |\n\n_Italic_ **bold** "},{"id":"43757dc6.981834","type":"function","z":"5d3f964c.fef898","name":"build server status update message","func":"var server_status = flow.get(\"server_status\") || {};\nvar stat = JSON.stringify(server_status);\n\nvar mid = flow.get(\"message_id\");\nvar cid = flow.get(\"chat_id\");\n\nmsg.payload = {};\nmsg.payload.content = stat;\nmsg.payload.type = \"editMessageText\";\nmsg.payload.chatId = cid;\n\nmsg.payload.options = {\n  message_id : mid,\n  chat_id: cid\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":1860,"wires":[["54b22eed.24e99","c31dea56.509f38"]],"info":"## New message\n\nHello *markdown* formatted messages!'\n\nwith `inline` code\n\n> and quote\n>> nested\n\n\\`\\`\\`\nvar code = \"cool\";\n\\`\\`\\`\n\n| table | data |\n|---|---|\n| 1 | fred |\n| 2 | bob |\n\n_Italic_ **bold** "},{"id":"c31dea56.509f38","type":"debug","z":"5d3f964c.fef898","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":870,"y":1800,"wires":[]},{"id":"79f57d65.851a84","type":"comment","z":"5d3f964c.fef898","name":"Send /serverstatus to register you interest in server status updates","info":"","x":340,"y":1580,"wires":[]},{"id":"7e79d1a4.00a12","type":"comment","z":"5d3f964c.fef898","name":"Send /stop to cancel server status updates","info":"","x":260,"y":1700,"wires":[]},{"id":"fa7a9376.321ce","type":"telegram bot","z":"5d3f964c.fef898","botname":"mybot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"","usesocks":false,"sockshost":"","socksport":"","socksusername":"","sockspassword":"","bothost":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":false}]
4 Likes

This seems exactly as what I need to achieve! Many thanks!

1 Like

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