Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slack] restructure alert actions, add 'visit site' button #3886

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 69 additions & 40 deletions server/notification-providers/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,75 @@ class Slack extends NotificationProvider {
return okMsg;
}

const actions = [];
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved

const baseURL = await setting("primaryBaseURL");

if (baseURL) {
actions.push({
"type": "button",
"text": {
"type": "plain_text",
"text": "Visit Uptime Kuma",
},
"value": "Uptime-Kuma",
"url": baseURL + getMonitorRelativeURL(monitorJSON.id),
});

}

if (monitorJSON.url) {
actions.push({
"type": "button",
"text": {
"type": "plain_text",
"text": "Visit site",
},
"value": "Site",
"url": monitorJSON.url,
});
}

const textMsg = "Uptime Kuma Alert";

//create an array to dynamically add blocks
const blocks = [];
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved

// the header block
blocks.push({
"type": "header",
"text": {
"type": "plain_text",
"text": textMsg,
},
});

// the body block, containing the details
blocks.push({
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Message*\n" + msg,
},
{
"type": "mrkdwn",
"text": `*Time (${heartbeatJSON["timezone"]})*\n${heartbeatJSON["localDateTime"]}`,
}
],
});

//only add this block if we have actions
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
if (actions.length > 0) {

CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
//the actions block, containing buttons
blocks.push({
"type": "actions",
"elements": actions,
});
}

//finally, build the entire slack request object
let data = {
"text": `${textMsg}\n${msg}`,
"channel": notification.slackchannel,
Expand All @@ -58,26 +126,7 @@ class Slack extends NotificationProvider {
"attachments": [
{
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": textMsg,
},
},
{
"type": "section",
"fields": [{
"type": "mrkdwn",
"text": "*Message*\n" + msg,
},
{
"type": "mrkdwn",
"text": `*Time (${heartbeatJSON["timezone"]})*\n${heartbeatJSON["localDateTime"]}`,
}],
}
],
"blocks": blocks,
DaanMeijer marked this conversation as resolved.
Show resolved Hide resolved
}
]
};
Expand All @@ -86,26 +135,6 @@ class Slack extends NotificationProvider {
await Slack.deprecateURL(notification.slackbutton);
}

const baseURL = await setting("primaryBaseURL");

// Button
if (baseURL) {
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),
}],
});
});
}

await axios.post(notification.slackwebhookURL, data);
return okMsg;
} catch (error) {
Expand Down