2021-09-07 07:42:46 -07:00
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
2024-10-08 16:43:44 -07:00
|
|
|
const { setSettings, setting } = require("../util-server");
|
2023-01-01 23:01:50 -08:00
|
|
|
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
2021-09-07 07:42:46 -07:00
|
|
|
|
|
|
|
class Slack extends NotificationProvider {
|
|
|
|
name = "slack";
|
|
|
|
|
2021-10-07 02:39:58 -07:00
|
|
|
/**
|
|
|
|
* Deprecated property notification.slackbutton
|
|
|
|
* Set it as primary base url if this is not yet set.
|
2023-08-11 00:46:41 -07:00
|
|
|
* @deprecated
|
2022-04-16 12:24:53 -07:00
|
|
|
* @param {string} url The primary base URL to use
|
2023-08-11 00:46:41 -07:00
|
|
|
* @returns {Promise<void>}
|
2021-10-07 02:39:58 -07:00
|
|
|
*/
|
|
|
|
static async deprecateURL(url) {
|
2024-10-08 16:43:44 -07:00
|
|
|
let currentPrimaryBaseURL = await setting("primaryBaseURL");
|
2021-10-07 02:39:58 -07:00
|
|
|
|
|
|
|
if (!currentPrimaryBaseURL) {
|
2024-10-08 16:43:44 -07:00
|
|
|
console.log("Move the url to be the primary base URL");
|
|
|
|
await setSettings("general", {
|
|
|
|
primaryBaseURL: url,
|
|
|
|
});
|
2021-10-07 02:39:58 -07:00
|
|
|
} else {
|
2024-10-08 16:43:44 -07:00
|
|
|
console.log("Already there, no need to move the primary base URL");
|
2021-10-07 02:39:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 17:43:54 -07:00
|
|
|
/**
|
|
|
|
* Builds the actions available in the slack message
|
|
|
|
* @param {string} baseURL Uptime Kuma base URL
|
|
|
|
* @param {object} monitorJSON The monitor config
|
|
|
|
* @returns {Array} The relevant action objects
|
|
|
|
*/
|
|
|
|
static buildActions(baseURL, monitorJSON) {
|
|
|
|
const actions = [];
|
|
|
|
|
|
|
|
if (baseURL) {
|
|
|
|
actions.push({
|
|
|
|
"type": "button",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Visit Uptime Kuma",
|
|
|
|
},
|
|
|
|
"value": "Uptime-Kuma",
|
|
|
|
"url": baseURL + getMonitorRelativeURL(monitorJSON.id),
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-27 11:04:03 -07:00
|
|
|
const address = this.extractAdress(monitorJSON);
|
|
|
|
if (address) {
|
2024-04-01 17:43:54 -07:00
|
|
|
actions.push({
|
|
|
|
"type": "button",
|
|
|
|
"text": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": "Visit site",
|
|
|
|
},
|
|
|
|
"value": "Site",
|
2024-06-27 11:04:03 -07:00
|
|
|
"url": address,
|
2024-04-01 17:43:54 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the different blocks the Slack message consists of.
|
|
|
|
* @param {string} baseURL Uptime Kuma base URL
|
|
|
|
* @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 {Array<object>} The rich content blocks for the Slack message
|
|
|
|
*/
|
|
|
|
static buildBlocks(baseURL, 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 = this.buildActions(baseURL, monitorJSON);
|
|
|
|
if (actions.length > 0) {
|
|
|
|
//the actions block, containing buttons
|
|
|
|
blocks.push({
|
|
|
|
"type": "actions",
|
|
|
|
"elements": actions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
|
2023-08-11 00:46:41 -07:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-09-07 07:42:46 -07:00
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
2024-03-14 06:21:15 -07:00
|
|
|
const okMsg = "Sent Successfully.";
|
2023-05-23 08:29:18 -07:00
|
|
|
|
2023-06-01 06:23:13 -07:00
|
|
|
if (notification.slackchannelnotify) {
|
2023-05-23 08:29:18 -07:00
|
|
|
msg += " <!channel>";
|
|
|
|
}
|
2023-04-17 10:49:15 -07:00
|
|
|
|
2021-09-07 07:42:46 -07:00
|
|
|
try {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let data = {
|
2023-05-23 08:29:18 -07:00
|
|
|
"text": msg,
|
2021-09-07 07:42:46 -07:00
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2021-10-07 02:39:58 -07:00
|
|
|
};
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 07:42:46 -07:00
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
|
2024-10-08 16:43:44 -07:00
|
|
|
const baseURL = await setting("primaryBaseURL");
|
2024-04-01 17:43:54 -07:00
|
|
|
|
|
|
|
const title = "Uptime Kuma Alert";
|
2021-09-07 07:42:46 -07:00
|
|
|
let data = {
|
|
|
|
"channel": notification.slackchannel,
|
|
|
|
"username": notification.slackusername,
|
|
|
|
"icon_emoji": notification.slackiconemo,
|
2024-10-08 03:09:58 -07:00
|
|
|
"attachments": [],
|
|
|
|
};
|
|
|
|
|
|
|
|
if (notification.slackrichmessage) {
|
|
|
|
data.attachments.push(
|
2021-09-07 07:42:46 -07:00
|
|
|
{
|
2023-01-01 23:01:50 -08:00
|
|
|
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
|
2024-04-01 17:43:54 -07:00
|
|
|
"blocks": Slack.buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg),
|
2023-01-01 23:01:50 -08:00
|
|
|
}
|
2024-10-08 03:09:58 -07:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
data.text = `${title}\n${msg}`;
|
|
|
|
}
|
2021-10-07 02:39:58 -07:00
|
|
|
|
|
|
|
if (notification.slackbutton) {
|
|
|
|
await Slack.deprecateURL(notification.slackbutton);
|
|
|
|
}
|
|
|
|
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
2021-09-07 07:42:46 -07:00
|
|
|
return okMsg;
|
|
|
|
} catch (error) {
|
2021-10-07 02:39:58 -07:00
|
|
|
this.throwGeneralAxiosError(error);
|
2021-09-07 07:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Slack;
|