uptime-kuma/server/notification-providers/slack.js

114 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-09-07 07:42:46 -07:00
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { setSettings, setting } = require("../util-server");
const { getMonitorRelativeURL, UP } = require("../../src/util");
2021-09-07 07:42:46 -07:00
class Slack extends NotificationProvider {
name = "slack";
/**
* Deprecated property notification.slackbutton
* Set it as primary base url if this is not yet set.
* @param {string} url The primary base URL to use
*/
static async deprecateURL(url) {
let currentPrimaryBaseURL = await setting("primaryBaseURL");
if (!currentPrimaryBaseURL) {
console.log("Move the url to be the primary base URL");
await setSettings("general", {
primaryBaseURL: url,
});
} else {
console.log("Already there, no need to move the primary base URL");
}
}
2021-09-07 07:42:46 -07:00
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-10-05 12:40:59 -07:00
let okMsg = "Sent Successfully.";
if (notification.slackchannelnotify) {
msg += " <!channel>";
}
2021-09-07 07:42:46 -07:00
try {
if (heartbeatJSON == null) {
let data = {
"text": msg,
2021-09-07 07:42:46 -07:00
"channel": notification.slackchannel,
"username": notification.slackusername,
"icon_emoji": notification.slackiconemo,
};
await axios.post(notification.slackwebhookURL, data);
2021-09-07 07:42:46 -07:00
return okMsg;
}
const textMsg = "Uptime Kuma Alert";
2021-09-07 07:42:46 -07:00
let data = {
"text": `${textMsg}\n${msg}`,
2021-09-07 07:42:46 -07:00
"channel": notification.slackchannel,
"username": notification.slackusername,
"icon_emoji": notification.slackiconemo,
"attachments": [
2021-09-07 07:42:46 -07:00
{
"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"]}`,
}],
}
],
}
]
};
if (notification.slackbutton) {
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),
}],
});
});
2021-09-07 07:42:46 -07:00
}
await axios.post(notification.slackwebhookURL, data);
2021-09-07 07:42:46 -07:00
return okMsg;
} catch (error) {
this.throwGeneralAxiosError(error);
2021-09-07 07:42:46 -07:00
}
}
}
module.exports = Slack;