Skip to content

Commit 69e9fce

Browse files
committed
added comments to code, restructured code to take into account not having any buttons
1 parent c75a418 commit 69e9fce

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

server/notification-providers/slack.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,45 @@ class Slack extends NotificationProvider {
7979
}
8080

8181
const textMsg = "Uptime Kuma Alert";
82+
83+
//create an array to dynamically add blocks
84+
const blocks = [];
85+
86+
// the header block
87+
blocks.push({
88+
"type": "header",
89+
"text": {
90+
"type": "plain_text",
91+
"text": textMsg,
92+
},
93+
});
94+
95+
// the body block, containing the details
96+
blocks.push({
97+
"type": "section",
98+
"fields": [
99+
{
100+
"type": "mrkdwn",
101+
"text": "*Message*\n" + msg,
102+
},
103+
{
104+
"type": "mrkdwn",
105+
"text": `*Time (${heartbeatJSON["timezone"]})*\n${heartbeatJSON["localDateTime"]}`,
106+
}
107+
],
108+
});
109+
110+
//only add this block if we have actions
111+
if(actions.length > 0){
112+
113+
//the actions block, containing buttons
114+
blocks.push({
115+
"type": "actions",
116+
"elements": actions,
117+
});
118+
}
119+
120+
//finally, build the entire slack request object
82121
let data = {
83122
"text": `${textMsg}\n${msg}`,
84123
"channel": notification.slackchannel,
@@ -87,30 +126,7 @@ class Slack extends NotificationProvider {
87126
"attachments": [
88127
{
89128
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
90-
"blocks": [
91-
{
92-
"type": "header",
93-
"text": {
94-
"type": "plain_text",
95-
"text": textMsg,
96-
},
97-
},
98-
{
99-
"type": "section",
100-
"fields": [{
101-
"type": "mrkdwn",
102-
"text": "*Message*\n" + msg,
103-
},
104-
{
105-
"type": "mrkdwn",
106-
"text": `*Time (${heartbeatJSON["timezone"]})*\n${heartbeatJSON["localDateTime"]}`,
107-
}],
108-
},
109-
{
110-
"type": "actions",
111-
"elements": actions,
112-
}
113-
],
129+
"blocks": blocks,
114130
}
115131
]
116132
};

0 commit comments

Comments
 (0)