mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-23 03:55:56 +00:00
Co-authored-by: Frank Elsinga <frank@elsinga.de> Closes: #4391 Squashed commit of the following: commit828d2a73d4
Merge:10f771cf
dd758903
Author: Frank Elsinga <frank@elsinga.de> Date: Fri Sep 13 22:51:25 2024 +0800 Merge branch 'master' into deprecations commit10f771cfc6
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 22:36:12 2024 +0100 formatting fixes commitd737b19e2f
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 21:27:30 2024 +0100 migrated all settings to use the `Settings` class commitc5e26e993e
Author: Frank Elsinga <frank@elsinga.de> Date: Thu Jan 18 21:04:44 2024 +0100 removed the deprecated logging functionality * fix(server/model/monitor): duplicate `Settings.set` for `tlsExpiryNotifyDays` * fix(eslint): minor linter complaints & a typo
171 lines
5.2 KiB
JavaScript
171 lines
5.2 KiB
JavaScript
const NotificationProvider = require("./notification-provider");
|
|
const axios = require("axios");
|
|
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
|
const { Settings } = require("../settings");
|
|
|
|
class Slack extends NotificationProvider {
|
|
name = "slack";
|
|
|
|
/**
|
|
* Deprecated property notification.slackbutton
|
|
* Set it as primary base url if this is not yet set.
|
|
* @deprecated
|
|
* @param {string} url The primary base URL to use
|
|
* @returns {Promise<void>}
|
|
*/
|
|
static async deprecateURL(url) {
|
|
let currentPrimaryBaseURL = await Settings.get("primaryBaseURL");
|
|
|
|
if (!currentPrimaryBaseURL) {
|
|
console.log("Move the url to be the primary base URL");
|
|
await Settings.set("primaryBaseURL", url, "general");
|
|
} else {
|
|
console.log("Already there, no need to move the primary base URL");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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),
|
|
});
|
|
|
|
}
|
|
|
|
const address = this.extractAddress(monitorJSON);
|
|
if (address) {
|
|
actions.push({
|
|
"type": "button",
|
|
"text": {
|
|
"type": "plain_text",
|
|
"text": "Visit site",
|
|
},
|
|
"value": "Site",
|
|
"url": address,
|
|
});
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
const okMsg = "Sent Successfully.";
|
|
|
|
if (notification.slackchannelnotify) {
|
|
msg += " <!channel>";
|
|
}
|
|
|
|
try {
|
|
if (heartbeatJSON == null) {
|
|
let data = {
|
|
"text": msg,
|
|
"channel": notification.slackchannel,
|
|
"username": notification.slackusername,
|
|
"icon_emoji": notification.slackiconemo,
|
|
};
|
|
await axios.post(notification.slackwebhookURL, data);
|
|
return okMsg;
|
|
}
|
|
|
|
const baseURL = await Settings.get("primaryBaseURL");
|
|
|
|
const title = "Uptime Kuma Alert";
|
|
let data = {
|
|
"channel": notification.slackchannel,
|
|
"username": notification.slackusername,
|
|
"icon_emoji": notification.slackiconemo,
|
|
"attachments": [],
|
|
};
|
|
|
|
if (notification.slackrichmessage) {
|
|
data.attachments.push(
|
|
{
|
|
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
|
|
"blocks": Slack.buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg),
|
|
}
|
|
);
|
|
} else {
|
|
data.text = `${title}\n${msg}`;
|
|
}
|
|
|
|
if (notification.slackbutton) {
|
|
await Slack.deprecateURL(notification.slackbutton);
|
|
}
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
|
return okMsg;
|
|
} catch (error) {
|
|
this.throwGeneralAxiosError(error);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
module.exports = Slack;
|