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 12 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
131 changes: 89 additions & 42 deletions server/notification-providers/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,92 @@ class Slack extends NotificationProvider {
}
}

/**
* Builds the actions available in the slack message
* @param {object} monitorJSON The monitor config
* @returns {Array} The relevant action objects
*/
static async buildActions(monitorJSON) {
const actions = [];

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,
});
}

return actions;
}

/**
* Builds the different blocks the Slack message consists of.
* @param {object} monitorJSON The monitor object
* @param {object} heartbeatJSON The heartbeat object
* @param {string} title The message title
* @param {string} msg The message body
* @returns {Promise<*[object]>} The rich content blocks for the Slack message
DaanMeijer marked this conversation as resolved.
Show resolved Hide resolved
*/
static async buildBlocks(monitorJSON, heartbeatJSON, title, msg) {

//create an array to dynamically add blocks
const blocks = [];

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

// 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"]}`,
}
],
});

const actions = await this.buildActions(monitorJSON);
if (actions.length > 0) {
//the actions block, containing buttons
blocks.push({
"type": "actions",
"elements": actions,
});
}

return blocks;
}

/**
* @inheritdoc
*/
Expand All @@ -49,35 +135,16 @@ class Slack extends NotificationProvider {
return okMsg;
}

const textMsg = "Uptime Kuma Alert";
const title = "Uptime Kuma Alert";
let data = {
"text": `${textMsg}\n${msg}`,
"text": `${title}\n${msg}`,
"channel": notification.slackchannel,
"username": notification.slackusername,
"icon_emoji": notification.slackiconemo,
"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": await Slack.buildBlocks(monitorJSON, heartbeatJSON, title, msg),
}
]
};
Expand All @@ -86,26 +153,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