diff options
author | Daniel Baumann <daniel@debian.org> | 2024-11-26 09:28:28 +0100 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-11-26 12:25:58 +0100 |
commit | a1882b67c41fe9901a0cd8059b5cc78a5beadec0 (patch) | |
tree | 2a24507c67aa99a15416707b2f7e645142230ed8 /server/notification-providers/google-chat.js | |
parent | Initial commit. (diff) | |
download | uptime-kuma-upstream.tar.xz uptime-kuma-upstream.zip |
Adding upstream version 2.0.0~beta.0+dfsg.upstream/2.0.0_beta.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'server/notification-providers/google-chat.js')
-rw-r--r-- | server/notification-providers/google-chat.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/server/notification-providers/google-chat.js b/server/notification-providers/google-chat.js new file mode 100644 index 0000000..0b72fea --- /dev/null +++ b/server/notification-providers/google-chat.js @@ -0,0 +1,94 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { setting } = require("../util-server"); +const { getMonitorRelativeURL, UP } = require("../../src/util"); + +class GoogleChat extends NotificationProvider { + name = "GoogleChat"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + const okMsg = "Sent Successfully."; + + try { + // Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic + + let chatHeader = { + title: "Uptime Kuma Alert", + }; + + if (monitorJSON && heartbeatJSON) { + chatHeader["title"] = + heartbeatJSON["status"] === UP + ? `✅ ${monitorJSON["name"]} is back online` + : `🔴 ${monitorJSON["name"]} went down`; + } + + // always show msg + let sectionWidgets = [ + { + textParagraph: { + text: `<b>Message:</b>\n${msg}`, + }, + }, + ]; + + // add time if available + if (heartbeatJSON) { + sectionWidgets.push({ + textParagraph: { + text: `<b>Time (${heartbeatJSON["timezone"]}):</b>\n${heartbeatJSON["localDateTime"]}`, + }, + }); + } + + // add button for monitor link if available + const baseURL = await setting("primaryBaseURL"); + if (baseURL) { + const urlPath = monitorJSON ? getMonitorRelativeURL(monitorJSON.id) : "/"; + sectionWidgets.push({ + buttonList: { + buttons: [ + { + text: "Visit Uptime Kuma", + onClick: { + openLink: { + url: baseURL + urlPath, + }, + }, + }, + ], + }, + }); + } + + let chatSections = [ + { + widgets: sectionWidgets, + }, + ]; + + // construct json data + let data = { + cardsV2: [ + { + card: { + header: chatHeader, + sections: chatSections, + }, + }, + ], + }; + + await axios.post(notification.googleChatWebhookURL, data); + return okMsg; + } catch (error) { + this.throwGeneralAxiosError(error); + } + + } +} + +module.exports = GoogleChat; |