|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
|
|
|
|
class Teams extends NotificationProvider {
|
|
|
|
name = "teams";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the message to send
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @param {string} monitorName Name of monitor
|
|
|
|
* @returns {string} Status message
|
|
|
|
*/
|
|
|
|
_statusMessageFactory = (status, monitorName) => {
|
|
|
|
if (status === DOWN) {
|
|
|
|
return `🔴 Application [${monitorName}] went down`;
|
|
|
|
} else if (status === UP) {
|
|
|
|
return `✅ Application [${monitorName}] is back online`;
|
|
|
|
}
|
|
|
|
return "Notification";
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select the style to use based on status
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @returns {string} Selected style for adaptive cards
|
|
|
|
*/
|
|
|
|
_getStyle = (status) => {
|
|
|
|
if (status === DOWN) {
|
|
|
|
return "attention";
|
|
|
|
}
|
|
|
|
if (status === UP) {
|
|
|
|
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})`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate payload for notification
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
_notificationPayloadFactory = ({
|
|
|
|
status,
|
|
|
|
monitorMessage,
|
|
|
|
monitorName,
|
|
|
|
monitorUrl,
|
|
|
|
}) => {
|
|
|
|
const notificationMessage = this._statusMessageFactory(
|
|
|
|
status,
|
|
|
|
monitorName
|
|
|
|
);
|
|
|
|
|
|
|
|
const facts = [];
|
|
|
|
|
|
|
|
if (monitorMessage) {
|
|
|
|
facts.push({
|
|
|
|
title: "Description",
|
|
|
|
value: monitorMessage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitorName) {
|
|
|
|
facts.push({
|
|
|
|
title: "Monitor",
|
|
|
|
value: monitorName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monitorUrl && monitorUrl !== "https://") {
|
|
|
|
facts.push({
|
|
|
|
title: "URL",
|
|
|
|
value: this._formatAsMarkdownLink(monitorUrl),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"type": "message",
|
|
|
|
"attachments": [
|
|
|
|
{
|
|
|
|
"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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the notification
|
|
|
|
* @param {string} webhookUrl URL to send the request to
|
|
|
|
* @param {object} payload Payload generated by _notificationPayloadFactory
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
_sendNotification = async (webhookUrl, payload) => {
|
|
|
|
await axios.post(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a general notification
|
|
|
|
* @param {string} webhookUrl URL to send request to
|
|
|
|
* @param {string} msg Message to send
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
_handleGeneralNotification = (webhookUrl, msg) => {
|
|
|
|
const payload = this._notificationPayloadFactory({
|
|
|
|
monitorMessage: msg
|
|
|
|
});
|
|
|
|
|
|
|
|
return this._sendNotification(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
|
|
let okMsg = "Sent Successfully.";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
await this._handleGeneralNotification(notification.webhookUrl, msg);
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
let url;
|
|
|
|
|
|
|
|
switch (monitorJSON["type"]) {
|
|
|
|
case "http":
|
|
|
|
case "keywork":
|
|
|
|
url = monitorJSON["url"];
|
|
|
|
break;
|
|
|
|
case "docker":
|
|
|
|
url = monitorJSON["docker_host"];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
url = monitorJSON["hostname"];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = this._notificationPayloadFactory({
|
|
|
|
monitorMessage: heartbeatJSON.msg,
|
|
|
|
monitorName: monitorJSON.name,
|
|
|
|
monitorUrl: url,
|
|
|
|
status: heartbeatJSON.status,
|
|
|
|
});
|
|
|
|
|
|
|
|
await this._sendNotification(notification.webhookUrl, payload);
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Teams;
|