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