From dab089086c6bed06d41dba01d203b7e65e3c6339 Mon Sep 17 00:00:00 2001 From: "Pargorn.Ru" Date: Fri, 17 Jan 2025 17:05:18 +0700 Subject: [PATCH 1/5] Add OneChat notification provider --- package-lock.json | 4 +- server/notification-providers/onechat.js | 70 ++++++++++++++++++++++++ server/notification.js | 2 + src/components/NotificationDialog.vue | 1 + src/components/notifications/OneChat.vue | 48 ++++++++++++++++ src/components/notifications/index.js | 2 + 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 server/notification-providers/onechat.js create mode 100644 src/components/notifications/OneChat.vue diff --git a/package-lock.json b/package-lock.json index 8d3f58b4d..ccf1a628c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "uptime-kuma", - "version": "2.0.0-beta.0", + "version": "2.0.0-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "uptime-kuma", - "version": "2.0.0-beta.0", + "version": "2.0.0-beta.1", "license": "MIT", "dependencies": { "@grpc/grpc-js": "~1.8.22", diff --git a/server/notification-providers/onechat.js b/server/notification-providers/onechat.js new file mode 100644 index 000000000..86d926085 --- /dev/null +++ b/server/notification-providers/onechat.js @@ -0,0 +1,70 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { DOWN, UP } = require("../../src/util"); + +class OneChat extends NotificationProvider { + name = "OneChat"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + const okMsg = "Sent Successfully."; + const url = "https://chat-api.one.th/message/api/v1/push_message"; + + try { + const config = { + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + notification.accessToken, + }, + }; + // Send a test message if the monitor is null + if (heartbeatJSON == null) { + const testMessage = { + to: notification.recieverId, + bot_id: notification.botId, + type: "text", + message: "Test Successful!", + }; + await axios.post(url, testMessage, config); + } else if (heartbeatJSON["status"] === DOWN) { + const downMessage = { + to: notification.recieverId, + bot_id: notification.botId, + type: "text", + message: + `UptimeKuma Alert:\n[🔴 Down]\n` + + `Name: ${monitorJSON["name"]}\n` + + `${heartbeatJSON["msg"]}\n` + + `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`, + }; + await axios.post(url, downMessage, config); + } else if (heartbeatJSON["status"] === UP) { + const upMessage = { + to: notification.recieverId, + bot_id: notification.botId, + type: "text", + message: + `UptimeKuma Alert:\n[✅ Up]\n` + + `Name: ${monitorJSON["name"]}\n` + + `${heartbeatJSON["msg"]}\n` + + `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`, + }; + await axios.post(url, upMessage, config); + } + + return okMsg; + } catch (error) { + // Handle errors and throw a descriptive message + if (error.response) { + const errorMessage = error.response.data?.message || "Unknown API error occurred."; + throw new Error(`OneChat API Error: ${errorMessage}`); + } else { + throw new Error(`Network or unexpected error: ${error.message}`); + } + } + } +} + +module.exports = OneChat; \ No newline at end of file diff --git a/server/notification.js b/server/notification.js index e7977eb4a..aed083470 100644 --- a/server/notification.js +++ b/server/notification.js @@ -30,6 +30,7 @@ const Mattermost = require("./notification-providers/mattermost"); const Nostr = require("./notification-providers/nostr"); const Ntfy = require("./notification-providers/ntfy"); const Octopush = require("./notification-providers/octopush"); +const OneChat = require("./notification-providers/onechat"); const OneBot = require("./notification-providers/onebot"); const Opsgenie = require("./notification-providers/opsgenie"); const PagerDuty = require("./notification-providers/pagerduty"); @@ -116,6 +117,7 @@ class Notification { new Nostr(), new Ntfy(), new Octopush(), + new OneChat(), new OneBot(), new Onesender(), new Opsgenie(), diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index f6d728029..6a206699f 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -135,6 +135,7 @@ export default { "nostr": "Nostr", "ntfy": "Ntfy", "octopush": "Octopush", + "OneChat": "OneChat", "OneBot": "OneBot", "Onesender": "Onesender", "Opsgenie": "Opsgenie", diff --git a/src/components/notifications/OneChat.vue b/src/components/notifications/OneChat.vue new file mode 100644 index 000000000..93457f6a9 --- /dev/null +++ b/src/components/notifications/OneChat.vue @@ -0,0 +1,48 @@ + \ No newline at end of file diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js index efa2af5c4..5330fa9ff 100644 --- a/src/components/notifications/index.js +++ b/src/components/notifications/index.js @@ -29,6 +29,7 @@ import Mattermost from "./Mattermost.vue"; import Nostr from "./Nostr.vue"; import Ntfy from "./Ntfy.vue"; import Octopush from "./Octopush.vue"; +import OneChat from "./OneChat.vue"; import OneBot from "./OneBot.vue"; import Onesender from "./Onesender.vue"; import Opsgenie from "./Opsgenie.vue"; @@ -103,6 +104,7 @@ const NotificationFormList = { "nostr": Nostr, "ntfy": Ntfy, "octopush": Octopush, + "OneChat": OneChat, "OneBot": OneBot, "Onesender": Onesender, "Opsgenie": Opsgenie, From ef9b6d2be8a6d9ef71790edb30bbbf8133d7bc92 Mon Sep 17 00:00:00 2001 From: "Pargorn.Ru" Date: Fri, 17 Jan 2025 19:47:10 +0700 Subject: [PATCH 2/5] fix ESLint issues in OneChat.js --- server/notification-providers/onechat.js | 40 +++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/server/notification-providers/onechat.js b/server/notification-providers/onechat.js index 86d926085..e2f170049 100644 --- a/server/notification-providers/onechat.js +++ b/server/notification-providers/onechat.js @@ -34,10 +34,17 @@ class OneChat extends NotificationProvider { bot_id: notification.botId, type: "text", message: - `UptimeKuma Alert:\n[🔴 Down]\n` + - `Name: ${monitorJSON["name"]}\n` + - `${heartbeatJSON["msg"]}\n` + - `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`, + "UptimeKuma Alert:\n" + + "[🔴 Down]\n" + + "Name: " + + monitorJSON["name"] + + "\n" + + heartbeatJSON["msg"] + + "\n" + + "Time (" + + heartbeatJSON["timezone"] + + "): " + + heartbeatJSON["localDateTime"], }; await axios.post(url, downMessage, config); } else if (heartbeatJSON["status"] === UP) { @@ -46,10 +53,17 @@ class OneChat extends NotificationProvider { bot_id: notification.botId, type: "text", message: - `UptimeKuma Alert:\n[✅ Up]\n` + - `Name: ${monitorJSON["name"]}\n` + - `${heartbeatJSON["msg"]}\n` + - `Time (${heartbeatJSON["timezone"]}): ${heartbeatJSON["localDateTime"]}`, + "UptimeKuma Alert:\n" + + "[🟢 Up]\n" + + "Name: " + + monitorJSON["name"] + + "\n" + + heartbeatJSON["msg"] + + "\n" + + "Time (" + + heartbeatJSON["timezone"] + + "): " + + heartbeatJSON["localDateTime"], }; await axios.post(url, upMessage, config); } @@ -58,13 +72,17 @@ class OneChat extends NotificationProvider { } catch (error) { // Handle errors and throw a descriptive message if (error.response) { - const errorMessage = error.response.data?.message || "Unknown API error occurred."; + const errorMessage = + error.response.data?.message || + "Unknown API error occurred."; throw new Error(`OneChat API Error: ${errorMessage}`); } else { - throw new Error(`Network or unexpected error: ${error.message}`); + throw new Error( + `Network or unexpected error: ${error.message}` + ); } } } } -module.exports = OneChat; \ No newline at end of file +module.exports = OneChat; From b3d149b70121626aa288982d3cf35a757fff41cf Mon Sep 17 00:00:00 2001 From: "Pargorn.Ru" Date: Fri, 17 Jan 2025 19:47:37 +0700 Subject: [PATCH 3/5] add English translations for OneChat Notification Provider --- src/lang/en.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lang/en.json b/src/lang/en.json index e215f1031..991b122e5 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1051,5 +1051,8 @@ "RabbitMQ Password": "RabbitMQ Password", "rabbitmqHelpText": "To use the monitor, you will need to enable the Management Plugin in your RabbitMQ setup. For more information, please consult the {rabitmq_documentation}.", "SendGrid API Key": "SendGrid API Key", - "Separate multiple email addresses with commas": "Separate multiple email addresses with commas" + "Separate multiple email addresses with commas": "Separate multiple email addresses with commas", + "OneChatAccessToken": "OneChat Access Token", + "OneChatUserIdOrGroupId": "OneChat User ID or Group ID", + "OneChatBotId": "OneChat Bot ID" } From b3e5d5ee8d95e0a93dd62d5a956a2b3c65398cee Mon Sep 17 00:00:00 2001 From: "Pargorn.Ru" Date: Fri, 17 Jan 2025 19:58:02 +0700 Subject: [PATCH 4/5] fix missing newline at end of OneChat.vue --- src/components/notifications/OneChat.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/notifications/OneChat.vue b/src/components/notifications/OneChat.vue index 93457f6a9..e503fb1d8 100644 --- a/src/components/notifications/OneChat.vue +++ b/src/components/notifications/OneChat.vue @@ -45,4 +45,4 @@ /> - \ No newline at end of file + From f0eaa46a022d7e66fdbf0c56d95455c44ec49b31 Mon Sep 17 00:00:00 2001 From: "Pargorn.Ru" Date: Fri, 17 Jan 2025 20:11:09 +0700 Subject: [PATCH 5/5] add link to OneChat documentation in OneChat.vue --- src/components/notifications/OneChat.vue | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/components/notifications/OneChat.vue b/src/components/notifications/OneChat.vue index e503fb1d8..c0e08d472 100644 --- a/src/components/notifications/OneChat.vue +++ b/src/components/notifications/OneChat.vue @@ -44,5 +44,12 @@ required /> + + +