Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google chat cards #3928

Merged
merged 7 commits into from
Mar 20, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions server/notification-providers/google-chat.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
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");
const { getMonitorRelativeURL, UP } = require("../../src/util");

class GoogleChat extends NotificationProvider {

Expand All @@ -16,26 +15,74 @@ class GoogleChat extends NotificationProvider {
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";
}
let chatHeader = {
title: "Uptime Kuma Alert",
};

if (monitorJSON && monitorJSON.name) {
textMsg += `*${monitorJSON.name}*\n`;
if (monitorJSON && heartbeatJSON) {
chatHeader["title"] =
heartbeatJSON["status"] === UP
? `✅ ${monitorJSON["name"]} is back online`
: `🔴 ${monitorJSON["name"]} went down`;
}

textMsg += `${msg}`;
// always show msg
let sectionWidgets = [
{
textParagraph: {
text: `<b>Message:</b>\n${msg}`,
},
},
];

// add time if available
if (heartbeatJSON) {
sectionWidgets.push({
textParagraph: {
text: `<b>Time (UTC):</b>\n${heartbeatJSON["localDateTime"]}`,
elliotmatson marked this conversation as resolved.
Show resolved Hide resolved
},
});
}

// add button for monitor link if available
const baseURL = await setting("primaryBaseURL");
if (baseURL && monitorJSON) {
textMsg += `\n${baseURL + getMonitorRelativeURL(monitorJSON.id)}`;
if (baseURL) {
sectionWidgets.push({
buttonList: {
buttons: [
{
text: "Visit Uptime Kuma",
onClick: {
openLink: {
url:
baseURL + (monitorJSON ? getMonitorRelativeURL(
monitorJSON.id
) : "/"),
elliotmatson marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
],
},
});
}

const data = {
"text": textMsg,
// construct first section
elliotmatson marked this conversation as resolved.
Show resolved Hide resolved
let chatSections = [
{
widgets: sectionWidgets,
},
];

// construct json data
let data = {
cardsV2: [
{
card: {
header: chatHeader,
sections: chatSections,
},
},
],
};

await axios.post(notification.googleChatWebhookURL, data);
Expand Down
Loading