From 4656ab3d5784139ed90ea04ec53ff38989baa54d Mon Sep 17 00:00:00 2001 From: ColdThunder11 Date: Fri, 28 Jan 2022 15:02:28 +0800 Subject: [PATCH] Add OneBot notification service --- server/notification-providers/onebot.js | 45 +++++++++++++++++++++++++ server/notification.js | 3 ++ src/components/notifications/OneBot.vue | 34 +++++++++++++++++++ src/components/notifications/index.js | 2 ++ src/languages/en.js | 6 ++++ src/languages/zh-CN.js | 6 ++++ 6 files changed, 96 insertions(+) create mode 100644 server/notification-providers/onebot.js create mode 100644 src/components/notifications/OneBot.vue diff --git a/server/notification-providers/onebot.js b/server/notification-providers/onebot.js new file mode 100644 index 00000000..0359e83a --- /dev/null +++ b/server/notification-providers/onebot.js @@ -0,0 +1,45 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); + +class OneBot extends NotificationProvider { + + name = "OneBot"; + + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + let okMsg = "Sent Successfully."; + try { + let httpAddr = notification.httpAddr; + if (!httpAddr.startsWith("http")) { + httpAddr = "http://" + httpAddr; + } + if (!httpAddr.endsWith("/")) { + httpAddr += "/"; + } + let onebotAPIUrl = httpAddr + "send_msg"; + let config = { + headers: { + "Content-Type": "application/json", + "Authorization": "Bearer " + notification.accessToken + } + }; + let psuhText = "UptimeKuma Alert: " + msg; + let data = { + "auto_escape": true, + "message": psuhText + }; + if (notification.msgType == "group") { + data["message_type"] = "group"; + data["group_id"] = notification.recieverId; + } else { + data["message_type"] = "private"; + data["user_id"] = notification.recieverId; + } + await axios.post(onebotAPIUrl, data, config); + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + } +} + +module.exports = OneBot; diff --git a/server/notification.js b/server/notification.js index 359e4c9e..88cfb890 100644 --- a/server/notification.js +++ b/server/notification.js @@ -31,6 +31,7 @@ const WeCom = require("./notification-providers/wecom"); 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"); class Notification { @@ -73,6 +74,8 @@ class Notification { new GoogleChat(), new Gorush(), new Alerta(), + new GoogleChat(), + new Onebot(), ]; for (let item of list) { diff --git a/src/components/notifications/OneBot.vue b/src/components/notifications/OneBot.vue new file mode 100644 index 00000000..63604d60 --- /dev/null +++ b/src/components/notifications/OneBot.vue @@ -0,0 +1,34 @@ + diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index 455b8262..2fa36c0f 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -29,6 +29,7 @@ import WeCom from "./WeCom.vue"; import GoogleChat from "./GoogleChat.vue"; import Gorush from "./Gorush.vue"; import Alerta from "./Alerta.vue"; +import OneBot from "./OneBot.vue"; /** * Manage all notification form. @@ -67,6 +68,7 @@ const NotificationFormList = { "GoogleChat": GoogleChat, "gorush": Gorush, "alerta": Alerta, + "OneBot": OneBot, }; export default NotificationFormList; diff --git a/src/languages/en.js b/src/languages/en.js index ac432ba9..2dcec68c 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -445,4 +445,10 @@ export default { "Domain Name Expiry Notification": "Domain Name Expiry Notification", "Proxy": "Proxy", "Date Created": "Date Created", + onebotHttpAddress: "OneBot HTTP Address", + onebotMessageType: "OneBot Message Type", + onebotGroupMessage: "Group", + onebotPrivateMessage: "Private", + onebotUserOrGroupId: "Group/User ID", + onebotSafetyTips: "For safety, must set access token", }; diff --git a/src/languages/zh-CN.js b/src/languages/zh-CN.js index ba1132a3..2f2d9f2b 100644 --- a/src/languages/zh-CN.js +++ b/src/languages/zh-CN.js @@ -452,4 +452,10 @@ export default { "Domain Name Expiry Notification": "域名到期时通知", "Proxy": "代理", "Date Created": "创建于", + onebotHttpAddress: "OneBot HTTP 地址", + onebotMessageType: "OneBot 消息类型", + onebotGroupMessage: "群聊", + onebotPrivateMessage: "私聊", + onebotUserOrGroupId: "群组/用户ID", + onebotSafetyTips: "出于安全原因,请务必设置AccessToken", };