2021-09-07 10:42:46 -04:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
2021-10-07 05:39:58 -04:00
|
|
|
const { setSettings, setting } = require("../util-server");
|
|
|
|
const { getMonitorRelativeURL } = require("../../src/util");
|
2021-09-07 10:42:46 -04:00
|
|
|
|
|
|
|
class Slack extends NotificationProvider {
|
|
|
|
|
|
|
|
name = "slack";
|
|
|
|
|
2021-10-07 05:39:58 -04:00
|
|
|
/**
|
|
|
|
* Deprecated property notification.slackbutton
|
|
|
|
* Set it as primary base url if this is not yet set.
|
|
|
|
*/
|
|
|
|
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 10:42:46 -04:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2021-10-05 15:40:59 -04:00
|
|
|
let okMsg = "Sent Successfully.";
|
2021-09-07 10:42:46 -04:00
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let data = {
|
2021-10-07 05:39:58 -04:00
|
|
|
"text": msg,
|
2021-09-07 10:42:46 -04:00
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2021-10-07 05:39:58 -04:00
|
|
|
};
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 10:42:46 -04:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const time = heartbeatJSON["time"];
|
|
|
|
let data = {
|
|
|
|
"text": "Uptime Kuma Alert",
|
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
|
|
|
"blocks": [{
|
|
|
|
"type": "header",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Uptime Kuma Alert",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "section",
|
|
|
|
"fields": [{
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": "*Message*\n" + msg,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": "*Time (UTC)*\n" + time,
|
|
|
|
}],
|
2021-10-07 05:39:58 -04:00
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (notification.slackbutton) {
|
|
|
|
await Slack.deprecateURL(notification.slackbutton);
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseURL = await setting("primaryBaseURL");
|
|
|
|
|
|
|
|
// Button
|
|
|
|
if (baseURL) {
|
|
|
|
data.blocks.push({
|
2021-09-07 10:42:46 -04:00
|
|
|
"type": "actions",
|
2021-10-07 05:39:58 -04:00
|
|
|
"elements": [{
|
|
|
|
"type": "button",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Visit Uptime Kuma",
|
2021-09-07 10:42:46 -04:00
|
|
|
},
|
2021-10-07 05:39:58 -04:00
|
|
|
"value": "Uptime-Kuma",
|
|
|
|
"url": baseURL + getMonitorRelativeURL(monitorJSON.id),
|
|
|
|
}],
|
|
|
|
});
|
2021-09-07 10:42:46 -04:00
|
|
|
}
|
2021-10-07 05:39:58 -04:00
|
|
|
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 10:42:46 -04:00
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
2021-10-07 05:39:58 -04:00
|
|
|
this.throwGeneralAxiosError(error);
|
2021-09-07 10:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Slack;
|