mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-21 11:05:56 +00:00
Co-authored-by: Frank Elsinga <frank@elsinga.de> Closes: #4391 Squashed commit of the following: commit828d2a73d4
Merge:10f771cf
dd758903
Author: Frank Elsinga <frank@elsinga.de> Date: Fri Sep 13 22:51:25 2024 +0800 Merge branch 'master' into deprecations commit10f771cfc6
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 22:36:12 2024 +0100 formatting fixes commitd737b19e2f
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 21:27:30 2024 +0100 migrated all settings to use the `Settings` class commitc5e26e993e
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 21:04:44 2024 +0100 removed the deprecated logging functionality * fix(server/model/monitor): duplicate `Settings.set` for `tlsExpiryNotifyDays` * fix(eslint): minor linter complaints & a typo
94 lines
2.9 KiB
JavaScript
94 lines
2.9 KiB
JavaScript
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: `<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 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;
|