diff --git a/server/notification-providers/slack.js b/server/notification-providers/slack.js index bb5a834e0..32f06641c 100644 --- a/server/notification-providers/slack.js +++ b/server/notification-providers/slack.js @@ -75,9 +75,10 @@ class Slack extends NotificationProvider { * @param {object} heartbeatJSON The heartbeat object * @param {string} title The message title * @param {string} msg The message body + * @param {boolean} includeGroupName Whether to include group name in the message * @returns {Array} The rich content blocks for the Slack message */ - buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg) { + buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg, includeGroupName) { //create an array to dynamically add blocks const blocks = []; @@ -91,17 +92,19 @@ class Slack extends NotificationProvider { }); // Optional context line for monitor group path (excluding monitor name) - const groupPath = monitorJSON?.path?.length > 1 ? monitorJSON.path.slice(0, -1).join(" / ") : ""; - if (groupPath) { - blocks.push({ - type: "context", - elements: [ - { - type: "mrkdwn", - text: `_${groupPath}_`, - }, - ], - }); + if (includeGroupName) { + const groupPath = monitorJSON?.path?.length > 1 ? monitorJSON.path.slice(0, -1).join(" / ") : ""; + if (groupPath) { + blocks.push({ + type: "context", + elements: [ + { + type: "mrkdwn", + text: `_${groupPath}_`, + }, + ], + }); + } } // the body block, containing the details @@ -156,6 +159,31 @@ class Slack extends NotificationProvider { const baseURL = await setting("primaryBaseURL"); + // Check if templating is enabled + if (notification.slackUseTemplate) { + const renderedText = await this.renderTemplate( + notification.slackTemplate, + msg, + monitorJSON, + heartbeatJSON + ); + + let data = { + text: renderedText, + channel: notification.slackchannel, + username: notification.slackusername, + icon_emoji: notification.slackiconemo, + }; + + await axios.post(notification.slackwebhookURL, data, config); + return okMsg; + } + + const includeGroupName = notification.slackIncludeGroupName ?? true; + + const groupPath = + includeGroupName && monitorJSON?.path?.length > 1 ? monitorJSON.path.slice(0, -1).join(" / ") : ""; + const title = monitorJSON?.name || "Uptime Kuma Alert"; let data = { text: msg, @@ -168,10 +196,15 @@ class Slack extends NotificationProvider { if (notification.slackrichmessage) { data.attachments.push({ color: heartbeatJSON["status"] === UP ? "#2eb886" : "#e01e5a", - blocks: this.buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg), + blocks: this.buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg, includeGroupName), }); } else { - data.text = `${title}\n${msg}`; + // Include group name in plain text messages if enabled + if (includeGroupName && groupPath) { + data.text = `_${groupPath}_\n${title}\n${msg}`; + } else { + data.text = `${title}\n${msg}`; + } } if (notification.slackbutton) { diff --git a/src/components/notifications/Slack.vue b/src/components/notifications/Slack.vue index 86619c7ff..9f6413259 100644 --- a/src/components/notifications/Slack.vue +++ b/src/components/notifications/Slack.vue @@ -32,6 +32,43 @@ +
+
+ + +
+
+ {{ $t("slackIncludeGroupNameDescription") }} +
+
+ +
+
+ + +
+
+ {{ $t("slackUseTemplateDescription") }} +
+
+ + +
* {{ $t("Required") }} @@ -67,3 +104,29 @@
+ + diff --git a/src/lang/en.json b/src/lang/en.json index 67a8e2a3d..87bd6c19f 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -863,6 +863,10 @@ "see Jira Cloud Docs": "see Jira Cloud Docs", "Jira Service Management": "Jira Service Management", "aboutSlackUsername": "Changes the display name of the message sender. If you want to mention someone, include it in the friendly name instead.", + "slackIncludeGroupName": "Include monitor group name", + "slackIncludeGroupNameDescription": "If enabled, the monitor group path will be included in notifications to help distinguish monitors with the same name across different groups.", + "slackUseTemplate": "Use custom message template", + "slackUseTemplateDescription": "If enabled, the message will be sent using a custom template. You can use Liquid templating to include monitor group information via monitorJSON.path or monitorJSON.pathName.", "aboutChannelName": "Enter the channel name on {0} Channel Name field if you want to bypass the Webhook channel. Ex: #other-channel", "aboutKumaURL": "If you leave the Uptime Kuma URL field blank, it will default to the Project GitHub page.", "smtpDkimSettings": "DKIM Settings",