From 532ad3044cb3e0b7e7771863c2d871f45b1e22b5 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Thu, 2 Sep 2021 21:55:25 +0800 Subject: [PATCH 1/9] Add space to pushbullet and lunasea notifications Start changes regarding standardization of notification messages --- server/notification.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/notification.js b/server/notification.js index 472012af..9417e733 100644 --- a/server/notification.js +++ b/server/notification.js @@ -491,7 +491,7 @@ class Notification { if (heartbeatJSON["status"] == 0) { let downdata = { - "title": "UptimeKuma Alert:" + monitorJSON["name"], + "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[🔴 Down]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], } await axios.post(lunaseadevice, downdata) @@ -500,7 +500,7 @@ class Notification { if (heartbeatJSON["status"] == 1) { let updata = { - "title": "UptimeKuma Alert:" + monitorJSON["name"], + "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[✅ Up]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], } await axios.post(lunaseadevice, updata) @@ -530,14 +530,14 @@ class Notification { } else if (heartbeatJSON["status"] == 0) { let downdata = { "type": "note", - "title": "UptimeKuma Alert:" + monitorJSON["name"], + "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[🔴 Down]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], } await axios.post(pushbulletUrl, downdata, config) } else if (heartbeatJSON["status"] == 1) { let updata = { "type": "note", - "title": "UptimeKuma Alert:" + monitorJSON["name"], + "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[✅ Up]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], } await axios.post(pushbulletUrl, updata, config) From dca5c59982f35a2dc29b1e61f25da198f28cfb30 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 18:24:43 +0800 Subject: [PATCH 2/9] Update lunasea body content Co-authored-by: Adam Stachowicz --- server/notification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification.js b/server/notification.js index 9417e733..fb132ced 100644 --- a/server/notification.js +++ b/server/notification.js @@ -492,7 +492,7 @@ class Notification { if (heartbeatJSON["status"] == 0) { let downdata = { "title": "UptimeKuma Alert: " + monitorJSON["name"], - "body": "[🔴 Down]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], + "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], } await axios.post(lunaseadevice, downdata) return okMsg; From 3bf380c684a902b8c5f5d1f6a76536fa0ca5c98d Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 18:26:10 +0800 Subject: [PATCH 3/9] Update lunasea "up" body content Co-authored-by: Adam Stachowicz --- server/notification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification.js b/server/notification.js index fb132ced..274f99cb 100644 --- a/server/notification.js +++ b/server/notification.js @@ -501,7 +501,7 @@ class Notification { if (heartbeatJSON["status"] == 1) { let updata = { "title": "UptimeKuma Alert: " + monitorJSON["name"], - "body": "[✅ Up]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], + "body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], } await axios.post(lunaseadevice, updata) return okMsg; From d346afd33bb2e349c4f8f08fb3b6d201bab208e2 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 18:26:38 +0800 Subject: [PATCH 4/9] Update pushbullet up body Co-authored-by: Adam Stachowicz --- server/notification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification.js b/server/notification.js index 274f99cb..fecc639c 100644 --- a/server/notification.js +++ b/server/notification.js @@ -538,7 +538,7 @@ class Notification { let updata = { "type": "note", "title": "UptimeKuma Alert: " + monitorJSON["name"], - "body": "[✅ Up]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], + "body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], } await axios.post(pushbulletUrl, updata, config) } From 9c32adfb55bb629bf32b665afb1bece1c9e36da1 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 18:26:44 +0800 Subject: [PATCH 5/9] Update pushbullet down body Co-authored-by: Adam Stachowicz --- server/notification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification.js b/server/notification.js index fecc639c..b9eba5a0 100644 --- a/server/notification.js +++ b/server/notification.js @@ -531,7 +531,7 @@ class Notification { let downdata = { "type": "note", "title": "UptimeKuma Alert: " + monitorJSON["name"], - "body": "[🔴 Down]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"], + "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], } await axios.post(pushbulletUrl, downdata, config) } else if (heartbeatJSON["status"] == 1) { From 6ae279c7f387ffe79a9341eaa9c11e9257d2989f Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 11:06:06 +0800 Subject: [PATCH 6/9] Move title generation to notification class --- server/model/monitor.js | 11 +---------- server/notification.js | 14 +++++++++++++- server/server.js | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 19f21d92..126d59b1 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -270,18 +270,9 @@ class Monitor extends BeanModel { this.id, ]) - let text; - if (bean.status === UP) { - text = "✅ Up" - } else { - text = "🔴 Down" - } - - let msg = `[${this.name}] [${text}] ${bean.msg}`; - for (let notification of notificationList) { try { - await Notification.send(JSON.parse(notification.config), msg, await this.toJSON(), bean.toJSON()) + await Notification.send(JSON.parse(notification.config), this.name, await this.toJSON(), bean.toJSON()) } catch (e) { console.error("Cannot send notification to " + notification.name); console.log(e); diff --git a/server/notification.js b/server/notification.js index b9eba5a0..42c4627d 100644 --- a/server/notification.js +++ b/server/notification.js @@ -15,9 +15,21 @@ class Notification { * @returns {Promise} Successful msg * Throw Error with fail msg */ - static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + static async send(notification, monitorName = null, monitorJSON = null, heartbeatJSON = null) { let okMsg = "Sent Successfully. "; + let msg = `[${notification.name}] Testing`; + // heartbeatJSON is only defined if we're not testing + if(heartbeatJSON) { + let text; + if (heartbeatJSON["status"] === 1) { + text = "✅ Up" + } else { + text = "🔴 Down" + } + msg = `[${monitorName}] [${text}] ${heartbeatJSON["msg"]}`; + } + if (notification.type === "telegram") { try { await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, { diff --git a/server/server.js b/server/server.js index 39191de7..8f4516dc 100644 --- a/server/server.js +++ b/server/server.js @@ -552,7 +552,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); try { checkLogin(socket) - let msg = await Notification.send(notification, notification.name + " Testing") + let msg = await Notification.send(notification) callback({ ok: true, From e9cd9be03abc36f0d6b03df38fcc726a7acfa057 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 11:09:34 +0800 Subject: [PATCH 7/9] Use constants for UP/DOWN through notifications class --- server/notification.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/server/notification.js b/server/notification.js index 42c4627d..520d78dd 100644 --- a/server/notification.js +++ b/server/notification.js @@ -4,6 +4,8 @@ const FormData = require("form-data"); const nodemailer = require("nodemailer"); const child_process = require("child_process"); +const { UP, DOWN } = require("../src/util"); + class Notification { /** @@ -22,7 +24,7 @@ class Notification { // heartbeatJSON is only defined if we're not testing if(heartbeatJSON) { let text; - if (heartbeatJSON["status"] === 1) { + if (heartbeatJSON["status"] === UP) { text = "✅ Up" } else { text = "🔴 Down" @@ -121,7 +123,7 @@ class Notification { } // If heartbeatJSON is not null, we go into the normal alerting loop. - if (heartbeatJSON["status"] == 0) { + if (heartbeatJSON["status"] == DOWN) { let discorddowndata = { username: discordDisplayName, embeds: [{ @@ -151,7 +153,7 @@ class Notification { await axios.post(notification.discordWebhookUrl, discorddowndata) return okMsg; - } else if (heartbeatJSON["status"] == 1) { + } else if (heartbeatJSON["status"] == UP) { let discordupdata = { username: discordDisplayName, embeds: [{ @@ -355,7 +357,7 @@ class Notification { const mattermostIconEmoji = notification.mattermosticonemo; const mattermostIconUrl = notification.mattermosticonurl; - if (heartbeatJSON["status"] == 0) { + if (heartbeatJSON["status"] == DOWN) { let mattermostdowndata = { username: mattermostUserName, text: "Uptime Kuma Alert", @@ -399,7 +401,7 @@ class Notification { mattermostdowndata ); return okMsg; - } else if (heartbeatJSON["status"] == 1) { + } else if (heartbeatJSON["status"] == UP) { let mattermostupdata = { username: mattermostUserName, text: "Uptime Kuma Alert", @@ -501,7 +503,7 @@ class Notification { return okMsg; } - if (heartbeatJSON["status"] == 0) { + if (heartbeatJSON["status"] == DOWN) { let downdata = { "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], @@ -510,7 +512,7 @@ class Notification { return okMsg; } - if (heartbeatJSON["status"] == 1) { + if (heartbeatJSON["status"] == UP) { let updata = { "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], @@ -539,14 +541,14 @@ class Notification { "body": "Testing Successful.", } await axios.post(pushbulletUrl, testdata, config) - } else if (heartbeatJSON["status"] == 0) { + } else if (heartbeatJSON["status"] == DOWN) { let downdata = { "type": "note", "title": "UptimeKuma Alert: " + monitorJSON["name"], "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], } await axios.post(pushbulletUrl, downdata, config) - } else if (heartbeatJSON["status"] == 1) { + } else if (heartbeatJSON["status"] == UP) { let updata = { "type": "note", "title": "UptimeKuma Alert: " + monitorJSON["name"], @@ -578,7 +580,7 @@ class Notification { ] } await axios.post(lineAPIUrl, testMessage, config) - } else if (heartbeatJSON["status"] == 0) { + } else if (heartbeatJSON["status"] == DOWN) { let downMessage = { "to": notification.lineUserID, "messages": [ @@ -589,7 +591,7 @@ class Notification { ] } await axios.post(lineAPIUrl, downMessage, config) - } else if (heartbeatJSON["status"] == 1) { + } else if (heartbeatJSON["status"] == UP) { let upMessage = { "to": notification.lineUserID, "messages": [ From b0acda52f9b6b47aedb3ce1b05ea78d73c6b7be0 Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 11:27:18 +0800 Subject: [PATCH 8/9] Add time to smtp body content --- server/notification.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/notification.js b/server/notification.js index 520d78dd..beb1ff9d 100644 --- a/server/notification.js +++ b/server/notification.js @@ -94,7 +94,7 @@ class Notification { } } else if (notification.type === "smtp") { - return await Notification.smtp(notification, msg) + return await Notification.smtp(notification, msg, heartbeatJSON) } else if (notification.type === "discord") { try { @@ -648,7 +648,7 @@ class Notification { await R.trash(bean) } - static async smtp(notification, msg) { + static async smtp(notification, msg, heartbeatJSON = null) { const config = { host: notification.smtpHost, @@ -666,12 +666,17 @@ class Notification { let transporter = nodemailer.createTransport(config); + let bodyTextContent = msg; + if(heartbeatJSON) { + bodyTextContent = `${msg}\nTime (UTC): ${heartbeatJSON["time"]}`; + } + // send mail with defined transport object await transporter.sendMail({ from: `"Uptime Kuma" <${notification.smtpFrom}>`, to: notification.smtpTo, subject: msg, - text: msg, + text: bodyTextContent, }); return "Sent Successfully."; From 41d32bb9dde554794613df002696841d6dceab1f Mon Sep 17 00:00:00 2001 From: Domenic Horner Date: Sat, 4 Sep 2021 20:08:18 +0800 Subject: [PATCH 9/9] Undo parm changes --- server/model/monitor.js | 11 ++++++++++- server/notification.js | 14 +------------- server/server.js | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 126d59b1..19f21d92 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -270,9 +270,18 @@ class Monitor extends BeanModel { this.id, ]) + let text; + if (bean.status === UP) { + text = "✅ Up" + } else { + text = "🔴 Down" + } + + let msg = `[${this.name}] [${text}] ${bean.msg}`; + for (let notification of notificationList) { try { - await Notification.send(JSON.parse(notification.config), this.name, await this.toJSON(), bean.toJSON()) + await Notification.send(JSON.parse(notification.config), msg, await this.toJSON(), bean.toJSON()) } catch (e) { console.error("Cannot send notification to " + notification.name); console.log(e); diff --git a/server/notification.js b/server/notification.js index beb1ff9d..f78401d9 100644 --- a/server/notification.js +++ b/server/notification.js @@ -17,21 +17,9 @@ class Notification { * @returns {Promise} Successful msg * Throw Error with fail msg */ - static async send(notification, monitorName = null, monitorJSON = null, heartbeatJSON = null) { + static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { let okMsg = "Sent Successfully. "; - let msg = `[${notification.name}] Testing`; - // heartbeatJSON is only defined if we're not testing - if(heartbeatJSON) { - let text; - if (heartbeatJSON["status"] === UP) { - text = "✅ Up" - } else { - text = "🔴 Down" - } - msg = `[${monitorName}] [${text}] ${heartbeatJSON["msg"]}`; - } - if (notification.type === "telegram") { try { await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, { diff --git a/server/server.js b/server/server.js index 8f4516dc..39191de7 100644 --- a/server/server.js +++ b/server/server.js @@ -552,7 +552,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); try { checkLogin(socket) - let msg = await Notification.send(notification) + let msg = await Notification.send(notification, notification.name + " Testing") callback({ ok: true,