From fb059f5e91bbe8ef97cc9107b3af6996b82e4b0d Mon Sep 17 00:00:00 2001 From: ngc7331 Date: Sat, 2 Apr 2022 14:40:33 +0800 Subject: [PATCH] Add support for Pushdeer notifications --- server/notification-providers/pushdeer.js | 52 +++++++++++++++++++++++ server/notification.js | 2 + src/components/notifications/Pushdeer.vue | 19 +++++++++ src/components/notifications/index.js | 2 + 4 files changed, 75 insertions(+) create mode 100644 server/notification-providers/pushdeer.js create mode 100644 src/components/notifications/Pushdeer.vue diff --git a/server/notification-providers/pushdeer.js b/server/notification-providers/pushdeer.js new file mode 100644 index 000000000..f86577693 --- /dev/null +++ b/server/notification-providers/pushdeer.js @@ -0,0 +1,52 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { DOWN, UP } = require("../../src/util"); + +class Pushdeer extends NotificationProvider { + + name = "Pushdeer"; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully."; + let pushdeerlink = "https://api2.pushdeer.com/message/push"; + + let valid = msg != null && monitorJSON != null && heartbeatJSON != null; + + let title; + if (valid && heartbeatJSON.status == UP) { + title = "## UptimeKuma: " + monitorJSON.name + " up"; + } else if (valid && heartbeatJSON.status == DOWN) { + title = "## UptimeKuma: " + monitorJSON.name + " down"; + } else { + title = "## UptimeKuma Message"; + } + + let data = { + "pushkey": notification.pushdeerKey, + "text": title, + "desp": msg.replace(/\n/g, "\n\n"), + "type": "markdown", + }; + + try { + let res = await axios.post(pushdeerlink, data); + + if ("error" in res.data) { + let error = res.data.error; + this.throwGeneralAxiosError(error); + } + if (res.data.content.result.length === 0) { + let error = "Invalid Pushdeer key"; + this.throwGeneralAxiosError(error); + } else if (JSON.parse(res.data.content.result[0]).success != "ok") { + let error = "Unknown error"; + this.throwGeneralAxiosError(error); + } + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } +} + +module.exports = Pushdeer; diff --git a/server/notification.js b/server/notification.js index a83a8cdce..e3d37bafb 100644 --- a/server/notification.js +++ b/server/notification.js @@ -32,6 +32,7 @@ const GoogleChat = require("./notification-providers/google-chat"); const Gorush = require("./notification-providers/gorush"); const Alerta = require("./notification-providers/alerta"); const OneBot = require("./notification-providers/onebot"); +const Pushdeer = require("./notification-providers/pushdeer"); class Notification { @@ -75,6 +76,7 @@ class Notification { new Gorush(), new Alerta(), new OneBot(), + new Pushdeer(), ]; for (let item of list) { diff --git a/src/components/notifications/Pushdeer.vue b/src/components/notifications/Pushdeer.vue new file mode 100644 index 000000000..762715504 --- /dev/null +++ b/src/components/notifications/Pushdeer.vue @@ -0,0 +1,19 @@ + + + \ No newline at end of file diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 2fa36c0f4..2a7d77933 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -30,6 +30,7 @@ import GoogleChat from "./GoogleChat.vue"; import Gorush from "./Gorush.vue"; import Alerta from "./Alerta.vue"; import OneBot from "./OneBot.vue"; +import Pushdeer from "./Pushdeer.vue"; /** * Manage all notification form. @@ -69,6 +70,7 @@ const NotificationFormList = { "gorush": Gorush, "alerta": Alerta, "OneBot": OneBot, + "Pushdeer": Pushdeer, }; export default NotificationFormList;