Google chat cards (#3928)

Co-authored-by: Frank Elsinga <frank@elsinga.de>
This commit is contained in:
Elliot Matson 2024-03-20 04:58:28 -07:00 committed by GitHub
parent bc077cc536
commit 5bc68fe0a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,8 +1,7 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const axios = require("axios"); const axios = require("axios");
const { setting } = require("../util-server"); const { setting } = require("../util-server");
const { getMonitorRelativeURL } = require("../../src/util"); const { getMonitorRelativeURL, UP } = require("../../src/util");
const { DOWN, UP } = require("../../src/util");
class GoogleChat extends NotificationProvider { class GoogleChat extends NotificationProvider {
name = "GoogleChat"; name = "GoogleChat";
@ -16,26 +15,71 @@ class GoogleChat extends NotificationProvider {
try { try {
// Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic // Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic
let textMsg = ""; let chatHeader = {
if (heartbeatJSON && heartbeatJSON.status === UP) { title: "Uptime Kuma Alert",
textMsg = "✅ Application is back online\n"; };
} else if (heartbeatJSON && heartbeatJSON.status === DOWN) {
textMsg = "🔴 Application went down\n"; if (monitorJSON && heartbeatJSON) {
chatHeader["title"] =
heartbeatJSON["status"] === UP
? `${monitorJSON["name"]} is back online`
: `🔴 ${monitorJSON["name"]} went down`;
} }
if (monitorJSON && monitorJSON.name) { // always show msg
textMsg += `*${monitorJSON.name}*\n`; 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"]}`,
},
});
} }
textMsg += `${msg}`; // add button for monitor link if available
const baseURL = await setting("primaryBaseURL"); const baseURL = await setting("primaryBaseURL");
if (baseURL && monitorJSON) { if (baseURL) {
textMsg += `\n${baseURL + getMonitorRelativeURL(monitorJSON.id)}`; const urlPath = monitorJSON ? getMonitorRelativeURL(monitorJSON.id) : "/";
sectionWidgets.push({
buttonList: {
buttons: [
{
text: "Visit Uptime Kuma",
onClick: {
openLink: {
url: baseURL + urlPath,
},
},
},
],
},
});
} }
const data = { let chatSections = [
"text": textMsg, {
widgets: sectionWidgets,
},
];
// construct json data
let data = {
cardsV2: [
{
card: {
header: chatHeader,
sections: chatSections,
},
},
],
}; };
await axios.post(notification.googleChatWebhookURL, data); await axios.post(notification.googleChatWebhookURL, data);