uptime-kuma/server/notification.js

281 lines
10 KiB
JavaScript
Raw Normal View History

2021-07-27 13:47:13 -04:00
const { R } = require("redbean-node");
const { log } = require("../src/util");
const Alerta = require("./notification-providers/alerta");
const AlertNow = require("./notification-providers/alertnow");
const AliyunSms = require("./notification-providers/aliyun-sms");
2021-09-07 10:42:46 -04:00
const Apprise = require("./notification-providers/apprise");
const Bark = require("./notification-providers/bark");
const Bitrix24 = require("./notification-providers/bitrix24");
const ClickSendSMS = require("./notification-providers/clicksendsms");
const CallMeBot = require("./notification-providers/call-me-bot");
const SMSC = require("./notification-providers/smsc");
const DingDing = require("./notification-providers/dingding");
2021-09-07 10:42:46 -04:00
const Discord = require("./notification-providers/discord");
const Feishu = require("./notification-providers/feishu");
const FreeMobile = require("./notification-providers/freemobile");
const GoogleChat = require("./notification-providers/google-chat");
const Gorush = require("./notification-providers/gorush");
2021-09-07 10:42:46 -04:00
const Gotify = require("./notification-providers/gotify");
const GrafanaOncall = require("./notification-providers/grafana-oncall");
const HomeAssistant = require("./notification-providers/home-assistant");
2024-02-12 18:58:54 -05:00
const HeiiOnCall = require("./notification-providers/heii-oncall");
const Keep = require("./notification-providers/keep");
2022-12-27 01:05:45 -05:00
const Kook = require("./notification-providers/kook");
2021-09-07 10:42:46 -04:00
const Line = require("./notification-providers/line");
const LineNotify = require("./notification-providers/linenotify");
2021-09-07 10:42:46 -04:00
const LunaSea = require("./notification-providers/lunasea");
2021-10-05 14:03:56 -04:00
const Matrix = require("./notification-providers/matrix");
const Mattermost = require("./notification-providers/mattermost");
const Nostr = require("./notification-providers/nostr");
const Ntfy = require("./notification-providers/ntfy");
2021-09-07 10:42:46 -04:00
const Octopush = require("./notification-providers/octopush");
const OneBot = require("./notification-providers/onebot");
2023-03-21 13:07:19 -04:00
const Opsgenie = require("./notification-providers/opsgenie");
const PagerDuty = require("./notification-providers/pagerduty");
const FlashDuty = require("./notification-providers/flashduty");
2023-02-06 13:33:14 -05:00
const PagerTree = require("./notification-providers/pagertree");
const PromoSMS = require("./notification-providers/promosms");
2021-09-07 10:42:46 -04:00
const Pushbullet = require("./notification-providers/pushbullet");
const PushDeer = require("./notification-providers/pushdeer");
2021-09-07 10:42:46 -04:00
const Pushover = require("./notification-providers/pushover");
const Pushy = require("./notification-providers/pushy");
const RocketChat = require("./notification-providers/rocket-chat");
const SerwerSMS = require("./notification-providers/serwersms");
2021-09-07 10:42:46 -04:00
const Signal = require("./notification-providers/signal");
const SIGNL4 = require("./notification-providers/signl4");
2021-09-07 10:42:46 -04:00
const Slack = require("./notification-providers/slack");
const SMSPartner = require("./notification-providers/smspartner");
const SMSEagle = require("./notification-providers/smseagle");
2021-09-07 10:42:46 -04:00
const SMTP = require("./notification-providers/smtp");
const Squadcast = require("./notification-providers/squadcast");
const Stackfield = require("./notification-providers/stackfield");
const Teams = require("./notification-providers/teams");
const TechulusPush = require("./notification-providers/techulus-push");
2021-09-07 10:42:46 -04:00
const Telegram = require("./notification-providers/telegram");
const Threema = require("./notification-providers/threema");
2023-03-25 12:56:01 -04:00
const Twilio = require("./notification-providers/twilio");
2022-10-13 06:15:02 -04:00
const Splunk = require("./notification-providers/splunk");
2021-09-07 10:42:46 -04:00
const Webhook = require("./notification-providers/webhook");
2021-12-25 12:33:47 -05:00
const WeCom = require("./notification-providers/wecom");
const GoAlert = require("./notification-providers/goalert");
const SMSManager = require("./notification-providers/smsmanager");
const ServerChan = require("./notification-providers/serverchan");
2022-12-08 06:32:10 -05:00
const ZohoCliq = require("./notification-providers/zoho-cliq");
const SevenIO = require("./notification-providers/sevenio");
const Whapi = require("./notification-providers/whapi");
const GtxMessaging = require("./notification-providers/gtx-messaging");
const Cellsynt = require("./notification-providers/cellsynt");
const Onesender = require("./notification-providers/onesender");
const Wpush = require("./notification-providers/wpush");
2021-07-09 02:14:03 -04:00
class Notification {
2024-02-12 20:24:42 -05:00
2021-09-07 10:42:46 -04:00
providerList = {};
/**
* Initialize the notification providers
* @returns {void}
* @throws Notification provider does not have a name
* @throws Duplicate notification providers in list
*/
2021-09-07 10:42:46 -04:00
static init() {
log.debug("notification", "Prepare Notification Providers");
2021-09-07 10:42:46 -04:00
this.providerList = {};
const list = [
new Alerta(),
new AlertNow(),
2021-10-12 23:55:01 -04:00
new AliyunSms(),
new Apprise(),
new Bark(),
new Bitrix24(),
new ClickSendSMS(),
new CallMeBot(),
new SMSC(),
2021-10-13 04:13:46 -04:00
new DingDing(),
2021-09-07 10:42:46 -04:00
new Discord(),
new Feishu(),
new FreeMobile(),
new GoogleChat(),
new Gorush(),
2021-09-07 10:42:46 -04:00
new Gotify(),
new GrafanaOncall(),
new HomeAssistant(),
2024-02-12 18:58:54 -05:00
new HeiiOnCall(),
new Keep(),
2022-12-27 01:05:45 -05:00
new Kook(),
2021-09-07 10:42:46 -04:00
new Line(),
new LineNotify(),
2021-09-07 10:42:46 -04:00
new LunaSea(),
2021-10-05 14:03:56 -04:00
new Matrix(),
new Mattermost(),
new Nostr(),
new Ntfy(),
2021-09-07 10:42:46 -04:00
new Octopush(),
new OneBot(),
new Onesender(),
2023-03-21 14:45:44 -04:00
new Opsgenie(),
new PagerDuty(),
new FlashDuty(),
2023-02-06 13:33:14 -05:00
new PagerTree(),
new PromoSMS(),
2021-09-07 10:42:46 -04:00
new Pushbullet(),
new PushDeer(),
2021-09-07 10:42:46 -04:00
new Pushover(),
new Pushy(),
new RocketChat(),
new ServerChan(),
new SerwerSMS(),
2021-09-07 10:42:46 -04:00
new Signal(),
new SIGNL4(),
new SMSManager(),
new SMSPartner(),
2021-09-07 10:42:46 -04:00
new Slack(),
new SMSEagle(),
2021-09-07 10:42:46 -04:00
new SMTP(),
new Squadcast(),
new Stackfield(),
new Teams(),
new TechulusPush(),
2021-09-07 10:42:46 -04:00
new Telegram(),
new Threema(),
2023-03-25 12:56:01 -04:00
new Twilio(),
2022-10-13 06:15:02 -04:00
new Splunk(),
2021-09-07 10:42:46 -04:00
new Webhook(),
2021-12-25 12:33:47 -05:00
new WeCom(),
2022-08-23 13:52:54 -04:00
new GoAlert(),
new ZohoCliq(),
new SevenIO(),
new Whapi(),
new GtxMessaging(),
new Cellsynt(),
new Wpush(),
2021-09-07 10:42:46 -04:00
];
for (let item of list) {
2024-02-12 20:24:42 -05:00
if (! item.name) {
2021-09-07 10:42:46 -04:00
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 08:49:46 -04:00
/**
* Send a notification
* @param {BeanModel} notification Notification to send
* @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 08:49:46 -04:00
* @returns {Promise<string>} Successful msg
* @throws Error with fail msg
2021-07-18 08:49:46 -04:00
*/
2024-02-12 20:24:42 -05:00
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-09-07 10:42:46 -04:00
if (this.providerList[notification.type]) {
2024-02-12 20:24:42 -05:00
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
2021-07-09 02:14:03 -04:00
} else {
2021-09-07 10:42:46 -04:00
throw new Error("Notification type is not supported");
2021-07-09 02:14:03 -04: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>} Notification that was saved
*/
2021-07-09 02:14:03 -04:00
static async save(notification, notificationID, userID) {
let bean;
2021-07-09 02:14:03 -04:00
if (notificationID) {
bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
]);
2021-07-09 02:14:03 -04:00
2024-02-12 20:24:42 -05:00
if (! bean) {
throw new Error("notification not found");
2021-07-09 02:14:03 -04:00
}
2024-02-12 20:24:42 -05:00
2021-07-09 02:14:03 -04:00
} else {
bean = R.dispense("notification");
2021-07-09 02:14:03 -04:00
}
bean.name = notification.name;
bean.user_id = userID;
bean.config = JSON.stringify(notification);
2021-09-09 08:22:32 -04:00
bean.is_default = notification.isDefault || false;
await R.store(bean);
if (notification.applyExisting) {
await applyNotificationEveryMonitor(bean.id, userID);
}
return bean;
2021-07-09 02:14:03 -04: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 02:14:03 -04:00
static async delete(notificationID, userID) {
let bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
notificationID,
userID,
]);
2021-07-09 02:14:03 -04:00
2024-02-12 20:24:42 -05:00
if (! bean) {
throw new Error("notification not found");
2021-07-09 02:14:03 -04:00
}
await R.trash(bean);
2021-07-09 02:14:03 -04:00
}
2021-07-09 13:08:08 -04:00
/**
* Check if apprise exists
* @returns {boolean} Does the command apprise exist?
*/
2021-07-18 06:51:58 -04:00
static checkApprise() {
2021-07-27 13:47:13 -04:00
let commandExistsSync = require("command-exists").sync;
let exists = commandExistsSync("apprise");
2021-07-18 06:51:58 -04:00
return exists;
2021-07-09 23:38:00 -04:00
}
2024-02-12 20:24:42 -05:00
2021-07-18 08:49:46 -04:00
}
2021-07-09 23:38:00 -04: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 = ?", [
2024-02-12 20:24:42 -05:00
userID
]);
for (let i = 0; i < monitors.length; i++) {
2024-02-12 20:24:42 -05:00
let checkNotification = await R.findOne("monitor_notification", " monitor_id = ? AND notification_id = ? ", [
monitors[i].id,
notificationID,
]);
2024-02-12 20:24:42 -05:00
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 02:14:03 -04:00
module.exports = {
Notification,
};