From a8af2a418e433752570ed124fb3db75534efa070 Mon Sep 17 00:00:00 2001 From: Arniwatt Chonkiattipoom <73098755+pruekk@users.noreply.github.com> Date: Mon, 2 Jan 2023 14:01:50 +0700 Subject: [PATCH] Slack notification block not working (#1958) * [empty commit] pull request for slack notification * Add attachments block for slack notification * chore: update action button in new attachment block * chore: loop in attachments to push blocks * chore: missing semicolon Co-authored-by: pruekanw --- server/notification-providers/slack.js | 67 +++++++++++++++----------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/server/notification-providers/slack.js b/server/notification-providers/slack.js index da1d6e66b..5a5d40cb3 100644 --- a/server/notification-providers/slack.js +++ b/server/notification-providers/slack.js @@ -1,7 +1,7 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); const { setSettings, setting } = require("../util-server"); -const { getMonitorRelativeURL } = require("../../src/util"); +const { getMonitorRelativeURL, UP } = require("../../src/util"); class Slack extends NotificationProvider { @@ -46,24 +46,31 @@ class Slack extends NotificationProvider { "channel": notification.slackchannel, "username": notification.slackusername, "icon_emoji": notification.slackiconemo, - "blocks": [{ - "type": "header", - "text": { - "type": "plain_text", - "text": "Uptime Kuma Alert", - }, - }, - { - "type": "section", - "fields": [{ - "type": "mrkdwn", - "text": "*Message*\n" + msg, - }, + "attachments": [ { - "type": "mrkdwn", - "text": "*Time (UTC)*\n" + time, - }], - }], + "color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a", + "blocks": [ + { + "type": "header", + "text": { + "type": "plain_text", + "text": "Uptime Kuma Alert", + }, + }, + { + "type": "section", + "fields": [{ + "type": "mrkdwn", + "text": "*Message*\n" + msg, + }, + { + "type": "mrkdwn", + "text": "*Time (UTC)*\n" + time, + }], + } + ], + } + ] }; if (notification.slackbutton) { @@ -74,17 +81,19 @@ class Slack extends NotificationProvider { // Button if (baseURL) { - data.blocks.push({ - "type": "actions", - "elements": [{ - "type": "button", - "text": { - "type": "plain_text", - "text": "Visit Uptime Kuma", - }, - "value": "Uptime-Kuma", - "url": baseURL + getMonitorRelativeURL(monitorJSON.id), - }], + data.attachments.forEach(element => { + element.blocks.push({ + "type": "actions", + "elements": [{ + "type": "button", + "text": { + "type": "plain_text", + "text": "Visit Uptime Kuma", + }, + "value": "Uptime-Kuma", + "url": baseURL + getMonitorRelativeURL(monitorJSON.id), + }], + }); }); }