mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-12-20 21:24:44 -05:00
a8af2a418e
* [empty commit] pull request for slack notification * Add attachments block for slack notification * chore: update action button in new attachment block * chore: loop in attachments to push blocks * chore: missing semicolon Co-authored-by: pruekanw <arniwatt.c@linecorp.com>
110 lines
3.9 KiB
JavaScript
110 lines
3.9 KiB
JavaScript
const NotificationProvider = require("./notification-provider");
|
|
const axios = require("axios");
|
|
const { setSettings, setting } = require("../util-server");
|
|
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
|
|
let okMsg = "Sent Successfully.";
|
|
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 time = heartbeatJSON["time"];
|
|
const textMsg = "Uptime Kuma Alert";
|
|
let data = {
|
|
"text": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg,
|
|
"channel": notification.slackchannel,
|
|
"username": notification.slackusername,
|
|
"icon_emoji": notification.slackiconemo,
|
|
"attachments": [
|
|
{
|
|
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
|
|
"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,
|
|
}],
|
|
}
|
|
],
|
|
}
|
|
]
|
|
};
|
|
|
|
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),
|
|
}],
|
|
});
|
|
});
|
|
}
|
|
|
|
await axios.post(notification.slackwebhookURL, data);
|
|
return okMsg;
|
|
} catch (error) {
|
|
this.throwGeneralAxiosError(error);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
module.exports = Slack;
|