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