# Telegram and Email Alerts

**URL:** <https://discourse.nodered.org/t/telegram-and-email-alerts/67950>\
**Category:** General\
**Created:** [19 September 2022 22:29 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950 "2022-09-19T22:29:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [19 September 2022 22:29 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/1 "2022-09-19T22:29:28Z")

</div>

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...

```auto
[
    {
        "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

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [19 September 2022 23:38 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/2 "2022-09-19T23:38:13Z")

</div>

This should do it for you -

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [19 September 2022 23:56 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/3 "2022-09-19T23:56:31Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [20 September 2022 01:10 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/4 "2022-09-20T01:10:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [20 September 2022 01:12 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/5 "2022-09-20T01:12:22Z")

</div>

Thanks @Sean-McG that code is excellent and pointed me in the right direction. Thank you so much, really appreciate the help 👍

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [20 September 2022 01:32 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/6 "2022-09-20T01:32:47Z")

</div>

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

```auto
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"**

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [20 September 2022 02:54 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/7 "2022-09-20T02:54:41Z")

</div>

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

```auto
Ping to 'APOLLO' (192.168.0.14) failed

```

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

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

```

So not sure why the...

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

```

Is not showing?

This code appears to work...

```auto
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...

```auto
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`

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [20 September 2022 07:19 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/8 "2022-09-20T07:19:05Z")

</div>

20/09/2022, 08:19:30[node: to Email node](http://192.168.1.254:1880/#)  
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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![gregoinc](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@gregoinc](https://discourse.nodered.org/u/gregoinc)\
**Post date:** [20 September 2022 08:40 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/9 "2022-09-20T08:40:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 October 2022 08:41 UTC](https://discourse.nodered.org/t/telegram-and-email-alerts/67950/10 "2022-10-04T08:41:24Z")

</div>

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