uptime-kuma/server/notification.js

209 lines
6.9 KiB
JavaScript
Raw Normal View History

2021-07-27 12:47:13 -05:00
const { R } = require("redbean-node");
2021-09-07 09:42:46 -05:00
const Apprise = require("./notification-providers/apprise");
const Discord = require("./notification-providers/discord");
const Gotify = require("./notification-providers/gotify");
const Line = require("./notification-providers/line");
const LunaSea = require("./notification-providers/lunasea");
const Mattermost = require("./notification-providers/mattermost");
2021-10-05 13:03:56 -05:00
const Matrix = require("./notification-providers/matrix");
2021-09-07 09:42:46 -05:00
const Octopush = require("./notification-providers/octopush");
const PromoSMS = require("./notification-providers/promosms");
const ClickSendSMS = require("./notification-providers/clicksendsms");
2021-09-07 09:42:46 -05:00
const Pushbullet = require("./notification-providers/pushbullet");
const Pushover = require("./notification-providers/pushover");
const Pushy = require("./notification-providers/pushy");
2022-01-21 01:42:03 -06:00
const TechulusPush = require("./notification-providers/techulus-push");
2021-09-07 09:42:46 -05:00
const RocketChat = require("./notification-providers/rocket-chat");
const Signal = require("./notification-providers/signal");
const Slack = require("./notification-providers/slack");
const SMTP = require("./notification-providers/smtp");
const Teams = require("./notification-providers/teams");
2021-09-07 09:42:46 -05:00
const Telegram = require("./notification-providers/telegram");
const Webhook = require("./notification-providers/webhook");
2021-10-11 04:20:09 -05:00
const Feishu = require("./notification-providers/feishu");
2021-10-12 22:55:01 -05:00
const AliyunSms = require("./notification-providers/aliyun-sms");
2021-10-13 03:13:46 -05:00
const DingDing = require("./notification-providers/dingding");
const Bark = require("./notification-providers/bark");
const { log } = require("../src/util");
const SerwerSMS = require("./notification-providers/serwersms");
const Stackfield = require("./notification-providers/stackfield");
2021-12-25 11:33:47 -06:00
const WeCom = require("./notification-providers/wecom");
const GoogleChat = require("./notification-providers/google-chat");
2022-01-09 11:05:11 -06:00
const Gorush = require("./notification-providers/gorush");
2022-01-26 08:54:17 -06:00
const Alerta = require("./notification-providers/alerta");
2022-04-13 21:43:59 -05:00
const OneBot = require("./notification-providers/onebot");
2022-04-14 01:29:54 -05:00
const PushDeer = require("./notification-providers/pushdeer");
2021-07-09 01:14:03 -05:00
class Notification {
2021-07-18 05:51:58 -05:00
2021-09-07 09:42:46 -05:00
providerList = {};
/** Initialize the notification providers */
2021-09-07 09:42:46 -05:00
static init() {
log.info("notification", "Prepare Notification Providers");
2021-09-07 09:42:46 -05:00
this.providerList = {};
const list = [
new Apprise(),
2021-10-12 22:55:01 -05:00
new AliyunSms(),
2021-10-13 03:13:46 -05:00
new DingDing(),
2021-09-07 09:42:46 -05:00
new Discord(),
new Teams(),
2021-09-07 09:42:46 -05:00
new Gotify(),
new Line(),
new LunaSea(),
2021-10-11 04:20:09 -05:00
new Feishu(),
2021-09-07 09:42:46 -05:00
new Mattermost(),
2021-10-05 13:03:56 -05:00
new Matrix(),
2021-09-07 09:42:46 -05:00
new Octopush(),
new PromoSMS(),
new ClickSendSMS(),
2021-09-07 09:42:46 -05:00
new Pushbullet(),
new Pushover(),
new Pushy(),
2022-01-21 01:42:03 -06:00
new TechulusPush(),
2021-09-07 09:42:46 -05:00
new RocketChat(),
new Signal(),
new Slack(),
new SMTP(),
new Telegram(),
new Webhook(),
new Bark(),
new SerwerSMS(),
new Stackfield(),
2021-12-25 11:33:47 -06:00
new WeCom(),
2022-01-26 08:54:17 -06:00
new GoogleChat(),
new Gorush(),
2022-01-26 08:54:17 -06:00
new Alerta(),
2022-04-13 21:43:59 -05:00
new OneBot(),
2022-04-14 01:29:54 -05:00
new PushDeer(),
2021-09-07 09:42:46 -05:00
];
for (let item of list) {
if (! item.name) {
throw new Error("Notification provider without name");
}
if (this.providerList[item.name]) {
throw new Error("Duplicate notification provider name");
}
this.providerList[item.name] = item;
}
}
2021-07-18 07:49:46 -05:00
/**
* Send a notification
* @param {BeanModel} notification
* @param {string} msg General Message
* @param {Object} monitorJSON Monitor details (For Up/Down only)
* @param {Object} heartbeatJSON Heartbeat details (For Up/Down only)
2021-07-18 07:49:46 -05:00
* @returns {Promise<string>} Successful msg
* @throws Error with fail msg
2021-07-18 07:49:46 -05:00
*/
2021-07-09 06:33:22 -05:00
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-09-07 09:42:46 -05:00
if (this.providerList[notification.type]) {
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
2021-07-09 01:14:03 -05:00
} else {
2021-09-07 09:42:46 -05:00
throw new Error("Notification type is not supported");
2021-07-09 01:14:03 -05:00
}
}
/**
* Save a notification
* @param {Object} notification Notification to save
* @param {?number} notificationID ID of notification to update
* @param {number} userID ID of user who adds notification
* @returns {Promise<Bean>}
*/
2021-07-09 01:14:03 -05:00
static async save(notification, notificationID, userID) {
let bean;
2021-07-09 01:14:03 -05:00
if (notificationID) {
bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
]);
2021-07-09 01:14:03 -05:00
if (! bean) {
throw new Error("notification not found");
2021-07-09 01:14:03 -05:00
}
} else {
bean = R.dispense("notification");
2021-07-09 01:14:03 -05:00
}
bean.name = notification.name;
bean.user_id = userID;
bean.config = JSON.stringify(notification);
2021-09-09 07:22:32 -05:00
bean.is_default = notification.isDefault || false;
await R.store(bean);
if (notification.applyExisting) {
await applyNotificationEveryMonitor(bean.id, userID);
}
return bean;
2021-07-09 01:14:03 -05:00
}
/**
* Delete a notification
* @param {number} notificationID ID of notification to delete
* @param {number} userID ID of user who created notification
* @returns {Promise<void>}
*/
2021-07-09 01:14:03 -05:00
static async delete(notificationID, userID) {
let bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
]);
2021-07-09 01:14:03 -05:00
if (! bean) {
throw new Error("notification not found");
2021-07-09 01:14:03 -05:00
}
await R.trash(bean);
2021-07-09 01:14:03 -05:00
}
2021-07-09 12:08:08 -05:00
/**
* Check if apprise exists
* @returns {boolean} Does the command apprise exist?
*/
2021-07-18 05:51:58 -05:00
static checkApprise() {
2021-07-27 12:47:13 -05:00
let commandExistsSync = require("command-exists").sync;
let exists = commandExistsSync("apprise");
2021-07-18 05:51:58 -05:00
return exists;
2021-07-09 22:38:00 -05:00
}
2021-07-18 07:49:46 -05:00
}
2021-07-09 22:38:00 -05:00
/**
* Apply the notification to every monitor
* @param {number} notificationID ID of notification to apply
* @param {number} userID ID of user who created notification
* @returns {Promise<void>}
*/
async function applyNotificationEveryMonitor(notificationID, userID) {
let monitors = await R.getAll("SELECT id FROM monitor WHERE user_id = ?", [
userID
]);
for (let i = 0; i < monitors.length; i++) {
let checkNotification = await R.findOne("monitor_notification", " monitor_id = ? AND notification_id = ? ", [
monitors[i].id,
notificationID,
]);
if (! checkNotification) {
let relation = R.dispense("monitor_notification");
relation.monitor_id = monitors[i].id;
relation.notification_id = notificationID;
await R.store(relation);
}
}
}
2021-07-09 01:14:03 -05:00
module.exports = {
Notification,
};