refactor MS-Teams notification to use adaptive cards

This commit is contained in:
taschenuhr 2024-02-28 09:47:14 +01:00
parent d88e7702b9
commit 7756070c57

View File

@ -21,18 +21,32 @@ class Teams extends NotificationProvider {
}; };
/** /**
* Select theme color to use based on status * Select the style to use based on status
* @param {const} status The status constant * @param {const} status The status constant
* @returns {string} Selected color in hex RGB format * @returns {string} Selected style for adaptive cards
*/ */
_getThemeColor = (status) => { _getStyle = (status) => {
if (status === DOWN) { if (status === DOWN) {
return "ff0000"; return "attention";
} }
if (status === UP) { if (status === UP) {
return "00e804"; 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})`;
} }
return "008cff";
}; };
/** /**
@ -57,40 +71,91 @@ class Teams extends NotificationProvider {
const facts = []; const facts = [];
if (monitorMessage) {
facts.push({
title: "Description",
value: monitorMessage,
});
}
if (monitorName) { if (monitorName) {
facts.push({ facts.push({
name: "Monitor", title: "Monitor",
value: monitorName, value: monitorName,
}); });
} }
if (monitorUrl && monitorUrl !== "https://") { if (monitorUrl && monitorUrl !== "https://") {
facts.push({ facts.push({
name: "URL", title: "URL",
value: monitorUrl, value: this._formatAsMarkdownLink(monitorUrl),
}); });
} }
return { return {
"@context": "https://schema.org/extensions", "type": "message",
"@type": "MessageCard", "attachments": [
themeColor: this._getThemeColor(status),
summary: notificationMessage,
sections: [
{ {
activityImage: "contentType": "application/vnd.microsoft.card.adaptive",
"https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png", "contentUrl": "",
activityTitle: "**Uptime Kuma**", "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"
}
]
}, },
{ {
activityTitle: notificationMessage, "type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"size": "Large",
"weight": "Bolder",
"text": "**Uptime Kuma Alert**"
}
]
}
]
}
]
}, },
{ {
activityTitle: "**Description**", "type": "TextBlock",
text: monitorMessage, "weight": "Bolder",
facts, "text": notificationMessage,
"separator": true,
"wrap": true
}, },
{
"type": "FactSet",
"facts": facts
}
], ],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5"
}
}
]
}; };
}; };