mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-12-27 22:49:42 -08:00
168 lines
4.9 KiB
JavaScript
168 lines
4.9 KiB
JavaScript
const axios = require("axios");
|
|
const {R} = require("redbean-node");
|
|
const FormData = require('form-data');
|
|
const nodemailer = require("nodemailer");
|
|
|
|
class Notification {
|
|
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
if (notification.type === "telegram") {
|
|
try {
|
|
await axios.get(`https://api.telegram.org/bot${notification.telegramBotToken}/sendMessage`, {
|
|
params: {
|
|
chat_id: notification.telegramChatID,
|
|
text: msg,
|
|
}
|
|
})
|
|
return true;
|
|
} catch (error) {
|
|
console.log(error)
|
|
return false;
|
|
}
|
|
|
|
} 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;
|
|
}
|
|
|
|
let res = await axios.post(notification.webhookURL, finalData, config)
|
|
return true;
|
|
} catch (error) {
|
|
console.log(error)
|
|
return false;
|
|
}
|
|
|
|
} else if (notification.type === "smtp") {
|
|
return await Notification.smtp(notification, msg)
|
|
|
|
} else if (notification.type === "discord") {
|
|
try {
|
|
// If heartbeatJSON is null, assume we're testing.
|
|
if(heartbeatJSON == null) {
|
|
let data = {
|
|
username: 'Uptime-Kuma',
|
|
content: msg
|
|
}
|
|
let res = await axios.post(notification.discordWebhookUrl, data)
|
|
return true;
|
|
}
|
|
// 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
|
|
}
|
|
]
|
|
}]
|
|
}
|
|
let res = await axios.post(notification.discordWebhookUrl, data)
|
|
return true;
|
|
} catch(error) {
|
|
console.log(error)
|
|
return false;
|
|
}
|
|
} 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)
|
|
}
|
|
|
|
static async smtp(notification, msg) {
|
|
|
|
let data = {
|
|
host: notification.smtpHost,
|
|
port: notification.smtpPort,
|
|
secure: notification.smtpSecure,
|
|
};
|
|
|
|
if (notification.smtpUsername) {
|
|
data.auth = {
|
|
user: notification.smtpUsername,
|
|
pass: notification.smtpPassword,
|
|
};
|
|
}
|
|
|
|
let transporter = nodemailer.createTransport(data);
|
|
|
|
// send mail with defined transport object
|
|
let info = await transporter.sendMail({
|
|
from: `"Uptime Kuma" <${notification.smtpFrom}>`,
|
|
to: notification.smtpTo,
|
|
subject: msg,
|
|
text: msg,
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
Notification,
|
|
}
|