Telegram and Email Alerts

Thanks to the awesome work by @Steve-Mcl I've managed to get my Telegram alerts running. Thanks Steve.

I am now attempting to add email alerting to the same flow... only, I cannot get the Subject to work? Node Red seems to be happy with the flow, but only shows the code in the subject and not the value?

Here's my code...

[
    {
        "id": "c18d9ee81483eb22",
        "type": "debug",
        "z": "fda74e4b5bc38453",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 870,
        "y": 320,
        "wires": []
    },
    {
        "id": "d7bc597446137a4e",
        "type": "inject",
        "z": "fda74e4b5bc38453",
        "name": "*** Setup devices to ping here ***",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "[{\"name\":\"gateway\",\"host\":\"192.168.4.5\"},{\"name\":\"Server 2\",\"host\":\"192.168.4.7\"},{\"name\":\"Server 1\",\"host\":\"192.168.4.29\"},{\"name\":\"Server 3\",\"host\":\"192.168.4.9\"},{\"name\":\"Server 5\",\"host\":\"192.168.4.14\"}]",
        "payloadType": "json",
        "x": 490,
        "y": 320,
        "wires": [
            [
                "88ed34f6f40208e6"
            ]
        ]
    },
    {
        "id": "88ed34f6f40208e6",
        "type": "ping",
        "z": "fda74e4b5bc38453",
        "mode": "triggered",
        "name": "",
        "host": "",
        "timer": "20",
        "inputs": 1,
        "x": 710,
        "y": 320,
        "wires": [
            [
                "c18d9ee81483eb22",
                "ec850e934b4a3560"
            ]
        ]
    },
    {
        "id": "ec850e934b4a3560",
        "type": "function",
        "z": "fda74e4b5bc38453",
        "name": "Prepare telegram message (if bad)",
        "func": "if(msg.payload === false) {\n    msg.payload = {};\n    msg.payload.type = 'message';\n    msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\n    msg.payload.chatId = 5612125043\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 400,
        "wires": [
            [
                "2146c727b359180b",
                "dbd882ec3cc0864e"
            ]
        ]
    },
    {
        "id": "305bafda3cfebc5c",
        "type": "debug",
        "z": "fda74e4b5bc38453",
        "name": "to Email node",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 940,
        "y": 400,
        "wires": []
    },
    {
        "id": "2146c727b359180b",
        "type": "function",
        "z": "fda74e4b5bc38453",
        "name": "Server Down Email",
        "func": "msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\nmsg.payload.topic = 'WARNING!! ${msg.ping.name} Server DOWN';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 710,
        "y": 400,
        "wires": [
            [
                "305bafda3cfebc5c"
            ]
        ]
    },
    {
        "id": "dbd882ec3cc0864e",
        "type": "debug",
        "z": "fda74e4b5bc38453",
        "name": "to Telegram node",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 950,
        "y": 460,
        "wires": []
    }
]

I also want to add a time stamp to the email... I usually use +time but not sure where to add it?

Appreciate any advice. Thanks, Mark

This should do it for you -

[{"id":"c18d9ee81483eb22","type":"debug","z":"7e04343289f26daa","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1030,"y":420,"wires":[]},{"id":"d7bc597446137a4e","type":"inject","z":"7e04343289f26daa","name":"*** Setup devices to ping here ***","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"gateway\",\"host\":\"192.168.4.5\"},{\"name\":\"Server 2\",\"host\":\"192.168.4.7\"},{\"name\":\"Server 1\",\"host\":\"192.168.4.29\"},{\"name\":\"Server 3\",\"host\":\"192.168.4.9\"},{\"name\":\"Server 5\",\"host\":\"192.168.4.14\"}]","payloadType":"json","x":650,"y":420,"wires":[["88ed34f6f40208e6"]]},{"id":"88ed34f6f40208e6","type":"ping","z":"7e04343289f26daa","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":870,"y":420,"wires":[["c18d9ee81483eb22","ec850e934b4a3560"]]},{"id":"ec850e934b4a3560","type":"function","z":"7e04343289f26daa","name":"Prepare telegram message (if bad)","func":"if(msg.payload === false) {\n    msg.payload = {};\n    msg.payload.type = 'message';\n    msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\n    msg.payload.chatId = 5612125043\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":500,"wires":[["2146c727b359180b","dbd882ec3cc0864e"]]},{"id":"305bafda3cfebc5c","type":"debug","z":"7e04343289f26daa","name":"to Email node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":500,"wires":[]},{"id":"2146c727b359180b","type":"function","z":"7e04343289f26daa","name":"Server Down Email","func":"const timestamp = Date.now()\nconst dateTime = new Date(timestamp).toLocaleString()\n\nmsg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\nmsg.payload.topic = `${dateTime} WARNING!! '${msg.ping.name}' Server DOWN`;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":500,"wires":[["305bafda3cfebc5c"]]},{"id":"dbd882ec3cc0864e","type":"debug","z":"7e04343289f26daa","name":"to Telegram node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":560,"wires":[]}]

object
type: "message"
content: "Ping to 'Server 5' (192.168.4.14) failed"
chatId: 5612125043
topic: "20/09/2022, 00:36:27 WARNING!! 'Server 5' Server DOWN"

If you want the timestamp in the message instead then just move the ${dateTime} into the msg.payload.content

Are you using the core email node node-red-node-email?

The help for that node says

Sends the msg.payload as an email, with a subject of msg.topic.

But you seem to be using msg.payload.content and msg.payload.topic?

msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;
msg.payload.topic = `${dateTime} WARNING!! '${msg.ping.name}' Server DOWN`;

Hi @jbudd you are absolutely correct... excellent spot! Based on the issue you've highlighted I am thinking I need to somehow take the msg.payload.content and redirect it to msg.payload for the core email node? I figure I could use a change node? Or is there a programmatic approach I could use?

I also need to strip out the msg.payload.chatId as that isn't needed for the email, only the Telegram bot.

Thanks @smcgann99 that code is excellent and pointed me in the right direction. Thank you so much, really appreciate the help :+1:

Change function node as follows to set correct payload and remove "un-wanted" data -

const timestamp = Date.now()
const dateTime = new Date(timestamp).toLocaleString()
msg.payload = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;
msg.topic = `${dateTime} WARNING!! '${msg.ping.name}' Server DOWN`;
delete msg.ping
delete msg.payload.chatId
return msg;

object
payload: "Ping to 'Server 5' (192.168.4.14) failed"
topic: "20/09/2022, 02:31:59 WARNING!! 'Server 5' Server DOWN"
_msgid: "d1262ac07cdcddcb"

Hi @smcgann99 tried the code... however it doesn't show the msg.topic for some reason? This is the result...

Ping to 'APOLLO' (192.168.0.14) failed

If I change msg.payload to msg.payload.content then this is the result...

{"type":"message","content":"Ping to 'APOLLO' (192.168.0.14) failed"}

So not sure why the...

msg.topic = `${dateTime} WARNING!! '${msg.ping.name}' Server DOWN`;

Is not showing?

This code appears to work...

const timestamp = Date.now()
const dateTime = new Date(timestamp).toLocaleString()

msg.payload.content = `${dateTime} Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;
msg.payload.topic = `WARNING!! '${msg.ping.name}' Server DOWN`;
delete msg.payload.chatId
delete msg.payload.type
return msg;

Here's what comes out...

9/20/2022, 1:05:51 PM Ping to 'APOLLO' (192.168.0.14) failed
WARNING!! 'APOLLO' Server DOWN

But if I change msg.payload.content to msg.payload it only shows...

9/20/2022, 1:04:42 PM Ping to 'APOLLO' (192.168.0.14) failed

20/09/2022, 08:19:30node: to Email node
20/09/2022, 08:19:32 WARNING!! 'Server 5' Server DOWN : msg.payload : string[40]
"Ping to 'Server 5' (192.168.4.14) failed"

It works for me - try the full flow with debug set to complete msg.object

[{"id":"c18d9ee81483eb22","type":"debug","z":"de795c381afdf5b7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":420,"wires":[]},{"id":"d7bc597446137a4e","type":"inject","z":"de795c381afdf5b7","name":"*** Setup devices to ping here ***","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"name\":\"gateway\",\"host\":\"192.168.4.5\"},{\"name\":\"Server 2\",\"host\":\"192.168.4.7\"},{\"name\":\"Server 1\",\"host\":\"192.168.4.29\"},{\"name\":\"Server 3\",\"host\":\"192.168.4.9\"},{\"name\":\"Server 5\",\"host\":\"192.168.4.14\"}]","payloadType":"json","x":330,"y":420,"wires":[["88ed34f6f40208e6"]]},{"id":"88ed34f6f40208e6","type":"ping","z":"de795c381afdf5b7","mode":"triggered","name":"","host":"","timer":"20","inputs":1,"x":550,"y":420,"wires":[["c18d9ee81483eb22","ec850e934b4a3560"]]},{"id":"ec850e934b4a3560","type":"function","z":"de795c381afdf5b7","name":"Prepare telegram message (if bad)","func":"if(msg.payload === false) {\n    msg.payload = {};\n    msg.payload.type = 'message';\n    msg.payload.content = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\n    msg.payload.chatId = 5612125043\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":500,"wires":[["2146c727b359180b","dbd882ec3cc0864e"]]},{"id":"305bafda3cfebc5c","type":"debug","z":"de795c381afdf5b7","name":"to Email node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":500,"wires":[]},{"id":"2146c727b359180b","type":"function","z":"de795c381afdf5b7","name":"Server Down Email","func":"const timestamp = Date.now()\nconst dateTime = new Date(timestamp).toLocaleString()\nmsg.payload = `Ping to '${msg.ping.name}' (${msg.ping.host}) failed`;\nmsg.topic = `${dateTime} WARNING!! '${msg.ping.name}' Server DOWN`;\ndelete msg.ping\ndelete msg.payload.chatId\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":500,"wires":[["305bafda3cfebc5c"]]},{"id":"dbd882ec3cc0864e","type":"debug","z":"de795c381afdf5b7","name":"to Telegram node","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":560,"wires":[]}]

@smcgann99 Humble apology, you are correct. Thanks again for the help, much appreciated.

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