push notificationMessage to card header and add action button to dashboard

This commit is contained in:
taschenuhr 2024-02-28 14:19:32 +01:00
parent 7756070c57
commit 5b87da94b3

View file

@ -1,6 +1,7 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const axios = require("axios"); const axios = require("axios");
const { DOWN, UP } = require("../../src/util"); const { setting } = require("../util-server");
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
class Teams extends NotificationProvider { class Teams extends NotificationProvider {
name = "teams"; name = "teams";
@ -13,9 +14,9 @@ class Teams extends NotificationProvider {
*/ */
_statusMessageFactory = (status, monitorName) => { _statusMessageFactory = (status, monitorName) => {
if (status === DOWN) { if (status === DOWN) {
return `🔴 Application [${monitorName}] went down`; return `🔴 [${monitorName}] went down`;
} else if (status === UP) { } else if (status === UP) {
return ` Application [${monitorName}] is back online`; return ` [${monitorName}] is back online`;
} }
return "Notification"; return "Notification";
}; };
@ -35,27 +36,14 @@ class Teams extends NotificationProvider {
return "emphasis"; 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 * Generate payload for notification
* @param {object} args Method arguments * @param {object} args Method arguments
* @param {const} args.status The status of the monitor * @param {const} args.status The status of the monitor
* @param {string} args.monitorMessage Message to send * @param {string} args.monitorMessage Message to send
* @param {string} args.monitorName Name of monitor affected * @param {string} args.monitorName Name of the monitor affected
* @param {string} args.monitorUrl URL of monitor affected * @param {string} args.monitorUrl URL of the monitor affected
* @param {string} args.dashboardUrl URL of the dashboard affected
* @returns {object} Notification payload * @returns {object} Notification payload
*/ */
_notificationPayloadFactory = ({ _notificationPayloadFactory = ({
@ -63,6 +51,7 @@ class Teams extends NotificationProvider {
monitorMessage, monitorMessage,
monitorName, monitorName,
monitorUrl, monitorUrl,
dashboardUrl,
}) => { }) => {
const notificationMessage = this._statusMessageFactory( const notificationMessage = this._statusMessageFactory(
status, status,
@ -88,11 +77,14 @@ class Teams extends NotificationProvider {
if (monitorUrl && monitorUrl !== "https://") { if (monitorUrl && monitorUrl !== "https://") {
facts.push({ facts.push({
title: "URL", title: "URL",
value: this._formatAsMarkdownLink(monitorUrl), // format URL as markdown syntax, to be clickable
value: `[${monitorUrl}](${monitorUrl})`,
}); });
} }
return { const headerMessage = `**${notificationMessage}**`;
const payload = {
"type": "message", "type": "message",
"attachments": [ "attachments": [
{ {
@ -129,25 +121,27 @@ class Teams extends NotificationProvider {
"items": [ "items": [
{ {
"type": "TextBlock", "type": "TextBlock",
"size": "Large", "size": "Medium",
"weight": "Bolder", "weight": "Bolder",
"text": "**Uptime Kuma Alert**" "text": headerMessage,
}
]
}
]
}
]
}, },
{ {
"type": "TextBlock", "type": "TextBlock",
"weight": "Bolder", "size": "Small",
"text": notificationMessage, "weight": "Default",
"separator": true, "text": "Uptime Kuma Alert",
"wrap": true "isSubtle": true,
"spacing": "None"
}
]
}
]
}
]
}, },
{ {
"type": "FactSet", "type": "FactSet",
"separator": true,
"facts": facts "facts": facts
} }
], ],
@ -157,6 +151,23 @@ class Teams extends NotificationProvider {
} }
] ]
}; };
if (dashboardUrl) {
payload.attachments.forEach(element => {
element.content.push({
"type": "ActionSet",
"actions": [
{
"type": "Action.OpenUrl",
"title": "Visit Uptime Kuma",
"url": dashboardUrl
}
]
});
});
}
return payload;
}; };
/** /**
@ -195,26 +206,33 @@ class Teams extends NotificationProvider {
return okMsg; return okMsg;
} }
let url; let monitorUrl;
switch (monitorJSON["type"]) { switch (monitorJSON["type"]) {
case "http": case "http":
case "keywork": case "keywork":
url = monitorJSON["url"]; monitorUrl = monitorJSON["url"];
break; break;
case "docker": case "docker":
url = monitorJSON["docker_host"]; monitorUrl = monitorJSON["docker_host"];
break; break;
default: default:
url = monitorJSON["hostname"]; monitorUrl = monitorJSON["hostname"];
break; break;
} }
const baseURL = await setting("primaryBaseURL");
let dashboardUrl;
if (baseURL) {
dashboardUrl = baseURL + getMonitorRelativeURL(monitorJSON.id);
}
const payload = this._notificationPayloadFactory({ const payload = this._notificationPayloadFactory({
monitorMessage: heartbeatJSON.msg, monitorMessage: heartbeatJSON.msg,
monitorName: monitorJSON.name, monitorName: monitorJSON.name,
monitorUrl: url, monitorUrl: monitorUrl,
status: heartbeatJSON.status, status: heartbeatJSON.status,
dashboardUrl: dashboardUrl,
}); });
await this._sendNotification(notification.webhookUrl, payload); await this._sendNotification(notification.webhookUrl, payload);