2021-09-12 17:46:59 +00:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
2024-02-28 13:19:32 +00:00
|
|
|
const { setting } = require("../util-server");
|
|
|
|
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
|
2021-09-12 17:46:59 +00:00
|
|
|
|
|
|
|
class Teams extends NotificationProvider {
|
|
|
|
name = "teams";
|
|
|
|
|
2022-04-16 19:24:53 +00:00
|
|
|
/**
|
|
|
|
* Generate the message to send
|
|
|
|
* @param {const} status The status constant
|
|
|
|
* @param {string} monitorName Name of monitor
|
2024-02-28 16:41:00 +00:00
|
|
|
* @param {boolean} withStatusSymbol If the status should be prepended as symbol
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {string} Status message
|
2022-04-16 19:24:53 +00:00
|
|
|
*/
|
2024-02-28 16:41:00 +00:00
|
|
|
_statusMessageFactory = (status, monitorName, withStatusSymbol) => {
|
2021-09-12 17:46:59 +00:00
|
|
|
if (status === DOWN) {
|
2024-02-28 16:41:00 +00:00
|
|
|
return (withStatusSymbol ? "🔴 " : "") + `[${monitorName}] went down`;
|
2021-09-12 17:46:59 +00:00
|
|
|
} else if (status === UP) {
|
2024-02-28 16:41:00 +00:00
|
|
|
return (withStatusSymbol ? "✅ " : "") + `[${monitorName}] is back online`;
|
2021-09-12 17:46:59 +00:00
|
|
|
}
|
2021-09-15 08:38:28 +00:00
|
|
|
return "Notification";
|
2021-09-12 17:46:59 +00:00
|
|
|
};
|
|
|
|
|
2022-04-16 19:24:53 +00:00
|
|
|
/**
|
2024-02-28 08:47:14 +00:00
|
|
|
* Select the style to use based on status
|
2022-04-16 19:24:53 +00:00
|
|
|
* @param {const} status The status constant
|
2024-02-28 08:47:14 +00:00
|
|
|
* @returns {string} Selected style for adaptive cards
|
2022-04-16 19:24:53 +00:00
|
|
|
*/
|
2024-02-28 08:47:14 +00:00
|
|
|
_getStyle = (status) => {
|
2021-09-12 17:46:59 +00:00
|
|
|
if (status === DOWN) {
|
2024-02-28 08:47:14 +00:00
|
|
|
return "attention";
|
2021-09-12 17:46:59 +00:00
|
|
|
}
|
|
|
|
if (status === UP) {
|
2024-02-28 08:47:14 +00:00
|
|
|
return "good";
|
|
|
|
}
|
|
|
|
return "emphasis";
|
|
|
|
};
|
|
|
|
|
2022-04-16 19:24:53 +00:00
|
|
|
/**
|
|
|
|
* Generate payload for notification
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {object} args Method arguments
|
2024-02-28 16:41:00 +00:00
|
|
|
* @param {object} args.heartbeatJSON Heartbeat details
|
2024-02-28 13:19:32 +00:00
|
|
|
* @param {string} args.monitorName Name of the monitor affected
|
|
|
|
* @param {string} args.monitorUrl URL of the monitor affected
|
|
|
|
* @param {string} args.dashboardUrl URL of the dashboard affected
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {object} Notification payload
|
2022-04-16 19:24:53 +00:00
|
|
|
*/
|
2021-09-12 17:46:59 +00:00
|
|
|
_notificationPayloadFactory = ({
|
2024-02-28 16:41:00 +00:00
|
|
|
heartbeatJSON,
|
2021-09-12 17:46:59 +00:00
|
|
|
monitorName,
|
|
|
|
monitorUrl,
|
2024-02-28 13:19:32 +00:00
|
|
|
dashboardUrl,
|
2021-09-12 17:46:59 +00:00
|
|
|
}) => {
|
2024-03-07 14:11:19 +00:00
|
|
|
const status = heartbeatJSON?.status;
|
2021-09-15 08:38:28 +00:00
|
|
|
const facts = [];
|
2024-02-28 16:41:00 +00:00
|
|
|
const actions = [];
|
|
|
|
|
|
|
|
if (dashboardUrl) {
|
|
|
|
actions.push({
|
|
|
|
"type": "Action.OpenUrl",
|
|
|
|
"title": "Visit Uptime Kuma",
|
|
|
|
"url": dashboardUrl
|
|
|
|
});
|
|
|
|
}
|
2021-09-15 08:38:28 +00:00
|
|
|
|
2024-02-28 16:41:00 +00:00
|
|
|
if (heartbeatJSON?.msg) {
|
2024-02-28 08:47:14 +00:00
|
|
|
facts.push({
|
|
|
|
title: "Description",
|
2024-02-28 16:41:00 +00:00
|
|
|
value: heartbeatJSON.msg,
|
2024-02-28 08:47:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:38:28 +00:00
|
|
|
if (monitorName) {
|
|
|
|
facts.push({
|
2024-02-28 08:47:14 +00:00
|
|
|
title: "Monitor",
|
2021-09-15 08:38:28 +00:00
|
|
|
value: monitorName,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-02 01:26:38 +00:00
|
|
|
if (monitorUrl && monitorUrl !== "https://") {
|
2021-09-15 08:38:28 +00:00
|
|
|
facts.push({
|
2024-02-28 08:47:14 +00:00
|
|
|
title: "URL",
|
2024-02-28 13:19:32 +00:00
|
|
|
// format URL as markdown syntax, to be clickable
|
|
|
|
value: `[${monitorUrl}](${monitorUrl})`,
|
2021-09-15 08:38:28 +00:00
|
|
|
});
|
2024-02-28 16:41:00 +00:00
|
|
|
actions.push({
|
|
|
|
"type": "Action.OpenUrl",
|
|
|
|
"title": "Visit Monitor URL",
|
|
|
|
"url": monitorUrl
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heartbeatJSON?.localDateTime) {
|
|
|
|
facts.push({
|
|
|
|
title: "Time",
|
|
|
|
value: heartbeatJSON.localDateTime + (heartbeatJSON.timezone ? ` (${heartbeatJSON.timezone})` : ""),
|
|
|
|
});
|
2021-09-15 08:38:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 13:19:32 +00:00
|
|
|
const payload = {
|
2024-02-28 08:47:14 +00:00
|
|
|
"type": "message",
|
2024-02-28 16:41:00 +00:00
|
|
|
// message with status prefix as notification text
|
|
|
|
"summary": this._statusMessageFactory(status, monitorName, true),
|
2024-02-28 08:47:14 +00:00
|
|
|
"attachments": [
|
2021-09-12 17:46:59 +00:00
|
|
|
{
|
2024-02-28 08:47:14 +00: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",
|
2024-02-28 13:19:32 +00:00
|
|
|
"size": "Medium",
|
2024-02-28 08:47:14 +00:00
|
|
|
"weight": "Bolder",
|
2024-02-28 16:41:00 +00:00
|
|
|
"text": `**${this._statusMessageFactory(status, monitorName, false)}**`,
|
2024-02-28 13:19:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "TextBlock",
|
|
|
|
"size": "Small",
|
|
|
|
"weight": "Default",
|
|
|
|
"text": "Uptime Kuma Alert",
|
|
|
|
"isSubtle": true,
|
|
|
|
"spacing": "None"
|
2024-02-28 08:47:14 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "FactSet",
|
2024-02-28 15:30:48 +00:00
|
|
|
"separator": false,
|
2024-02-28 08:47:14 +00:00
|
|
|
"facts": facts
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
|
|
|
|
"version": "1.5"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2021-09-12 17:46:59 +00:00
|
|
|
};
|
2024-02-28 13:19:32 +00:00
|
|
|
|
2024-02-28 16:41:00 +00:00
|
|
|
if (actions) {
|
2024-02-28 15:31:47 +00:00
|
|
|
payload.attachments[0].content.body.push({
|
|
|
|
"type": "ActionSet",
|
2024-02-28 16:41:00 +00:00
|
|
|
"actions": actions,
|
2024-02-28 13:19:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
2021-09-12 17:46:59 +00:00
|
|
|
};
|
|
|
|
|
2022-04-16 19:24:53 +00:00
|
|
|
/**
|
|
|
|
* Send the notification
|
|
|
|
* @param {string} webhookUrl URL to send the request to
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {object} payload Payload generated by _notificationPayloadFactory
|
|
|
|
* @returns {Promise<void>}
|
2022-04-16 19:24:53 +00:00
|
|
|
*/
|
2021-09-12 17:46:59 +00:00
|
|
|
_sendNotification = async (webhookUrl, payload) => {
|
|
|
|
await axios.post(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
2022-04-16 19:24:53 +00: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 08:38:28 +00:00
|
|
|
_handleGeneralNotification = (webhookUrl, msg) => {
|
2021-09-12 17:46:59 +00:00
|
|
|
const payload = this._notificationPayloadFactory({
|
2024-03-07 14:11:19 +00:00
|
|
|
heartbeatJSON: {
|
|
|
|
msg: msg
|
|
|
|
}
|
2021-09-12 17:46:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return this._sendNotification(webhookUrl, payload);
|
|
|
|
};
|
|
|
|
|
2023-08-11 07:46:41 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-12 17:46:59 +00:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 13:21:15 +00:00
|
|
|
const okMsg = "Sent Successfully.";
|
2021-09-12 17:46:59 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
2021-09-15 08:38:28 +00:00
|
|
|
await this._handleGeneralNotification(notification.webhookUrl, msg);
|
2021-09-12 17:46:59 +00:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
2024-02-28 13:19:32 +00:00
|
|
|
let monitorUrl;
|
2021-09-12 17:46:59 +00:00
|
|
|
|
2022-10-02 01:26:38 +00:00
|
|
|
switch (monitorJSON["type"]) {
|
|
|
|
case "http":
|
|
|
|
case "keywork":
|
2024-02-28 13:19:32 +00:00
|
|
|
monitorUrl = monitorJSON["url"];
|
2022-10-02 01:26:38 +00:00
|
|
|
break;
|
|
|
|
case "docker":
|
2024-02-28 13:19:32 +00:00
|
|
|
monitorUrl = monitorJSON["docker_host"];
|
2022-10-02 01:26:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-02-28 13:19:32 +00:00
|
|
|
monitorUrl = monitorJSON["hostname"];
|
2022-10-02 01:26:38 +00:00
|
|
|
break;
|
2021-09-12 17:46:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 13:19:32 +00:00
|
|
|
const baseURL = await setting("primaryBaseURL");
|
|
|
|
let dashboardUrl;
|
|
|
|
if (baseURL) {
|
|
|
|
dashboardUrl = baseURL + getMonitorRelativeURL(monitorJSON.id);
|
|
|
|
}
|
|
|
|
|
2021-09-12 17:46:59 +00:00
|
|
|
const payload = this._notificationPayloadFactory({
|
2024-02-28 16:41:00 +00:00
|
|
|
heartbeatJSON: heartbeatJSON,
|
2021-09-12 17:46:59 +00:00
|
|
|
monitorName: monitorJSON.name,
|
2024-02-28 13:19:32 +00:00
|
|
|
monitorUrl: monitorUrl,
|
|
|
|
dashboardUrl: dashboardUrl,
|
2021-09-12 17:46:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await this._sendNotification(notification.webhookUrl, payload);
|
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Teams;
|