2021-09-07 09:42:46 -05:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
2021-10-07 04:39:58 -05:00
|
|
|
const { setSettings, setting } = require("../util-server");
|
2023-01-02 01:01:50 -06:00
|
|
|
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
2021-09-07 09:42:46 -05:00
|
|
|
|
|
|
|
class Slack extends NotificationProvider {
|
|
|
|
|
|
|
|
name = "slack";
|
|
|
|
|
2021-10-07 04:39:58 -05:00
|
|
|
/**
|
|
|
|
* Deprecated property notification.slackbutton
|
|
|
|
* Set it as primary base url if this is not yet set.
|
2022-04-16 14:24:53 -05:00
|
|
|
* @param {string} url The primary base URL to use
|
2021-10-07 04:39:58 -05:00
|
|
|
*/
|
|
|
|
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 09:42:46 -05:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-10-05 14:40:59 -05:00
|
|
|
let okMsg = "Sent Successfully.";
|
2023-05-23 10:29:18 -05:00
|
|
|
|
|
|
|
if (notification.slackchannelmention) {
|
|
|
|
msg += " <!channel>";
|
|
|
|
}
|
2023-04-17 12:49:15 -05:00
|
|
|
|
2021-09-07 09:42:46 -05:00
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let data = {
|
2023-05-23 10:29:18 -05:00
|
|
|
"text": msg,
|
2021-09-07 09:42:46 -05:00
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2021-10-07 04:39:58 -05:00
|
|
|
};
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 09:42:46 -05:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const time = heartbeatJSON["time"];
|
2021-10-13 13:47:23 -05:00
|
|
|
const textMsg = "Uptime Kuma Alert";
|
2021-09-07 09:42:46 -05:00
|
|
|
let data = {
|
2023-05-23 10:29:18 -05:00
|
|
|
"text": `${textMsg}\n${msg}`,
|
2021-09-07 09:42:46 -05:00
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2023-01-02 01:01:50 -06:00
|
|
|
"attachments": [
|
2021-09-07 09:42:46 -05:00
|
|
|
{
|
2023-01-02 01:01:50 -06:00
|
|
|
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
|
|
|
|
"blocks": [
|
|
|
|
{
|
|
|
|
"type": "header",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
2023-04-17 12:49:15 -05:00
|
|
|
"text": textMsg,
|
2023-01-02 01:01:50 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "section",
|
|
|
|
"fields": [{
|
|
|
|
"type": "mrkdwn",
|
2023-05-23 10:29:18 -05:00
|
|
|
"text": "*Message*\n" + msg,
|
2023-01-02 01:01:50 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": "*Time (UTC)*\n" + time,
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
2021-10-07 04:39:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (notification.slackbutton) {
|
|
|
|
await Slack.deprecateURL(notification.slackbutton);
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseURL = await setting("primaryBaseURL");
|
|
|
|
|
|
|
|
// Button
|
|
|
|
if (baseURL) {
|
2023-01-02 01:01:50 -06:00
|
|
|
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-10-07 04:39:58 -05:00
|
|
|
});
|
2021-09-07 09:42:46 -05:00
|
|
|
}
|
2021-10-07 04:39:58 -05:00
|
|
|
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 09:42:46 -05:00
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
2021-10-07 04:39:58 -05:00
|
|
|
this.throwGeneralAxiosError(error);
|
2021-09-07 09:42:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Slack;
|