Use constants for UP/DOWN through notifications class

This commit is contained in:
Domenic Horner 2021-09-04 11:09:34 +08:00
parent 6ae279c7f3
commit e9cd9be03a

View File

@ -4,6 +4,8 @@ const FormData = require("form-data");
const nodemailer = require("nodemailer"); const nodemailer = require("nodemailer");
const child_process = require("child_process"); const child_process = require("child_process");
const { UP, DOWN } = require("../src/util");
class Notification { class Notification {
/** /**
@ -22,7 +24,7 @@ class Notification {
// heartbeatJSON is only defined if we're not testing // heartbeatJSON is only defined if we're not testing
if(heartbeatJSON) { if(heartbeatJSON) {
let text; let text;
if (heartbeatJSON["status"] === 1) { if (heartbeatJSON["status"] === UP) {
text = "✅ Up" text = "✅ Up"
} else { } else {
text = "🔴 Down" text = "🔴 Down"
@ -121,7 +123,7 @@ class Notification {
} }
// If heartbeatJSON is not null, we go into the normal alerting loop. // If heartbeatJSON is not null, we go into the normal alerting loop.
if (heartbeatJSON["status"] == 0) { if (heartbeatJSON["status"] == DOWN) {
let discorddowndata = { let discorddowndata = {
username: discordDisplayName, username: discordDisplayName,
embeds: [{ embeds: [{
@ -151,7 +153,7 @@ class Notification {
await axios.post(notification.discordWebhookUrl, discorddowndata) await axios.post(notification.discordWebhookUrl, discorddowndata)
return okMsg; return okMsg;
} else if (heartbeatJSON["status"] == 1) { } else if (heartbeatJSON["status"] == UP) {
let discordupdata = { let discordupdata = {
username: discordDisplayName, username: discordDisplayName,
embeds: [{ embeds: [{
@ -355,7 +357,7 @@ class Notification {
const mattermostIconEmoji = notification.mattermosticonemo; const mattermostIconEmoji = notification.mattermosticonemo;
const mattermostIconUrl = notification.mattermosticonurl; const mattermostIconUrl = notification.mattermosticonurl;
if (heartbeatJSON["status"] == 0) { if (heartbeatJSON["status"] == DOWN) {
let mattermostdowndata = { let mattermostdowndata = {
username: mattermostUserName, username: mattermostUserName,
text: "Uptime Kuma Alert", text: "Uptime Kuma Alert",
@ -399,7 +401,7 @@ class Notification {
mattermostdowndata mattermostdowndata
); );
return okMsg; return okMsg;
} else if (heartbeatJSON["status"] == 1) { } else if (heartbeatJSON["status"] == UP) {
let mattermostupdata = { let mattermostupdata = {
username: mattermostUserName, username: mattermostUserName,
text: "Uptime Kuma Alert", text: "Uptime Kuma Alert",
@ -501,7 +503,7 @@ class Notification {
return okMsg; return okMsg;
} }
if (heartbeatJSON["status"] == 0) { if (heartbeatJSON["status"] == DOWN) {
let downdata = { let downdata = {
"title": "UptimeKuma Alert: " + monitorJSON["name"], "title": "UptimeKuma Alert: " + monitorJSON["name"],
"body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], "body": "[🔴 Down] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"],
@ -510,7 +512,7 @@ class Notification {
return okMsg; return okMsg;
} }
if (heartbeatJSON["status"] == 1) { if (heartbeatJSON["status"] == UP) {
let updata = { let updata = {
"title": "UptimeKuma Alert: " + monitorJSON["name"], "title": "UptimeKuma Alert: " + monitorJSON["name"],
"body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"], "body": "[✅ Up] " + heartbeatJSON["msg"] + "\nTime (UTC): " + heartbeatJSON["time"],
@ -539,14 +541,14 @@ class Notification {
"body": "Testing Successful.", "body": "Testing Successful.",
} }
await axios.post(pushbulletUrl, testdata, config) await axios.post(pushbulletUrl, testdata, config)
} else if (heartbeatJSON["status"] == 0) { } else if (heartbeatJSON["status"] == DOWN) {
let downdata = { let downdata = {
"type": "note", "type": "note",
"title": "UptimeKuma Alert: " + monitorJSON["name"], "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) await axios.post(pushbulletUrl, downdata, config)
} else if (heartbeatJSON["status"] == 1) { } else if (heartbeatJSON["status"] == UP) {
let updata = { let updata = {
"type": "note", "type": "note",
"title": "UptimeKuma Alert: " + monitorJSON["name"], "title": "UptimeKuma Alert: " + monitorJSON["name"],
@ -578,7 +580,7 @@ class Notification {
] ]
} }
await axios.post(lineAPIUrl, testMessage, config) await axios.post(lineAPIUrl, testMessage, config)
} else if (heartbeatJSON["status"] == 0) { } else if (heartbeatJSON["status"] == DOWN) {
let downMessage = { let downMessage = {
"to": notification.lineUserID, "to": notification.lineUserID,
"messages": [ "messages": [
@ -589,7 +591,7 @@ class Notification {
] ]
} }
await axios.post(lineAPIUrl, downMessage, config) await axios.post(lineAPIUrl, downMessage, config)
} else if (heartbeatJSON["status"] == 1) { } else if (heartbeatJSON["status"] == UP) {
let upMessage = { let upMessage = {
"to": notification.lineUserID, "to": notification.lineUserID,
"messages": [ "messages": [