From 4daa1ef5ac7cf40e02af04893468a966541d69ea Mon Sep 17 00:00:00 2001 From: Tobias Thiemann Date: Tue, 23 Jul 2024 09:09:16 +0200 Subject: [PATCH 1/4] Backport Teams Adaptive-Templeate This commit backports the Adaptive-Templeate. The version of the template uses is from commit 2c31f3a2ff37d96331b0dc14d896eea37f76c47a. --- server/notification-providers/teams.js | 173 ++++++++++++++++++++++++- 1 file changed, 166 insertions(+), 7 deletions(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index bcfc82c24..389445f55 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -35,6 +35,21 @@ class Teams extends NotificationProvider { return "008cff"; }; + /** + * 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"; + }; + /** * Generate payload for notification * @param {const} status The status of the monitor @@ -48,6 +63,137 @@ class Teams extends NotificationProvider { monitorMessage, monitorName, monitorUrl, + }) => { + const facts = []; + const actions = []; + + if (monitorMessage) { + facts.push({ + title: "Description", + value: monitorMessage, + }); + } + + if (monitorName) { + facts.push({ + title: "Monitor", + value: monitorName, + }); + } + + if (monitorUrl && monitorUrl !== "https://") { + facts.push({ + title: "URL", + // format URL as markdown syntax, to be clickable + value: `[${monitorUrl}](${monitorUrl})`, + }); + 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})` : ""), + // }); + // } + + const payload = { + "type": "message", + // message with status prefix as notification text + "summary": this._statusMessageFactory(status, monitorName, true), + "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": "Medium", + "weight": "Bolder", + "text": `**${this._statusMessageFactory(status, monitorName, false)}**`, + }, + { + "type": "TextBlock", + "size": "Small", + "weight": "Default", + "text": "Uptime Kuma Alert", + "isSubtle": true, + "spacing": "None" + } + ] + } + ] + } + ] + }, + { + "type": "FactSet", + "separator": false, + "facts": facts + } + ], + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.5" + } + } + ] + }; + + if (actions) { + payload.attachments[0].content.body.push({ + "type": "ActionSet", + "actions": actions, + }); + } + + return payload; + } + + /** + * Generate payload for notification + * @param {const} status The status of the monitor + * @param {string} monitorMessage Message to send + * @param {string} monitorName Name of monitor affected + * @param {string} monitorUrl URL of monitor affected + * @returns {Object} + */ + _notificationPayloadFactoryLegacy = ({ + status, + monitorMessage, + monitorName, + monitorUrl, }) => { const notificationMessage = this._statusMessageFactory( status, @@ -140,14 +286,27 @@ class Teams extends NotificationProvider { break; } - const payload = this._notificationPayloadFactory({ - monitorMessage: heartbeatJSON.msg, - monitorName: monitorJSON.name, - monitorUrl: url, - status: heartbeatJSON.status, - }); + if (notification.webhookUrl.includes("azure.com")) { + const payload = this._notificationPayloadFactory({ + monitorMessage: heartbeatJSON.msg, + monitorName: monitorJSON.name, + monitorUrl: url, + status: heartbeatJSON.status, + }); + + await this._sendNotification(notification.webhookUrl, payload); + } + else { + const payload = this._notificationPayloadFactoryLegacy({ + monitorMessage: heartbeatJSON.msg, + monitorName: monitorJSON.name, + monitorUrl: url, + status: heartbeatJSON.status, + }); + + await this._sendNotification(notification.webhookUrl, payload); + } - await this._sendNotification(notification.webhookUrl, payload); return okMsg; } catch (error) { this.throwGeneralAxiosError(error); From 27266f60ba61e379232f3ebee9989fb047064b5c Mon Sep 17 00:00:00 2001 From: Tobias Thiemann Date: Tue, 23 Jul 2024 10:21:54 +0200 Subject: [PATCH 2/4] downgrade adaptive-card version to 1.4 downgrade version, otherwise the error "We're sorry, this card couldn't be displayed" will be displayed when using flowbot, which is default --- server/notification-providers/teams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index 389445f55..4eae4a663 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -165,7 +165,7 @@ class Teams extends NotificationProvider { } ], "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "version": "1.5" + "version": "1.4" } } ] From 5332b3fc9bd1c5b30da288cb4d49eec617601624 Mon Sep 17 00:00:00 2001 From: Tobias Thiemann Date: Tue, 23 Jul 2024 10:56:55 +0200 Subject: [PATCH 3/4] fix lint issues --- server/notification-providers/teams.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index 4eae4a663..3169b98a6 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -179,7 +179,7 @@ class Teams extends NotificationProvider { } return payload; - } + }; /** * Generate payload for notification @@ -295,8 +295,7 @@ class Teams extends NotificationProvider { }); await this._sendNotification(notification.webhookUrl, payload); - } - else { + } else { const payload = this._notificationPayloadFactoryLegacy({ monitorMessage: heartbeatJSON.msg, monitorName: monitorJSON.name, From 7ca064f24d64afcb6094f5ec5f5505eace524b90 Mon Sep 17 00:00:00 2001 From: tonypai Date: Tue, 3 Sep 2024 03:43:46 +0000 Subject: [PATCH 4/4] Fix and remove legacy code --- server/notification-providers/teams.js | 92 +++----------------------- 1 file changed, 8 insertions(+), 84 deletions(-) diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index 3169b98a6..9a1d0b79b 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -94,17 +94,10 @@ class Teams extends NotificationProvider { }); } - // if (heartbeatJSON?.localDateTime) { - // facts.push({ - // title: "Time", - // value: heartbeatJSON.localDateTime + (heartbeatJSON.timezone ? ` (${heartbeatJSON.timezone})` : ""), - // }); - // } - const payload = { "type": "message", // message with status prefix as notification text - "summary": this._statusMessageFactory(status, monitorName, true), + "summary": this._statusMessageFactory(status, monitorName), "attachments": [ { "contentType": "application/vnd.microsoft.card.adaptive", @@ -181,64 +174,6 @@ class Teams extends NotificationProvider { return payload; }; - /** - * Generate payload for notification - * @param {const} status The status of the monitor - * @param {string} monitorMessage Message to send - * @param {string} monitorName Name of monitor affected - * @param {string} monitorUrl URL of monitor affected - * @returns {Object} - */ - _notificationPayloadFactoryLegacy = ({ - status, - monitorMessage, - monitorName, - monitorUrl, - }) => { - const notificationMessage = this._statusMessageFactory( - status, - monitorName - ); - - const facts = []; - - if (monitorName) { - facts.push({ - name: "Monitor", - value: monitorName, - }); - } - - if (monitorUrl && monitorUrl !== "https://") { - facts.push({ - name: "URL", - value: monitorUrl, - }); - } - - return { - "@context": "https://schema.org/extensions", - "@type": "MessageCard", - themeColor: this._getThemeColor(status), - summary: notificationMessage, - sections: [ - { - activityImage: - "https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png", - activityTitle: "**Uptime Kuma**", - }, - { - activityTitle: notificationMessage, - }, - { - activityTitle: "**Description**", - text: monitorMessage, - facts, - }, - ], - }; - }; - /** * Send the notification * @param {string} webhookUrl URL to send the request to @@ -286,25 +221,14 @@ class Teams extends NotificationProvider { break; } - if (notification.webhookUrl.includes("azure.com")) { - const payload = this._notificationPayloadFactory({ - monitorMessage: heartbeatJSON.msg, - monitorName: monitorJSON.name, - monitorUrl: url, - status: heartbeatJSON.status, - }); + const payload = this._notificationPayloadFactory({ + monitorMessage: heartbeatJSON.msg, + monitorName: monitorJSON.name, + monitorUrl: url, + status: heartbeatJSON.status, + }); - await this._sendNotification(notification.webhookUrl, payload); - } else { - const payload = this._notificationPayloadFactoryLegacy({ - monitorMessage: heartbeatJSON.msg, - monitorName: monitorJSON.name, - monitorUrl: url, - status: heartbeatJSON.status, - }); - - await this._sendNotification(notification.webhookUrl, payload); - } + await this._sendNotification(notification.webhookUrl, payload); return okMsg; } catch (error) {