From b059c190695b491cd4f10b3c591cbf05887fac85 Mon Sep 17 00:00:00 2001 From: Alone88 Date: Mon, 26 Aug 2024 10:57:56 +0800 Subject: [PATCH] Add WPush Notification Provider (#5049) Signed-off-by: Alone88 --- server/notification-providers/wpush.js | 51 ++++++++++++++++++++++++++ server/notification.js | 2 + src/components/NotificationDialog.vue | 1 + src/components/notifications/WPush.vue | 31 ++++++++++++++++ src/components/notifications/index.js | 2 + 5 files changed, 87 insertions(+) create mode 100644 server/notification-providers/wpush.js create mode 100644 src/components/notifications/WPush.vue diff --git a/server/notification-providers/wpush.js b/server/notification-providers/wpush.js new file mode 100644 index 000000000..db043f9c5 --- /dev/null +++ b/server/notification-providers/wpush.js @@ -0,0 +1,51 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { DOWN, UP } = require("../../src/util"); + +class WPush extends NotificationProvider { + name = "WPush"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + const okMsg = "Sent Successfully."; + + try { + const context = { + "title": this.checkStatus(heartbeatJSON, monitorJSON), + "content": msg, + "apikey": notification.wpushAPIkey, + "channel": notification.wpushChannel + }; + const result = await axios.post("https://api.wpush.cn/api/v1/send", context); + if (result.data.code !== 0) { + throw result.data.message; + } + + return okMsg; + + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + + /** + * Get the formatted title for message + * @param {?object} heartbeatJSON Heartbeat details (For Up/Down only) + * @param {?object} monitorJSON Monitor details (For Up/Down only) + * @returns {string} Formatted title + */ + checkStatus(heartbeatJSON, monitorJSON) { + let title = "UptimeKuma Message"; + if (heartbeatJSON != null && heartbeatJSON["status"] === UP) { + title = "UptimeKuma Monitor Up " + monitorJSON["name"]; + } + if (heartbeatJSON != null && heartbeatJSON["status"] === DOWN) { + title = "UptimeKuma Monitor Down " + monitorJSON["name"]; + } + return title; + } +} + +module.exports = WPush; diff --git a/server/notification.js b/server/notification.js index e6a2e6d5e..8fce5b90c 100644 --- a/server/notification.js +++ b/server/notification.js @@ -65,6 +65,7 @@ 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"); class Notification { @@ -147,6 +148,7 @@ class Notification { new Whapi(), new GtxMessaging(), new Cellsynt(), + new Wpush(), ]; for (let item of list) { if (! item.name) { diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index 288b00559..c7ae19231 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -179,6 +179,7 @@ export default { "WeCom": "WeCom (企业微信群机器人)", "ServerChan": "ServerChan (Server酱)", "smsc": "SMSC", + "WPush": "WPush(wpush.cn)", }; // Sort by notification name diff --git a/src/components/notifications/WPush.vue b/src/components/notifications/WPush.vue new file mode 100644 index 000000000..58499c403 --- /dev/null +++ b/src/components/notifications/WPush.vue @@ -0,0 +1,31 @@ + + + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 51642d7da..5ed5e70b2 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -63,6 +63,7 @@ import Splunk from "./Splunk.vue"; import SevenIO from "./SevenIO.vue"; import Whapi from "./Whapi.vue"; import Cellsynt from "./Cellsynt.vue"; +import WPush from "./WPush.vue"; /** * Manage all notification form. @@ -134,6 +135,7 @@ const NotificationFormList = { "whapi": Whapi, "gtxmessaging": GtxMessaging, "Cellsynt": Cellsynt, + "WPush": WPush }; export default NotificationFormList;