2021-09-12 19:46:59 +02:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
|
|
|
|
class Teams extends NotificationProvider {
|
|
|
|
name = "teams";
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Generate the message to send
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @param {string} monitorName Name of monitor
|
2023-08-11 09:46:41 +02:00
|
|
|
* @returns {string} Status message
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2021-09-12 19:46:59 +02:00
|
|
|
_statusMessageFactory = (status, monitorName) => {
|
|
|
|
if (status === DOWN) {
|
|
|
|
return `🔴 Application [${monitorName}] went down`;
|
|
|
|
} else if (status === UP) {
|
|
|
|
return `✅ Application [${monitorName}] is back online`;
|
|
|
|
}
|
2021-09-15 10:38:28 +02:00
|
|
|
return "Notification";
|
2021-09-12 19:46:59 +02:00
|
|
|
};
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
2024-02-28 10:47:14 +02:00
|
|
|
* Select the style to use based on status
|
2022-04-16 21:24:53 +02:00
|
|
|
* @param {const} status The status constant
|
2024-02-28 10:47:14 +02:00
|
|
|
* @returns {string} Selected style for adaptive cards
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2024-02-28 10:47:14 +02:00
|
|
|
_getStyle = (status) => {
|
2021-09-12 19:46:59 +02:00
|
|
|
if (status === DOWN) {
|
2024-02-28 10:47:14 +02:00
|
|
|
return "attention";
|
2021-09-12 19:46:59 +02:00
|
|
|
}
|
|
|
|
if (status === UP) {
|
2024-02-28 10:47:14 +02:00
|
|
|
return "good";
|
|
|
|
}
|
|
|
|
return "emphasis";
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format an URL in the markdown format
|
|
|
|
* @param {string} url An absolute URL
|
|
|
|
* @param {string} linkName Optional name of the link
|
|
|
|
* @returns {string} The URL formatted as markdown link
|
|
|
|
*/
|
|
|
|
_formatAsMarkdownLink = (url, linkName) => {
|
|
|
|
if (linkName) {
|
|
|
|
return `[${linkName}](${url})`;
|
|
|
|
} else {
|
|
|
|
return `[${url}](${url})`;
|
2021-09-12 19:46:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Generate payload for notification
|
2023-08-11 09:46:41 +02:00
|
|
|
* @param {object} args Method arguments
|
|
|
|
* @param {const} args.status The status of the monitor
|
|
|
|
* @param {string} args.monitorMessage Message to send
|
|
|
|
* @param {string} args.monitorName Name of monitor affected
|
|
|
|
* @param {string} args.monitorUrl URL of monitor affected
|
|
|
|
* @returns {object} Notification payload
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2021-09-12 19:46:59 +02:00
|
|
|
_notificationPayloadFactory = ({
|
|
|
|
status,
|
|
|
|
monitorMessage,
|
|
|
|
monitorName,
|
|
|
|
monitorUrl,
|
|
|
|
}) => {
|
|
|
|
const notificationMessage = this._statusMessageFactory(
|
|
|
|
status,
|
|
|
|
monitorName
|
|
|
|
);
|
2021-09-15 10:38:28 +02:00
|
|
|
|
|
|
|
const facts = [];
|
|
|
|
|
2024-02-28 10:47:14 +02:00
|
|
|
if (monitorMessage) {
|
|
|
|
facts.push({
|
|
|
|
title: "Description",
|
|
|
|
value: monitorMessage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-15 10:38:28 +02:00
|
|
|
if (monitorName) {
|
|
|
|
facts.push({
|
2024-02-28 10:47:14 +02:00
|
|
|
title: "Monitor",
|
2021-09-15 10:38:28 +02:00
|
|
|
value: monitorName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-02 03:26:38 +02:00
|
|
|
if (monitorUrl && monitorUrl !== "https://") {
|
2021-09-15 10:38:28 +02:00
|
|
|
facts.push({
|
2024-02-28 10:47:14 +02:00
|
|
|
title: "URL",
|
|
|
|
value: this._formatAsMarkdownLink(monitorUrl),
|
2021-09-15 10:38:28 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-12 19:46:59 +02:00
|
|
|
return {
|
2024-02-28 10:47:14 +02:00
|
|
|
"type": "message",
|
|
|
|
"attachments": [
|
2021-09-12 19:46:59 +02:00
|
|
|
{
|
2024-02-28 10:47:14 +02:00
|
|
|
"contentType": "application/vnd.microsoft.card.adaptive",
|
|
|
|
"contentUrl": "",
|
|
|
|
"content": {
|
|
|
|
"type": "AdaptiveCard",
|
|
|
|
"body": [
|
|
|
|
{
|
|
|
|
"type": "Container",
|
|
|
|
"verticalContentAlignment": "Center",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"type": "ColumnSet",
|
|
|
|
"style": this._getStyle(status),
|
|
|
|
"columns": [
|
|
|
|
{
|
|
|
|
"type": "Column",
|
|
|
|
"width": "auto",
|
|
|
|
"verticalContentAlignment": "Center",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"type": "Image",
|
|
|
|
"width": "32px",
|
|
|
|
"style": "Person",
|
|
|
|
"url": "https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png",
|
|
|
|
"altText": "Uptime Kuma Logo"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "Column",
|
|
|
|
"width": "stretch",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"type": "TextBlock",
|
|
|
|
"size": "Large",
|
|
|
|
"weight": "Bolder",
|
|
|
|
"text": "**Uptime Kuma Alert**"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "TextBlock",
|
|
|
|
"weight": "Bolder",
|
|
|
|
"text": notificationMessage,
|
|
|
|
"separator": true,
|
|
|
|
"wrap": true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "FactSet",
|
|
|
|
"facts": facts
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
|
|
"version": "1.5"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2021-09-12 19:46:59 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Send the notification
|
|
|
|
* @param {string} webhookUrl URL to send the request to
|
2023-08-11 09:46:41 +02:00
|
|
|
* @param {object} payload Payload generated by _notificationPayloadFactory
|
|
|
|
* @returns {Promise<void>}
|
2022-04-16 21:24:53 +02:00
|
|
|
*/
|
2021-09-12 19:46:59 +02:00
|
|
|
_sendNotification = async (webhookUrl, payload) => {
|
|
|
|
await axios.post(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
2022-04-16 21:24:53 +02:00
|
|
|
/**
|
|
|
|
* Send a general notification
|
|
|
|
* @param {string} webhookUrl URL to send request to
|
|
|
|
* @param {string} msg Message to send
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-09-15 10:38:28 +02:00
|
|
|
_handleGeneralNotification = (webhookUrl, msg) => {
|
2021-09-12 19:46:59 +02:00
|
|
|
const payload = this._notificationPayloadFactory({
|
2021-09-15 10:38:28 +02:00
|
|
|
monitorMessage: msg
|
2021-09-12 19:46:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return this._sendNotification(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
2023-08-11 09:46:41 +02:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-12 19:46:59 +02:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-10-05 21:40:59 +02:00
|
|
|
let okMsg = "Sent Successfully.";
|
2021-09-12 19:46:59 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
2021-09-15 10:38:28 +02:00
|
|
|
await this._handleGeneralNotification(notification.webhookUrl, msg);
|
2021-09-12 19:46:59 +02:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
let url;
|
|
|
|
|
2022-10-02 03:26:38 +02:00
|
|
|
switch (monitorJSON["type"]) {
|
|
|
|
case "http":
|
|
|
|
case "keywork":
|
|
|
|
url = monitorJSON["url"];
|
|
|
|
break;
|
|
|
|
case "docker":
|
|
|
|
url = monitorJSON["docker_host"];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
url = monitorJSON["hostname"];
|
|
|
|
break;
|
2021-09-12 19:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const payload = this._notificationPayloadFactory({
|
2021-09-15 10:38:28 +02:00
|
|
|
monitorMessage: heartbeatJSON.msg,
|
2021-09-12 19:46:59 +02:00
|
|
|
monitorName: monitorJSON.name,
|
|
|
|
monitorUrl: url,
|
|
|
|
status: heartbeatJSON.status,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._sendNotification(notification.webhookUrl, payload);
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Teams;
|