2021-07-27 18:47:13 +01:00
|
|
|
const { R } = require("redbean-node");
|
2021-09-07 15:42:46 +01: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 19:03:56 +01:00
|
|
|
const Matrix = require("./notification-providers/matrix");
|
2021-09-07 15:42:46 +01:00
|
|
|
const Octopush = require("./notification-providers/octopush");
|
2021-10-07 20:56:32 +01:00
|
|
|
const PromoSMS = require("./notification-providers/promosms");
|
2021-10-24 20:17:29 +01:00
|
|
|
const ClickSendSMS = require("./notification-providers/clicksendsms");
|
2021-09-07 15:42:46 +01:00
|
|
|
const Pushbullet = require("./notification-providers/pushbullet");
|
|
|
|
const Pushover = require("./notification-providers/pushover");
|
|
|
|
const Pushy = require("./notification-providers/pushy");
|
2022-01-21 07:42:03 +00:00
|
|
|
const TechulusPush = require("./notification-providers/techulus-push");
|
2021-09-07 15:42:46 +01: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");
|
2021-09-12 18:46:59 +01:00
|
|
|
const Teams = require("./notification-providers/teams");
|
2021-09-07 15:42:46 +01:00
|
|
|
const Telegram = require("./notification-providers/telegram");
|
|
|
|
const Webhook = require("./notification-providers/webhook");
|
2021-10-11 10:20:09 +01:00
|
|
|
const Feishu = require("./notification-providers/feishu");
|
2021-10-13 04:55:01 +01:00
|
|
|
const AliyunSms = require("./notification-providers/aliyun-sms");
|
2021-10-13 09:13:46 +01:00
|
|
|
const DingDing = require("./notification-providers/dingding");
|
2021-10-23 17:20:24 +01:00
|
|
|
const Bark = require("./notification-providers/bark");
|
2021-11-25 17:24:36 +00:00
|
|
|
const SerwerSMS = require("./notification-providers/serwersms");
|
2021-12-02 07:53:45 +00:00
|
|
|
const Stackfield = require("./notification-providers/stackfield");
|
2021-12-25 17:33:47 +00:00
|
|
|
const WeCom = require("./notification-providers/wecom");
|
2021-12-11 22:43:12 +00:00
|
|
|
const GoogleChat = require("./notification-providers/google-chat");
|
2022-01-09 17:05:11 +00:00
|
|
|
const Gorush = require("./notification-providers/gorush");
|
2022-01-26 14:54:17 +00:00
|
|
|
const Alerta = require("./notification-providers/alerta");
|
2021-09-04 04:09:34 +01:00
|
|
|
|
2021-07-09 07:14:03 +01:00
|
|
|
class Notification {
|
2021-07-18 11:51:58 +01:00
|
|
|
|
2021-09-07 15:42:46 +01:00
|
|
|
providerList = {};
|
|
|
|
|
|
|
|
static init() {
|
|
|
|
console.log("Prepare Notification Providers");
|
|
|
|
|
|
|
|
this.providerList = {};
|
|
|
|
|
|
|
|
const list = [
|
|
|
|
new Apprise(),
|
2021-10-13 04:55:01 +01:00
|
|
|
new AliyunSms(),
|
2021-10-13 09:13:46 +01:00
|
|
|
new DingDing(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new Discord(),
|
2021-09-12 18:46:59 +01:00
|
|
|
new Teams(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new Gotify(),
|
|
|
|
new Line(),
|
|
|
|
new LunaSea(),
|
2021-10-11 10:20:09 +01:00
|
|
|
new Feishu(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new Mattermost(),
|
2021-10-05 19:03:56 +01:00
|
|
|
new Matrix(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new Octopush(),
|
2021-10-07 20:56:32 +01:00
|
|
|
new PromoSMS(),
|
2021-10-24 20:17:29 +01:00
|
|
|
new ClickSendSMS(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new Pushbullet(),
|
|
|
|
new Pushover(),
|
|
|
|
new Pushy(),
|
2022-01-21 07:42:03 +00:00
|
|
|
new TechulusPush(),
|
2021-09-07 15:42:46 +01:00
|
|
|
new RocketChat(),
|
|
|
|
new Signal(),
|
|
|
|
new Slack(),
|
|
|
|
new SMTP(),
|
|
|
|
new Telegram(),
|
|
|
|
new Webhook(),
|
2021-10-23 17:20:24 +01:00
|
|
|
new Bark(),
|
2021-11-25 17:24:36 +00:00
|
|
|
new SerwerSMS(),
|
2021-12-02 07:53:45 +00:00
|
|
|
new Stackfield(),
|
2021-12-25 17:33:47 +00:00
|
|
|
new WeCom(),
|
2022-01-26 14:54:17 +00:00
|
|
|
new GoogleChat(),
|
2022-03-04 06:10:37 +00:00
|
|
|
new Gorush(),
|
2022-01-26 14:54:17 +00:00
|
|
|
new Alerta(),
|
2021-09-07 15:42:46 +01: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 13:49:46 +01:00
|
|
|
/**
|
|
|
|
*
|
2021-09-07 15:42:46 +01:00
|
|
|
* @param notification : BeanModel
|
|
|
|
* @param msg : string General Message
|
|
|
|
* @param monitorJSON : object Monitor details (For Up/Down only)
|
|
|
|
* @param heartbeatJSON : object Heartbeat details (For Up/Down only)
|
2021-07-18 13:49:46 +01:00
|
|
|
* @returns {Promise<string>} Successful msg
|
|
|
|
* Throw Error with fail msg
|
|
|
|
*/
|
2021-07-09 12:33:22 +01:00
|
|
|
static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-09-07 15:42:46 +01:00
|
|
|
if (this.providerList[notification.type]) {
|
|
|
|
return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON);
|
2021-07-09 07:14:03 +01:00
|
|
|
} else {
|
2021-09-07 15:42:46 +01:00
|
|
|
throw new Error("Notification type is not supported");
|
2021-07-09 07:14:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-09-05 22:23:06 +01:00
|
|
|
bean.config = JSON.stringify(notification);
|
2021-09-09 13:22:32 +01:00
|
|
|
bean.is_default = notification.isDefault || false;
|
2021-07-09 07:14:03 +01:00
|
|
|
await R.store(bean)
|
2021-09-05 22:23:06 +01:00
|
|
|
|
|
|
|
if (notification.applyExisting) {
|
|
|
|
await applyNotificationEveryMonitor(bean.id, userID);
|
|
|
|
}
|
2021-09-09 14:24:29 +01:00
|
|
|
|
|
|
|
return bean;
|
2021-07-09 07:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2021-07-09 18:08:08 +01:00
|
|
|
|
2021-07-18 11:51:58 +01:00
|
|
|
static checkApprise() {
|
2021-07-27 18:47:13 +01:00
|
|
|
let commandExistsSync = require("command-exists").sync;
|
|
|
|
let exists = commandExistsSync("apprise");
|
2021-07-18 11:51:58 +01:00
|
|
|
return exists;
|
2021-07-10 04:38:00 +01:00
|
|
|
}
|
|
|
|
|
2021-07-18 13:49:46 +01:00
|
|
|
}
|
2021-07-10 04:38:00 +01:00
|
|
|
|
2021-09-05 22:23:06 +01: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,
|
|
|
|
])
|
|
|
|
|
|
|
|
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 07:14:03 +01:00
|
|
|
module.exports = {
|
|
|
|
Notification,
|
|
|
|
}
|