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