2021-12-11 14:43:12 -08:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { setting } = require("../util-server");
|
|
|
|
const { getMonitorRelativeURL } = require("../../src/util");
|
2021-12-15 04:40:21 -08:00
|
|
|
const { DOWN, 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) {
|
|
|
|
let okMsg = "Sent Successfully.";
|
|
|
|
try {
|
|
|
|
// Google Chat message formatting: https://developers.google.com/chat/api/guides/message-formats/basic
|
|
|
|
|
2022-04-13 09:30:32 -07:00
|
|
|
let textMsg = "";
|
2021-12-11 14:43:12 -08:00
|
|
|
if (heartbeatJSON && heartbeatJSON.status === UP) {
|
2022-04-13 09:30:32 -07:00
|
|
|
textMsg = "✅ Application is back online\n";
|
2021-12-15 04:40:21 -08:00
|
|
|
} else if (heartbeatJSON && heartbeatJSON.status === DOWN) {
|
2022-04-13 09:30:32 -07:00
|
|
|
textMsg = "🔴 Application went down\n";
|
2021-12-11 14:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (monitorJSON && monitorJSON.name) {
|
2021-12-15 04:40:21 -08:00
|
|
|
textMsg += `*${monitorJSON.name}*\n`;
|
2021-12-11 14:43:12 -08:00
|
|
|
}
|
|
|
|
|
2021-12-15 04:40:21 -08:00
|
|
|
textMsg += `${msg}`;
|
2021-12-11 14:43:12 -08:00
|
|
|
|
|
|
|
const baseURL = await setting("primaryBaseURL");
|
2022-01-05 23:48:12 -08:00
|
|
|
if (baseURL && monitorJSON) {
|
2021-12-11 14:43:12 -08:00
|
|
|
textMsg += `\n${baseURL + getMonitorRelativeURL(monitorJSON.id)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
"text": textMsg,
|
|
|
|
};
|
|
|
|
|
|
|
|
await axios.post(notification.googleChatWebhookURL, data);
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GoogleChat;
|