2021-07-09 08:14:03 +02:00
|
|
|
const axios = require("axios");
|
2021-07-27 19:47:13 +02:00
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const FormData = require("form-data");
|
2021-07-09 19:08:08 +02:00
|
|
|
const nodemailer = require("nodemailer");
|
2021-07-18 12:51:58 +02:00
|
|
|
const child_process = require("child_process");
|
2021-07-09 08:14:03 +02:00
|
|
|
|
|
|
|
class Notification {
|
2021-07-18 12:51:58 +02:00
|
|
|
|
2021-07-18 14:49:46 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param notification
|
|
|
|
* @param msg
|
|
|
|
* @param monitorJSON
|
|
|
|
* @param heartbeatJSON
|
|
|
|
* @returns {Promise<string>} Successful msg
|
|
|
|
* Throw Error with fail msg
|
|
|
|
*/
|
2021-07-09 13:33:22 +02:00
|
|
|
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-07-18 14:49:46 +02:00
|
|
|
let okMsg = "Sent Successfully. ";
|
2021-07-18 12:51:58 +02:00
|
|
|
|
2021-07-09 08:14:03 +02:00
|
|
|
if (notification.type === "telegram") {
|
2021-07-09 13:33:22 +02:00
|
|
|
try {
|
|
|
|
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
|
|
|
params: {
|
|
|
|
chat_id: notification.telegramChatID,
|
|
|
|
text: msg,
|
2021-07-27 19:47:13 +02:00
|
|
|
},
|
2021-07-09 13:33:22 +02:00
|
|
|
})
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
|
|
|
|
2021-07-14 11:25:10 +02:00
|
|
|
} catch (error) {
|
2021-07-18 14:49:46 +02:00
|
|
|
let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
|
|
|
|
throw new Error(msg)
|
2021-07-14 11:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (notification.type === "gotify") {
|
|
|
|
try {
|
2021-07-18 14:49:46 +02:00
|
|
|
if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
|
2021-07-14 11:25:10 +02:00
|
|
|
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
|
|
|
|
}
|
|
|
|
await axios.post(`${notification.gotifyserverurl}/message?token=${notification.gotifyapplicationToken}`, {
|
|
|
|
"message": msg,
|
2021-07-14 21:56:38 +02:00
|
|
|
"priority": notification.gotifyPriority || 8,
|
2021-07-27 19:47:13 +02:00
|
|
|
"title": "Uptime-Kuma",
|
2021-07-14 11:25:10 +02:00
|
|
|
})
|
2021-07-18 14:49:46 +02:00
|
|
|
|
|
|
|
return okMsg;
|
|
|
|
|
2021-07-09 13:33:22 +02:00
|
|
|
} catch (error) {
|
2021-07-18 14:49:46 +02:00
|
|
|
throwGeneralAxiosError(error)
|
2021-07-09 13:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (notification.type === "webhook") {
|
|
|
|
try {
|
|
|
|
let data = {
|
|
|
|
heartbeat: heartbeatJSON,
|
|
|
|
monitor: monitorJSON,
|
|
|
|
msg,
|
|
|
|
};
|
|
|
|
let finalData;
|
|
|
|
let config = {};
|
|
|
|
|
|
|
|
if (notification.webhookContentType === "form-data") {
|
|
|
|
finalData = new FormData();
|
2021-07-27 19:47:13 +02:00
|
|
|
finalData.append("data", JSON.stringify(data));
|
2021-07-09 13:33:22 +02:00
|
|
|
|
|
|
|
config = {
|
2021-07-27 19:47:13 +02:00
|
|
|
headers: finalData.getHeaders(),
|
2021-07-09 13:33:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
finalData = data;
|
2021-07-09 08:14:03 +02:00
|
|
|
}
|
2021-07-09 13:33:22 +02:00
|
|
|
|
2021-07-22 05:15:53 +02:00
|
|
|
await axios.post(notification.webhookURL, finalData, config)
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
|
|
|
|
2021-07-09 13:33:22 +02:00
|
|
|
} catch (error) {
|
2021-07-18 14:49:46 +02:00
|
|
|
throwGeneralAxiosError(error)
|
2021-07-09 13:33:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:08:08 +02:00
|
|
|
} else if (notification.type === "smtp") {
|
|
|
|
return await Notification.smtp(notification, msg)
|
2021-07-10 05:38:00 +02:00
|
|
|
|
|
|
|
} else if (notification.type === "discord") {
|
2021-07-12 15:13:36 +02:00
|
|
|
try {
|
2021-07-27 19:47:13 +02:00
|
|
|
// If heartbeatJSON is null, assume we're testing.
|
|
|
|
if (heartbeatJSON == null) {
|
2021-08-05 18:11:11 +02:00
|
|
|
let discordtestdata = {
|
2021-08-07 07:42:36 +02:00
|
|
|
username: notification.discordUsername || "Uptime Kuma",
|
2021-07-27 19:47:13 +02:00
|
|
|
content: msg,
|
|
|
|
}
|
2021-08-05 18:11:11 +02:00
|
|
|
await axios.post(notification.discordWebhookUrl, discordtestdata)
|
2021-07-27 19:47:13 +02:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
// If heartbeatJSON is not null, we go into the normal alerting loop.
|
|
|
|
if (heartbeatJSON["status"] == 0) {
|
2021-08-05 18:11:11 +02:00
|
|
|
let discorddowndata = {
|
2021-08-07 20:18:33 +02:00
|
|
|
username: notification.discordUsername || "Uptime Kuma",
|
|
|
|
embeds: [{
|
|
|
|
title: "❌ One of your services went down. ❌",
|
|
|
|
color: 16711680,
|
|
|
|
timestamp: heartbeatJSON["time"],
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: "Service Name",
|
|
|
|
value: monitorJSON["name"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service URL",
|
|
|
|
value: monitorJSON["url"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Time (UTC)",
|
|
|
|
value: heartbeatJSON["time"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
value: heartbeatJSON["msg"],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
await axios.post(notification.discordWebhookUrl, discorddowndata)
|
|
|
|
return okMsg;
|
|
|
|
|
|
|
|
} if (heartbeatJSON["status"] == 1) {
|
2021-08-05 18:11:11 +02:00
|
|
|
let discordupdata = {
|
2021-08-07 20:18:33 +02:00
|
|
|
username: notification.discordUsername || "Uptime Kuma",
|
|
|
|
embeds: [{
|
|
|
|
title: "✅ Your service " + monitorJSON["name"] + " is up! ✅",
|
|
|
|
color: 65280,
|
|
|
|
timestamp: heartbeatJSON["time"],
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
name: "Service Name",
|
|
|
|
value: monitorJSON["name"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service URL",
|
|
|
|
value: "[Visit Service](" + monitorJSON["url"] + ")",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Time (UTC)",
|
|
|
|
value: heartbeatJSON["time"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Ping",
|
|
|
|
value: heartbeatJSON["ping"] + "ms",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
await axios.post(notification.discordWebhookUrl, discordupdata)
|
|
|
|
return okMsg;
|
2021-08-05 18:11:11 +02:00
|
|
|
}
|
2021-07-27 19:47:13 +02:00
|
|
|
} catch (error) {
|
|
|
|
throwGeneralAxiosError(error)
|
2021-07-12 15:13:36 +02:00
|
|
|
}
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-12 22:06:03 +02:00
|
|
|
} else if (notification.type === "signal") {
|
2021-07-27 19:47:13 +02:00
|
|
|
try {
|
|
|
|
let data = {
|
|
|
|
"message": msg,
|
|
|
|
"number": notification.signalNumber,
|
|
|
|
"recipients": notification.signalRecipients.replace(/\s/g, "").split(","),
|
|
|
|
};
|
|
|
|
let config = {};
|
|
|
|
|
|
|
|
await axios.post(notification.signalURL, data, config)
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
throwGeneralAxiosError(error)
|
|
|
|
}
|
2021-07-18 03:10:15 +02:00
|
|
|
|
2021-08-03 17:14:27 +02:00
|
|
|
} else if (notification.type === "pushy") {
|
|
|
|
try {
|
|
|
|
await axios.post(`https://api.pushy.me/push?api_key=${notification.pushyAPIKey}`, {
|
|
|
|
"to": notification.pushyToken,
|
|
|
|
"data": {
|
|
|
|
"message": "Uptime-Kuma"
|
|
|
|
},
|
|
|
|
"notification": {
|
|
|
|
"body": msg,
|
|
|
|
"badge": 1,
|
|
|
|
"sound": "ping.aiff"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-14 17:37:14 +02:00
|
|
|
} else if (notification.type === "slack") {
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
2021-07-27 19:47:13 +02:00
|
|
|
let data = {
|
|
|
|
"text": "Uptime Kuma Slack testing successful.",
|
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
|
|
|
}
|
2021-07-22 05:15:53 +02:00
|
|
|
await axios.post(notification.slackwebhookURL, data)
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
2021-07-14 17:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const time = heartbeatJSON["time"];
|
|
|
|
let data = {
|
2021-07-17 09:17:52 +02:00
|
|
|
"text": "Uptime Kuma Alert",
|
2021-07-27 19:47:13 +02:00
|
|
|
"channel": notification.slackchannel,
|
2021-07-17 09:17:52 +02:00
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2021-07-14 17:37:14 +02:00
|
|
|
"blocks": [{
|
2021-07-27 19:47:13 +02:00
|
|
|
"type": "header",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Uptime Kuma Alert",
|
2021-07-14 17:37:14 +02:00
|
|
|
},
|
2021-07-27 19:47:13 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "section",
|
|
|
|
"fields": [{
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": "*Message*\n" + msg,
|
2021-07-14 17:37:14 +02:00
|
|
|
},
|
|
|
|
{
|
2021-07-27 19:47:13 +02:00
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": "*Time (UTC)*\n" + time,
|
|
|
|
}],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "actions",
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"type": "button",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Visit Uptime Kuma",
|
|
|
|
},
|
|
|
|
"value": "Uptime-Kuma",
|
|
|
|
"url": notification.slackbutton || "https://github.com/louislam/uptime-kuma",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}],
|
|
|
|
}
|
2021-07-22 05:15:53 +02:00
|
|
|
await axios.post(notification.slackwebhookURL, data)
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
2021-07-14 17:37:14 +02:00
|
|
|
} catch (error) {
|
2021-07-18 14:49:46 +02:00
|
|
|
throwGeneralAxiosError(error)
|
2021-07-17 14:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (notification.type === "pushover") {
|
2021-07-27 19:47:13 +02:00
|
|
|
let pushoverlink = "https://api.pushover.net/1/messages.json"
|
2021-07-17 14:25:02 +02:00
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
2021-07-27 19:47:13 +02:00
|
|
|
let data = {
|
|
|
|
"message": "<b>Uptime Kuma Pushover testing successful.</b>",
|
|
|
|
"user": notification.pushoveruserkey,
|
|
|
|
"token": notification.pushoverapptoken,
|
|
|
|
"sound": notification.pushoversounds,
|
|
|
|
"priority": notification.pushoverpriority,
|
|
|
|
"title": notification.pushovertitle,
|
|
|
|
"retry": "30",
|
|
|
|
"expire": "3600",
|
|
|
|
"html": 1,
|
|
|
|
}
|
2021-07-22 05:15:53 +02:00
|
|
|
await axios.post(pushoverlink, data)
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
2021-07-17 14:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
2021-07-27 19:47:13 +02:00
|
|
|
"message": "<b>Uptime Kuma Alert</b>\n\n<b>Message</b>:" + msg + "\n<b>Time (UTC)</b>:" + heartbeatJSON["time"],
|
|
|
|
"user": notification.pushoveruserkey,
|
2021-07-17 14:25:02 +02:00
|
|
|
"token": notification.pushoverapptoken,
|
|
|
|
"sound": notification.pushoversounds,
|
|
|
|
"priority": notification.pushoverpriority,
|
|
|
|
"title": notification.pushovertitle,
|
|
|
|
"retry": "30",
|
|
|
|
"expire": "3600",
|
2021-07-27 19:47:13 +02:00
|
|
|
"html": 1,
|
|
|
|
}
|
2021-07-22 05:15:53 +02:00
|
|
|
await axios.post(pushoverlink, data)
|
2021-07-18 14:49:46 +02:00
|
|
|
return okMsg;
|
2021-07-14 17:37:14 +02:00
|
|
|
} catch (error) {
|
2021-07-18 14:49:46 +02:00
|
|
|
throwGeneralAxiosError(error)
|
2021-07-14 17:37:14 +02:00
|
|
|
}
|
2021-07-12 22:06:03 +02:00
|
|
|
|
2021-07-18 12:51:58 +02:00
|
|
|
} else if (notification.type === "apprise") {
|
|
|
|
|
|
|
|
return Notification.apprise(notification, msg)
|
|
|
|
|
2021-07-31 07:03:20 +02:00
|
|
|
} else if (notification.type === "lunasea") {
|
2021-07-31 07:35:18 +02:00
|
|
|
let lunaseadevice = "https://notify.lunasea.app/v1/custom/device/" + notification.lunaseaDevice
|
2021-07-31 07:03:20 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let testdata = {
|
|
|
|
"title": "Uptime Kuma Alert",
|
2021-07-31 07:35:18 +02:00
|
|
|
"body": "Testing Successful.",
|
2021-07-31 07:03:20 +02:00
|
|
|
}
|
|
|
|
await axios.post(lunaseadevice, testdata)
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heartbeatJSON["status"] == 0) {
|
|
|
|
let downdata = {
|
2021-07-31 07:35:18 +02:00
|
|
|
"title": "UptimeKuma Alert:" + monitorJSON["name"],
|
|
|
|
"body": "[🔴 Down]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"],
|
2021-07-31 07:03:20 +02:00
|
|
|
}
|
|
|
|
await axios.post(lunaseadevice, downdata)
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heartbeatJSON["status"] == 1) {
|
|
|
|
let updata = {
|
2021-07-31 07:35:18 +02:00
|
|
|
"title": "UptimeKuma Alert:" + monitorJSON["name"],
|
|
|
|
"body": "[✅ Up]" + heartbeatJSON["msg"] + "\nTime (UTC):" + heartbeatJSON["time"],
|
2021-07-31 07:03:20 +02:00
|
|
|
}
|
|
|
|
await axios.post(lunaseadevice, updata)
|
|
|
|
return okMsg;
|
|
|
|
}
|
2021-07-31 07:35:18 +02:00
|
|
|
|
|
|
|
} catch (error) {
|
2021-07-31 07:03:20 +02:00
|
|
|
throwGeneralAxiosError(error)
|
|
|
|
}
|
|
|
|
|
2021-07-09 08:14:03 +02:00
|
|
|
} else {
|
|
|
|
throw new Error("Notification type is not supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async save(notification, notificationID, userID) {
|
|
|
|
let bean
|
|
|
|
|
|
|
|
if (notificationID) {
|
|
|
|
bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
|
|
|
|
notificationID,
|
|
|
|
userID,
|
|
|
|
])
|
|
|
|
|
|
|
|
if (! bean) {
|
|
|
|
throw new Error("notification not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
bean = R.dispense("notification")
|
|
|
|
}
|
|
|
|
|
|
|
|
bean.name = notification.name;
|
|
|
|
bean.user_id = userID;
|
|
|
|
bean.config = JSON.stringify(notification)
|
|
|
|
await R.store(bean)
|
|
|
|
}
|
|
|
|
|
|
|
|
static async delete(notificationID, userID) {
|
|
|
|
let bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
|
|
|
|
notificationID,
|
|
|
|
userID,
|
|
|
|
])
|
|
|
|
|
|
|
|
if (! bean) {
|
|
|
|
throw new Error("notification not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
await R.trash(bean)
|
|
|
|
}
|
2021-07-09 19:08:08 +02:00
|
|
|
|
|
|
|
static async smtp(notification, msg) {
|
|
|
|
|
|
|
|
let transporter = nodemailer.createTransport({
|
|
|
|
host: notification.smtpHost,
|
|
|
|
port: notification.smtpPort,
|
|
|
|
secure: notification.smtpSecure,
|
|
|
|
auth: {
|
|
|
|
user: notification.smtpUsername,
|
|
|
|
pass: notification.smtpPassword,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// send mail with defined transport object
|
2021-07-18 03:10:15 +02:00
|
|
|
await transporter.sendMail({
|
2021-07-09 19:08:08 +02:00
|
|
|
from: `"Uptime Kuma" <${notification.smtpFrom}>`,
|
|
|
|
to: notification.smtpTo,
|
|
|
|
subject: msg,
|
|
|
|
text: msg,
|
|
|
|
});
|
|
|
|
|
2021-07-18 14:49:46 +02:00
|
|
|
return "Sent Successfully.";
|
2021-07-09 19:08:08 +02:00
|
|
|
}
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-18 12:51:58 +02:00
|
|
|
static async apprise(notification, msg) {
|
|
|
|
let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL])
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-27 19:47:13 +02:00
|
|
|
let output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found";
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-18 12:51:58 +02:00
|
|
|
if (output) {
|
2021-07-18 14:49:46 +02:00
|
|
|
|
|
|
|
if (! output.includes("ERROR")) {
|
|
|
|
return "Sent Successfully";
|
2021-07-18 12:51:58 +02:00
|
|
|
}
|
2021-07-27 19:47:13 +02:00
|
|
|
|
2021-07-27 20:02:20 +02:00
|
|
|
throw new Error(output)
|
2021-07-18 12:51:58 +02:00
|
|
|
} else {
|
2021-07-18 14:49:46 +02:00
|
|
|
return ""
|
2021-07-18 12:51:58 +02:00
|
|
|
}
|
2021-07-09 19:08:08 +02:00
|
|
|
}
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-18 12:51:58 +02:00
|
|
|
static checkApprise() {
|
2021-07-27 19:47:13 +02:00
|
|
|
let commandExistsSync = require("command-exists").sync;
|
|
|
|
let exists = commandExistsSync("apprise");
|
2021-07-18 12:51:58 +02:00
|
|
|
return exists;
|
2021-07-10 05:38:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 14:49:46 +02:00
|
|
|
}
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-18 14:49:46 +02:00
|
|
|
function throwGeneralAxiosError(error) {
|
|
|
|
let msg = "Error: " + error + " ";
|
2021-07-10 05:38:00 +02:00
|
|
|
|
2021-07-18 14:49:46 +02:00
|
|
|
if (error.response && error.response.data) {
|
|
|
|
if (typeof error.response.data === "string") {
|
|
|
|
msg += error.response.data;
|
|
|
|
} else {
|
|
|
|
msg += JSON.stringify(error.response.data)
|
|
|
|
}
|
2021-07-10 05:38:00 +02:00
|
|
|
}
|
2021-07-18 14:49:46 +02:00
|
|
|
|
|
|
|
throw new Error(msg)
|
2021-07-09 08:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Notification,
|
|
|
|
}
|