const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { getMonitorRelativeURL, UP } = require("../../src/util"); const { Settings } = require("../settings"); 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: `Message:\n${msg}`, }, }, ]; // add time if available if (heartbeatJSON) { sectionWidgets.push({ textParagraph: { text: `Time (${heartbeatJSON["timezone"]}):\n${heartbeatJSON["localDateTime"]}`, }, }); } // add button for monitor link if available const baseURL = await Settings.get("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;