Liu Song’s Projects


~/Projects/uptime

git clone https://code.lsong.org/uptime

Commit

Commit
e18d4b6ad094e85ca3db5b1a17865f38d3ff1f95
Author
Louis Lam <[email protected]>
Date
2021-12-30 00:16:34 +0800 +0800
Diffstat
 server/notification-providers/google-chat.js | 47 ++++++++++++++++++++++
 server/notification.js | 2 
 src/components/notifications/GoogleChat.vue | 13 ++++++
 src/components/notifications/index.js | 9 ++-

Merge pull request #1045 from bilipp/google-chat-notifications

Add support for Google Chat Notifications


diff --git a/server/notification-providers/google-chat.js b/server/notification-providers/google-chat.js
new file mode 100644
index 0000000000000000000000000000000000000000..1610553c75c9c762bf94be58ccf99f2bf67fceeb
--- /dev/null
+++ b/server/notification-providers/google-chat.js
@@ -0,0 +1,47 @@
+const NotificationProvider = require("./notification-provider");
+const axios = require("axios");
+const { setting } = require("../util-server");
+const { getMonitorRelativeURL } = require("../../src/util");
+const { DOWN, UP } = require("../../src/util");
+
+class GoogleChat extends NotificationProvider {
+
+    name = "Google Chat";
+
+    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
+        let okMsg = "Sent Successfully.";
+        try {
+            // Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic
+
+            let textMsg = ''
+            if (heartbeatJSON && heartbeatJSON.status === UP) {
+                textMsg = `✅ Application is back online\n`;
+            } else if (heartbeatJSON && heartbeatJSON.status === DOWN) {
+                textMsg = `🔴 Application went down\n`;
+            }
+
+            if (monitorJSON && monitorJSON.name) {
+                textMsg += `*${monitorJSON.name}*\n`;
+            }
+
+            textMsg += `${msg}`;
+
+            const baseURL = await setting("primaryBaseURL");
+            if (baseURL) {
+                textMsg += `\n${baseURL + getMonitorRelativeURL(monitorJSON.id)}`;
+            }
+
+            const data = {
+                "text": textMsg,
+            };
+
+            await axios.post(notification.googleChatWebhookURL, data);
+            return okMsg;
+        } catch (error) {
+            this.throwGeneralAxiosError(error);
+        }
+
+    }
+}
+
+module.exports = GoogleChat;




diff --git a/server/notification.js b/server/notification.js
index 3eb5f97bfdbc4474056d6463cf2c4ddfa12d7e80..56a7e84d881190a46e6f158af2defb0950166cd9 100644
--- a/server/notification.js
+++ b/server/notification.js
@@ -25,6 +25,7 @@ const DingDing = require("./notification-providers/dingding");
 const Bark = require("./notification-providers/bark");
 const SerwerSMS = require("./notification-providers/serwersms");
 const Stackfield = require("./notification-providers/stackfield");
+const GoogleChat = require("./notification-providers/google-chat");
 
 class Notification {
 
@@ -62,6 +63,7 @@             new Webhook(),
             new Bark(),
             new SerwerSMS(),
             new Stackfield(),
+            new GoogleChat()
         ];
 
         for (let item of list) {




diff --git a/src/components/notifications/GoogleChat.vue b/src/components/notifications/GoogleChat.vue
new file mode 100644
index 0000000000000000000000000000000000000000..c19cae0de2acb535f44bfcb8783d9da673867fb7
--- /dev/null
+++ b/src/components/notifications/GoogleChat.vue
@@ -0,0 +1,13 @@
+<template>
+    <div class="mb-3">
+        <label for="google-chat-webhook-url" class="form-label">{{ $t("Webhook URL") }}<span style="color: red;"><sup>*</sup></span></label>
+        <input id="google-chat-webhook-url" v-model="$parent.notification.googleChatWebhookURL" type="text" class="form-control" required>
+
+        <div class="form-text">
+            <span style="color: red;"><sup>*</sup></span>{{ $t("Required") }}
+            <i18n-t tag="p" keypath="aboutWebhooks" style="margin-top: 8px;">
+                <a href="https://developers.google.com/chat/how-tos/webhooks" target="_blank">https://developers.google.com/chat/how-tos/webhooks</a>
+            </i18n-t>
+        </div>
+    </div>
+</template>




diff --git a/src/components/notifications/index.js b/src/components/notifications/index.js
index 155a1ab265c3ffadc4596b151b1d2a4229eb6928..9d870f91c29fb980fc4620bbcf090ec46a59d827 100644
--- a/src/components/notifications/index.js
+++ b/src/components/notifications/index.js
@@ -1,4 +1,4 @@
-import STMP from "./SMTP.vue"
+import STMP from "./SMTP.vue";
 import Telegram from "./Telegram.vue";
 import Discord from "./Discord.vue";
 import Webhook from "./Webhook.vue";
@@ -23,8 +23,9 @@ import AliyunSMS from "./AliyunSms.vue";
 import DingDing from "./DingDing.vue";
 import Bark from "./Bark.vue";
 import SerwerSMS from "./SerwerSMS.vue";
-import Telegram from "./Telegram.vue";
+import Stackfield from "./Stackfield.vue";
 import Gotify from "./Gotify.vue";
+import Webhook from "./Webhook.vue";
 
 /**
  * Manage all notification form.
@@ -58,6 +60,8 @@     "Bark": Bark,
     "serwersms": SerwerSMS,
     "stackfield": Stackfield,
 import Gotify from "./Gotify.vue";
+import Signal from "./Signal.vue";
+};
 
 import Gotify from "./Gotify.vue";
-import STMP from "./SMTP.vue"
+import Slack from "./Slack.vue";