2021-07-27 20:47:13 +03:00
|
|
|
const { R } = require("redbean-node");
|
2022-06-11 22:02:44 +03:00
|
|
|
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 17:42:46 +03:00
|
|
|
const Apprise = require("./notification-providers/apprise");
|
2022-06-11 22:02:44 +03:00
|
|
|
const Bark = require("./notification-providers/bark");
|
|
|
|
const ClickSendSMS = require("./notification-providers/clicksendsms");
|
|
|
|
const DingDing = require("./notification-providers/dingding");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Discord = require("./notification-providers/discord");
|
2022-06-11 22:02:44 +03:00
|
|
|
const Feishu = require("./notification-providers/feishu");
|
2022-10-05 12:30:49 +03:00
|
|
|
const FreeMobile = require("./notification-providers/freemobile");
|
2022-06-11 22:02:44 +03:00
|
|
|
const GoogleChat = require("./notification-providers/google-chat");
|
|
|
|
const Gorush = require("./notification-providers/gorush");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Gotify = require("./notification-providers/gotify");
|
2022-07-31 14:41:18 +03:00
|
|
|
const HomeAssistant = require("./notification-providers/home-assistant");
|
2022-12-27 09:05:45 +03:00
|
|
|
const Kook = require("./notification-providers/kook");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Line = require("./notification-providers/line");
|
2022-07-29 10:13:50 +03:00
|
|
|
const LineNotify = require("./notification-providers/linenotify");
|
2021-09-07 17:42:46 +03:00
|
|
|
const LunaSea = require("./notification-providers/lunasea");
|
2021-10-05 21:03:56 +03:00
|
|
|
const Matrix = require("./notification-providers/matrix");
|
2022-06-11 22:02:44 +03:00
|
|
|
const Mattermost = require("./notification-providers/mattermost");
|
|
|
|
const Ntfy = require("./notification-providers/ntfy");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Octopush = require("./notification-providers/octopush");
|
2022-06-11 22:02:44 +03:00
|
|
|
const OneBot = require("./notification-providers/onebot");
|
|
|
|
const PagerDuty = require("./notification-providers/pagerduty");
|
2021-10-07 22:56:32 +03:00
|
|
|
const PromoSMS = require("./notification-providers/promosms");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Pushbullet = require("./notification-providers/pushbullet");
|
2022-06-11 22:02:44 +03:00
|
|
|
const PushDeer = require("./notification-providers/pushdeer");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Pushover = require("./notification-providers/pushover");
|
|
|
|
const Pushy = require("./notification-providers/pushy");
|
|
|
|
const RocketChat = require("./notification-providers/rocket-chat");
|
2022-06-11 22:02:44 +03:00
|
|
|
const SerwerSMS = require("./notification-providers/serwersms");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Signal = require("./notification-providers/signal");
|
|
|
|
const Slack = require("./notification-providers/slack");
|
2022-10-24 13:45:56 +03:00
|
|
|
const SMSEagle = require("./notification-providers/smseagle");
|
2021-09-07 17:42:46 +03:00
|
|
|
const SMTP = require("./notification-providers/smtp");
|
2022-09-26 12:16:34 +03:00
|
|
|
const Squadcast = require("./notification-providers/squadcast");
|
2022-06-11 22:02:44 +03:00
|
|
|
const Stackfield = require("./notification-providers/stackfield");
|
2021-09-12 20:46:59 +03:00
|
|
|
const Teams = require("./notification-providers/teams");
|
2022-06-11 22:02:44 +03:00
|
|
|
const TechulusPush = require("./notification-providers/techulus-push");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Telegram = require("./notification-providers/telegram");
|
2022-10-13 13:15:02 +03:00
|
|
|
const Splunk = require("./notification-providers/splunk");
|
2021-09-07 17:42:46 +03:00
|
|
|
const Webhook = require("./notification-providers/webhook");
|
2021-12-25 20:33:47 +03:00
|
|
|
const WeCom = require("./notification-providers/wecom");
|
2022-08-23 20:56:20 +03:00
|
|
|
const GoAlert = require("./notification-providers/goalert");
|
2022-09-15 10:11:05 +03:00
|
|
|
const SMSManager = require("./notification-providers/smsmanager");
|
2022-09-23 18:27:22 +03:00
|
|
|
const ServerChan = require("./notification-providers/serverchan");
|
2022-12-08 14:32:10 +03:00
|
|
|
const ZohoCliq = require("./notification-providers/zoho-cliq");
|
2021-09-04 06:09:34 +03:00
|
|
|
|
2021-07-09 09:14:03 +03:00
|
|
|
class Notification {
|
2021-07-18 13:51:58 +03:00
|
|
|
|
2021-09-07 17:42:46 +03:00
|
|
|
providerList = {};
|
|
|
|
|
2022-04-20 21:56:40 +03:00
|
|
|
/** Initialize the notification providers */
|
2021-09-07 17:42:46 +03:00
|
|
|
static init() {
|
2022-04-13 18:33:37 +03:00
|
|
|
log.info("notification", "Prepare Notification Providers");
|
2021-09-07 17:42:46 +03:00
|
|
|
|
|
|
|
this.providerList = {};
|
|
|
|
|
|
|
|
const list = [
|
2022-06-11 22:02:44 +03:00
|
|
|
new Alerta(),
|
|
|
|
new AlertNow(),
|
2021-10-13 06:55:01 +03:00
|
|
|
new AliyunSms(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new Apprise(),
|
|
|
|
new Bark(),
|
|
|
|
new ClickSendSMS(),
|
2021-10-13 11:13:46 +03:00
|
|
|
new DingDing(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Discord(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new Feishu(),
|
2022-10-05 12:30:49 +03:00
|
|
|
new FreeMobile(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new GoogleChat(),
|
|
|
|
new Gorush(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Gotify(),
|
2022-07-31 14:41:18 +03:00
|
|
|
new HomeAssistant(),
|
2022-12-27 09:05:45 +03:00
|
|
|
new Kook(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Line(),
|
2022-07-29 10:13:50 +03:00
|
|
|
new LineNotify(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new LunaSea(),
|
2021-10-05 21:03:56 +03:00
|
|
|
new Matrix(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new Mattermost(),
|
|
|
|
new Ntfy(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Octopush(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new OneBot(),
|
|
|
|
new PagerDuty(),
|
2021-10-07 22:56:32 +03:00
|
|
|
new PromoSMS(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Pushbullet(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new PushDeer(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Pushover(),
|
|
|
|
new Pushy(),
|
|
|
|
new RocketChat(),
|
2022-09-23 18:27:22 +03:00
|
|
|
new ServerChan(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new SerwerSMS(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Signal(),
|
2022-09-15 10:11:05 +03:00
|
|
|
new SMSManager(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Slack(),
|
2022-10-24 13:45:56 +03:00
|
|
|
new SMSEagle(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new SMTP(),
|
2022-09-26 12:16:34 +03:00
|
|
|
new Squadcast(),
|
2022-06-11 22:02:44 +03:00
|
|
|
new Stackfield(),
|
|
|
|
new Teams(),
|
|
|
|
new TechulusPush(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Telegram(),
|
2022-10-13 13:15:02 +03:00
|
|
|
new Splunk(),
|
2021-09-07 17:42:46 +03:00
|
|
|
new Webhook(),
|
2021-12-25 20:33:47 +03:00
|
|
|
new WeCom(),
|
2022-08-23 20:52:54 +03:00
|
|
|
new GoAlert(),
|
2022-12-08 14:32:10 +03:00
|
|
|
new ZohoCliq()
|
2021-09-07 17:42:46 +03: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 15:49:46 +03:00
|
|
|
/**
|
2022-04-20 21:56:40 +03: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 15:49:46 +03:00
|
|
|
* @returns {Promise<string>} Successful msg
|
2022-04-20 21:56:40 +03:00
|
|
|
* @throws Error with fail msg
|
2021-07-18 15:49:46 +03:00
|
|
|
*/
|
2021-07-09 14:33:22 +03:00
|
|
|
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-09-07 17:42:46 +03:00
|
|
|
if (this.providerList[notification.type]) {
|
|
|
|
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
|
2021-07-09 09:14:03 +03:00
|
|
|
} else {
|
2021-09-07 17:42:46 +03:00
|
|
|
throw new Error("Notification type is not supported");
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:56:40 +03: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 09:14:03 +03:00
|
|
|
static async save(notification, notificationID, userID) {
|
2022-04-20 21:56:40 +03:00
|
|
|
let bean;
|
2021-07-09 09:14:03 +03:00
|
|
|
|
|
|
|
if (notificationID) {
|
|
|
|
bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
|
|
|
|
notificationID,
|
|
|
|
userID,
|
2022-04-20 21:56:40 +03:00
|
|
|
]);
|
2021-07-09 09:14:03 +03:00
|
|
|
|
|
|
|
if (! bean) {
|
2022-04-20 21:56:40 +03:00
|
|
|
throw new Error("notification not found");
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-04-20 21:56:40 +03:00
|
|
|
bean = R.dispense("notification");
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bean.name = notification.name;
|
|
|
|
bean.user_id = userID;
|
2021-09-06 00:23:06 +03:00
|
|
|
bean.config = JSON.stringify(notification);
|
2021-09-09 15:22:32 +03:00
|
|
|
bean.is_default = notification.isDefault || false;
|
2022-04-20 21:56:40 +03:00
|
|
|
await R.store(bean);
|
2021-09-06 00:23:06 +03:00
|
|
|
|
|
|
|
if (notification.applyExisting) {
|
|
|
|
await applyNotificationEveryMonitor(bean.id, userID);
|
|
|
|
}
|
2021-09-09 16:24:29 +03:00
|
|
|
|
|
|
|
return bean;
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:56:40 +03: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 09:14:03 +03:00
|
|
|
static async delete(notificationID, userID) {
|
|
|
|
let bean = await R.findOne("notification", " id = ? AND user_id = ? ", [
|
|
|
|
notificationID,
|
|
|
|
userID,
|
2022-04-20 21:56:40 +03:00
|
|
|
]);
|
2021-07-09 09:14:03 +03:00
|
|
|
|
|
|
|
if (! bean) {
|
2022-04-20 21:56:40 +03:00
|
|
|
throw new Error("notification not found");
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:56:40 +03:00
|
|
|
await R.trash(bean);
|
2021-07-09 09:14:03 +03:00
|
|
|
}
|
2021-07-09 20:08:08 +03:00
|
|
|
|
2022-04-20 21:56:40 +03:00
|
|
|
/**
|
|
|
|
* Check if apprise exists
|
|
|
|
* @returns {boolean} Does the command apprise exist?
|
|
|
|
*/
|
2021-07-18 13:51:58 +03:00
|
|
|
static checkApprise() {
|
2021-07-27 20:47:13 +03:00
|
|
|
let commandExistsSync = require("command-exists").sync;
|
|
|
|
let exists = commandExistsSync("apprise");
|
2021-07-18 13:51:58 +03:00
|
|
|
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
|
|
|
|
2022-04-20 21:56:40 +03: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>}
|
|
|
|
*/
|
2021-09-06 00:23:06 +03:00
|
|
|
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,
|
2022-04-20 21:56:40 +03:00
|
|
|
]);
|
2021-09-06 00:23:06 +03:00
|
|
|
|
|
|
|
if (! checkNotification) {
|
|
|
|
let relation = R.dispense("monitor_notification");
|
|
|
|
relation.monitor_id = monitors[i].id;
|
|
|
|
relation.notification_id = notificationID;
|
2022-04-20 21:56:40 +03:00
|
|
|
await R.store(relation);
|
2021-09-06 00:23:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 09:14:03 +03:00
|
|
|
module.exports = {
|
|
|
|
Notification,
|
2022-04-20 21:56:40 +03:00
|
|
|
};
|