2021-12-11 14:43:12 -08:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { setting } = require("../util-server");
|
2024-03-20 04:58:28 -07:00
|
|
|
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
2021-12-11 14:43:12 -08:00
|
|
|
|
|
|
|
class GoogleChat extends NotificationProvider {
|
2022-01-05 14:57:40 -08:00
|
|
|
name = "GoogleChat";
|
2021-12-11 14:43:12 -08:00
|
|
|
|
2023-08-11 00:46:41 -07:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-12-11 14:43:12 -08:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 06:21:15 -07:00
|
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
|
2021-12-11 14:43:12 -08:00
|
|
|
try {
|
|
|
|
// Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic
|
|
|
|
|
2024-03-20 04:58:28 -07:00
|
|
|
let chatHeader = {
|
|
|
|
title: "Uptime Kuma Alert",
|
|
|
|
};
|
2021-12-11 14:43:12 -08:00
|
|
|
|
2024-03-20 04:58:28 -07:00
|
|
|
if (monitorJSON && heartbeatJSON) {
|
|
|
|
chatHeader["title"] =
|
|
|
|
heartbeatJSON["status"] === UP
|
|
|
|
? `✅ ${monitorJSON["name"]} is back online`
|
|
|
|
: `🔴 ${monitorJSON["name"]} went down`;
|
2021-12-11 14:43:12 -08:00
|
|
|
}
|
|
|
|
|
2024-03-20 04:58:28 -07:00
|
|
|
// always show msg
|
|
|
|
let sectionWidgets = [
|
|
|
|
{
|
|
|
|
textParagraph: {
|
|
|
|
text: `<b>Message:</b>\n${msg}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
2021-12-11 14:43:12 -08:00
|
|
|
|
2024-03-20 04:58:28 -07:00
|
|
|
// 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
|
2021-12-11 14:43:12 -08:00
|
|
|
const baseURL = await setting("primaryBaseURL");
|
2024-03-20 04:58:28 -07:00
|
|
|
if (baseURL) {
|
|
|
|
const urlPath = monitorJSON ? getMonitorRelativeURL(monitorJSON.id) : "/";
|
|
|
|
sectionWidgets.push({
|
|
|
|
buttonList: {
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
text: "Visit Uptime Kuma",
|
|
|
|
onClick: {
|
|
|
|
openLink: {
|
|
|
|
url: baseURL + urlPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
2021-12-11 14:43:12 -08:00
|
|
|
}
|
|
|
|
|
2024-03-20 04:58:28 -07:00
|
|
|
let chatSections = [
|
|
|
|
{
|
|
|
|
widgets: sectionWidgets,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// construct json data
|
|
|
|
let data = {
|
|
|
|
cardsV2: [
|
|
|
|
{
|
|
|
|
card: {
|
|
|
|
header: chatHeader,
|
|
|
|
sections: chatSections,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-12-11 14:43:12 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
await axios.post(notification.googleChatWebhookURL, data);
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GoogleChat;
|