uptime-kuma/server/notification.js

334 lines
11 KiB
JavaScript
Raw Normal View History

2021-07-09 09:14:03 +03:00
const axios = require("axios");
const {R} = require("redbean-node");
2021-07-09 14:33:22 +03:00
const FormData = require('form-data');
2021-07-09 20:08:08 +03:00
const nodemailer = require("nodemailer");
2021-07-18 13:51:58 +03:00
const child_process = require("child_process");
2021-07-09 09:14:03 +03:00
class Notification {
2021-07-18 13:51:58 +03:00
2021-07-18 15:49:46 +03:00
/**
*
* @param notification
* @param msg
* @param monitorJSON
* @param heartbeatJSON
* @returns {Promise<string>} Successful msg
* Throw Error with fail msg
*/
2021-07-09 14:33:22 +03:00
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-07-18 15:49:46 +03:00
let okMsg = "Sent Successfully. ";
2021-07-18 13:51:58 +03:00
2021-07-09 09:14:03 +03:00
if (notification.type === "telegram") {
2021-07-09 14:33:22 +03:00
try {
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
params: {
chat_id: notification.telegramChatID,
text: msg,
}
})
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-14 12:25:10 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
let msg = (error.response.data.description) ? error.response.data.description : "Error without description"
throw new Error(msg)
2021-07-14 12:25:10 +03:00
}
} else if (notification.type === "gotify") {
try {
2021-07-18 15:49:46 +03:00
if (notification.gotifyserverurl && notification.gotifyserverurl.endsWith("/")) {
2021-07-14 12:25:10 +03:00
notification.gotifyserverurl = notification.gotifyserverurl.slice(0, -1);
}
await axios.post(`${notification.gotifyserverurl}/message?token=${notification.gotifyapplicationToken}`, {
"message": msg,
"priority": notification.gotifyPriority || 8,
2021-07-14 12:25:10 +03:00
"title": "Uptime-Kuma"
})
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-09 14:33:22 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
2021-07-09 14:33:22 +03: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();
finalData.append('data', JSON.stringify(data));
config = {
headers: finalData.getHeaders()
}
} else {
finalData = data;
2021-07-09 09:14:03 +03:00
}
2021-07-09 14:33:22 +03:00
2021-07-22 06:15:53 +03:00
await axios.post(notification.webhookURL, finalData, config)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-09 14:33:22 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
2021-07-09 14:33:22 +03:00
}
2021-07-09 20:08:08 +03:00
} else if (notification.type === "smtp") {
return await Notification.smtp(notification, msg)
2021-07-10 06:38:00 +03:00
} else if (notification.type === "discord") {
try {
// If heartbeatJSON is null, assume we're testing.
if(heartbeatJSON == null) {
let data = {
username: 'Uptime-Kuma',
content: msg
}
2021-07-22 06:15:53 +03:00
await axios.post(notification.discordWebhookUrl, data)
2021-07-18 15:49:46 +03:00
return okMsg;
}
// If heartbeatJSON is not null, we go into the normal alerting loop.
if(heartbeatJSON['status'] == 0) {
var alertColor = "16711680";
} else if(heartbeatJSON['status'] == 1) {
var alertColor = "65280";
}
let data = {
username: 'Uptime-Kuma',
embeds: [{
title: "Uptime-Kuma Alert",
color: alertColor,
fields: [
{
name: "Time (UTC)",
value: heartbeatJSON["time"]
},
{
name: "Message",
value: msg
}
]
}]
}
2021-07-22 06:15:53 +03:00
await axios.post(notification.discordWebhookUrl, data)
2021-07-18 15:49:46 +03:00
return okMsg;
} catch(error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
}
2021-07-10 06:38:00 +03:00
2021-07-12 23:06:03 +03:00
} else if (notification.type === "signal") {
try {
let data = {
"message": msg,
2021-07-12 23:06:03 +03:00
"number": notification.signalNumber,
"recipients": notification.signalRecipients.replace(/\s/g, '').split(",")
};
let config = {};
2021-07-22 06:15:53 +03:00
await axios.post(notification.signalURL, data, config)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-12 23:06:03 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
2021-07-12 23:06:03 +03:00
}
2021-07-14 18:37:14 +03:00
} else if (notification.type === "slack") {
try {
if (heartbeatJSON == null) {
2021-07-17 10:17:52 +03:00
let data = {'text': "Uptime Kuma Slack testing successful.", 'channel': notification.slackchannel, 'username': notification.slackusername, 'icon_emoji': notification.slackiconemo}
2021-07-22 06:15:53 +03:00
await axios.post(notification.slackwebhookURL, data)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-14 18:37:14 +03:00
}
const time = heartbeatJSON["time"];
let data = {
2021-07-17 10:17:52 +03:00
"text": "Uptime Kuma Alert",
"channel":notification.slackchannel,
"username": notification.slackusername,
"icon_emoji": notification.slackiconemo,
2021-07-14 18:37:14 +03:00
"blocks": [{
"type": "header",
"text": {
"type": "plain_text",
"text": "Uptime Kuma Alert"
}
},
{
"type": "section",
"fields": [{
"type": "mrkdwn",
"text": '*Message*\n'+msg
},
{
"type": "mrkdwn",
"text": "*Time (UTC)*\n"+time
}
]
},
{
2021-07-17 10:17:52 +03:00
"type": "actions",
"elements": [
{
"type": "button",
"text": {
2021-07-14 18:37:14 +03:00
"type": "plain_text",
"text": "Visit Uptime Kuma",
},
2021-07-17 10:17:52 +03:00
"value": "Uptime-Kuma",
2021-07-17 16:29:26 +03:00
"url": notification.slackbutton || "https://github.com/louislam/uptime-kuma"
2021-07-17 10:17:52 +03:00
}
2021-07-14 18:37:14 +03:00
]
}
]
}
2021-07-22 06:15:53 +03:00
await axios.post(notification.slackwebhookURL, data)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-14 18:37:14 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
2021-07-17 15:25:02 +03:00
}
} else if (notification.type === "pushover") {
var pushoverlink = 'https://api.pushover.net/1/messages.json'
try {
if (heartbeatJSON == null) {
2021-07-18 13:51:58 +03:00
let data = {'message': "<b>Uptime Kuma Pushover testing successful.</b>",
2021-07-17 15:25:02 +03:00
'user': notification.pushoveruserkey, 'token': notification.pushoverapptoken, 'sound':notification.pushoversounds,
'priority': notification.pushoverpriority, 'title':notification.pushovertitle, 'retry': "30", 'expire':"3600", 'html': 1}
2021-07-22 06:15:53 +03:00
await axios.post(pushoverlink, data)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-17 15:25:02 +03:00
}
let data = {
"message": "<b>Uptime Kuma Alert</b>\n\n<b>Message</b>:" +msg + '\n<b>Time (UTC)</b>:' +time,
"user":notification.pushoveruserkey,
"token": notification.pushoverapptoken,
"sound": notification.pushoversounds,
"priority": notification.pushoverpriority,
"title": notification.pushovertitle,
"retry": "30",
"expire": "3600",
"html": 1
}
2021-07-22 06:15:53 +03:00
await axios.post(pushoverlink, data)
2021-07-18 15:49:46 +03:00
return okMsg;
2021-07-14 18:37:14 +03:00
} catch (error) {
2021-07-18 15:49:46 +03:00
throwGeneralAxiosError(error)
2021-07-14 18:37:14 +03:00
}
2021-07-12 23:06:03 +03:00
2021-07-18 13:51:58 +03:00
} else if (notification.type === "apprise") {
return Notification.apprise(notification, msg)
2021-07-09 09:14:03 +03: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 20:08:08 +03: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
await transporter.sendMail({
2021-07-09 20:08:08 +03:00
from: `"Uptime Kuma" <${notification.smtpFrom}>`,
to: notification.smtpTo,
subject: msg,
text: msg,
});
2021-07-18 15:49:46 +03:00
return "Sent Successfully.";
2021-07-09 20:08:08 +03:00
}
2021-07-10 06:38:00 +03:00
2021-07-18 13:51:58 +03:00
static async apprise(notification, msg) {
let s = child_process.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL])
2021-07-10 06:38:00 +03:00
2021-07-18 15:49:46 +03:00
let output = (s.stdout) ? s.stdout.toString() : 'ERROR: maybe apprise not found';
2021-07-10 06:38:00 +03:00
2021-07-18 13:51:58 +03:00
if (output) {
2021-07-18 15:49:46 +03:00
if (! output.includes("ERROR")) {
return "Sent Successfully";
} else {
throw new Error(output)
2021-07-18 13:51:58 +03:00
}
} else {
2021-07-18 15:49:46 +03:00
return ""
2021-07-18 13:51:58 +03:00
}
2021-07-09 20:08:08 +03:00
}
2021-07-10 06:38:00 +03:00
2021-07-18 13:51:58 +03:00
static checkApprise() {
let commandExistsSync = require('command-exists').sync;
let exists = commandExistsSync('apprise');
return exists;
2021-07-10 06:38:00 +03:00
}
2021-07-18 15:49:46 +03:00
}
2021-07-10 06:38:00 +03:00
2021-07-18 15:49:46 +03:00
function throwGeneralAxiosError(error) {
let msg = "Error: " + error + " ";
2021-07-10 06:38:00 +03:00
2021-07-18 15:49:46 +03: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 06:38:00 +03:00
}
2021-07-18 15:49:46 +03:00
throw new Error(msg)
2021-07-09 09:14:03 +03:00
}
module.exports = {
Notification,
}