mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-02-26 05:25:56 +00:00
[slack] implemented slack bot mode, updating of slack messages
This commit is contained in:
parent
eca90a2b00
commit
a95da7dc54
2 changed files with 190 additions and 42 deletions
|
@ -1,7 +1,8 @@
|
||||||
const NotificationProvider = require("./notification-provider");
|
const NotificationProvider = require("./notification-provider");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const { setSettings, setting } = require("../util-server");
|
const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util");
|
||||||
const { getMonitorRelativeURL, UP } = require("../../src/util");
|
const { Settings } = require("../settings");
|
||||||
|
const { log } = require("../../src/util");
|
||||||
|
|
||||||
class Slack extends NotificationProvider {
|
class Slack extends NotificationProvider {
|
||||||
name = "slack";
|
name = "slack";
|
||||||
|
@ -14,13 +15,11 @@ class Slack extends NotificationProvider {
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
static async deprecateURL(url) {
|
static async deprecateURL(url) {
|
||||||
let currentPrimaryBaseURL = await setting("primaryBaseURL");
|
let currentPrimaryBaseURL = await Settings.get("primaryBaseURL");
|
||||||
|
|
||||||
if (!currentPrimaryBaseURL) {
|
if (!currentPrimaryBaseURL) {
|
||||||
console.log("Move the url to be the primary base URL");
|
console.log("Move the url to be the primary base URL");
|
||||||
await setSettings("general", {
|
await Settings.set("primaryBaseURL", url, "general");
|
||||||
primaryBaseURL: url,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
console.log("Already there, no need to move the primary base URL");
|
console.log("Already there, no need to move the primary base URL");
|
||||||
}
|
}
|
||||||
|
@ -114,6 +113,167 @@ class Slack extends NotificationProvider {
|
||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the message object to send to Slack
|
||||||
|
* @param {object} heartbeatJSON The heartbeat bean
|
||||||
|
* @param {object} monitorJSON The monitor bean
|
||||||
|
* @param {object} notification The notification config
|
||||||
|
* @param {string} title The message title
|
||||||
|
* @param {string} msg The textual message
|
||||||
|
* @returns {Promise<object>} The message object
|
||||||
|
*/
|
||||||
|
static async buildMessage(heartbeatJSON, monitorJSON, notification, title, msg) {
|
||||||
|
|
||||||
|
// check if the notification provider is being tested
|
||||||
|
if (heartbeatJSON == null) {
|
||||||
|
return {
|
||||||
|
"text": msg,
|
||||||
|
"channel": notification.slackchannel,
|
||||||
|
"username": notification.slackusername,
|
||||||
|
"icon_emoji": notification.slackiconemo,
|
||||||
|
"attachments": [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseURL = await Settings.get("primaryBaseURL");
|
||||||
|
|
||||||
|
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}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static ENDPOINTS = {
|
||||||
|
postMessage: "https://slack.com/api/chat.postMessage",
|
||||||
|
getPermalink: "https://slack.com/api/chat.getPermalink",
|
||||||
|
update: "https://slack.com/api/chat.update",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Keeps track of open alerts in order to update/close them
|
||||||
|
static openAlerts = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delivers the message object to slack, through the chosen method
|
||||||
|
* @param {object} options The slack configuration
|
||||||
|
* @param {object} heartbeatJSON The heartbeat bean
|
||||||
|
* @param {object} message The message object to send to Slack
|
||||||
|
* @returns {Promise<T|AxiosResponse<any>>} The response from axios
|
||||||
|
*/
|
||||||
|
static async deliverMessage(options, heartbeatJSON, message) {
|
||||||
|
|
||||||
|
let response = null;
|
||||||
|
switch (options.mode) {
|
||||||
|
case "app":
|
||||||
|
response = Slack.deliverMessageViaAppApi(options, heartbeatJSON, message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "webhook":
|
||||||
|
default:
|
||||||
|
response = axios.post(options.slackwebhookURL, message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Track an open alert for a specific monitor
|
||||||
|
* @param {string} monitorId The monitor id
|
||||||
|
* @param {object} data The object representing the message
|
||||||
|
*/
|
||||||
|
static trackAlert(monitorId, data) {
|
||||||
|
Slack.openAlerts[monitorId] = Slack.openAlerts[monitorId] || [];
|
||||||
|
|
||||||
|
Slack.openAlerts[monitorId].push(data);
|
||||||
|
|
||||||
|
log.debug("notification.slack", `Monitor ${monitorId} now has ${Slack.openAlerts[monitorId].length} open alerts`);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the open alerts for a specific monitor
|
||||||
|
* @param {string} monitorId The monitor id
|
||||||
|
*/
|
||||||
|
static clearAlerts(monitorId) {
|
||||||
|
Slack.openAlerts[monitorId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the alert(s) for the ongoing incident for a specific monitor
|
||||||
|
* @param {string} monitorId The monitor id
|
||||||
|
* @returns {Array<object>} all open alerts
|
||||||
|
*/
|
||||||
|
static getAlerts(monitorId) {
|
||||||
|
return Slack.openAlerts[monitorId] || [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delivers the message through the Slack App API
|
||||||
|
* @param {object} options Slack configuration
|
||||||
|
* @param {object} heartbeatJSON The heartbeat bean
|
||||||
|
* @param {object} message The message object to send
|
||||||
|
* @returns {Promise<object>} The axios response
|
||||||
|
*/
|
||||||
|
static async deliverMessageViaAppApi(options, heartbeatJSON, message) {
|
||||||
|
|
||||||
|
let response = null;
|
||||||
|
const token = options.botToken;
|
||||||
|
const monitorId = heartbeatJSON?.monitorId;
|
||||||
|
|
||||||
|
const axiosConfig = {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer " + token,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const existingAlerts = Slack.getAlerts(monitorId);
|
||||||
|
if (existingAlerts.length > 0 && heartbeatJSON?.status === UP) {
|
||||||
|
|
||||||
|
log.info("slack", `Updating ${existingAlerts.length} message(s)`);
|
||||||
|
|
||||||
|
//Update the messages in parallel
|
||||||
|
const responses = await Promise.all(existingAlerts.map(( { channel, ts } ) => {
|
||||||
|
message.channel = channel;
|
||||||
|
message.ts = ts;
|
||||||
|
return axios.post(Slack.ENDPOINTS.update, message, axiosConfig);
|
||||||
|
}));
|
||||||
|
|
||||||
|
//get the last response
|
||||||
|
response = responses.pop();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response = await axios.post(Slack.ENDPOINTS.postMessage, message, axiosConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.data.ok) {
|
||||||
|
|
||||||
|
if (heartbeatJSON?.status === DOWN) {
|
||||||
|
Slack.trackAlert(monitorId, response.data);
|
||||||
|
} else if (heartbeatJSON?.status === UP) {
|
||||||
|
Slack.clearAlerts(monitorId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
|
@ -125,43 +285,17 @@ class Slack extends NotificationProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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 setting("primaryBaseURL");
|
|
||||||
|
|
||||||
const title = "Uptime Kuma Alert";
|
const title = "Uptime Kuma Alert";
|
||||||
let data = {
|
|
||||||
"channel": notification.slackchannel,
|
|
||||||
"username": notification.slackusername,
|
|
||||||
"icon_emoji": notification.slackiconemo,
|
|
||||||
"attachments": [],
|
|
||||||
};
|
|
||||||
|
|
||||||
if (notification.slackrichmessage) {
|
const message = await Slack.buildMessage(heartbeatJSON, monitorJSON, notification, title, msg);
|
||||||
data.attachments.push(
|
|
||||||
{
|
|
||||||
"color": (heartbeatJSON["status"] === UP) ? "#2eb886" : "#e01e5a",
|
|
||||||
"blocks": Slack.buildBlocks(baseURL, monitorJSON, heartbeatJSON, title, msg),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
data.text = `${title}\n${msg}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//not sure what this does, I think it can be safely removed
|
||||||
if (notification.slackbutton) {
|
if (notification.slackbutton) {
|
||||||
await Slack.deprecateURL(notification.slackbutton);
|
await Slack.deprecateURL(notification.slackbutton);
|
||||||
}
|
}
|
||||||
|
|
||||||
await axios.post(notification.slackwebhookURL, data);
|
await Slack.deliverMessage(notification, heartbeatJSON, message);
|
||||||
|
|
||||||
return okMsg;
|
return okMsg;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.throwGeneralAxiosError(error);
|
this.throwGeneralAxiosError(error);
|
||||||
|
|
|
@ -1,11 +1,25 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="slack-webhook-url" class="form-label">{{ $t("Webhook URL") }}<span style="color: red;"><sup>*</sup></span></label>
|
<label for="slack-mode">API mode</label>
|
||||||
<input id="slack-webhook-url" v-model="$parent.notification.slackwebhookURL" type="text" class="form-control" required>
|
<select id="slack-mode" v-model="$parent.notification.mode" class="form-control">
|
||||||
<label for="slack-username" class="form-label">{{ $t("Username") }}</label>
|
<option value="webhook">Webhook</option>
|
||||||
<input id="slack-username" v-model="$parent.notification.slackusername" type="text" class="form-control">
|
<option value="app">App</option>
|
||||||
<label for="slack-iconemo" class="form-label">{{ $t("Icon Emoji") }}</label>
|
</select>
|
||||||
<input id="slack-iconemo" v-model="$parent.notification.slackiconemo" type="text" class="form-control">
|
|
||||||
|
<fieldset v-if="$parent.notification.mode == 'webhook'">
|
||||||
|
<label for="slack-webhook-url" class="form-label">{{ $t("Webhook URL") }}<span style="color: red;"><sup>*</sup></span></label>
|
||||||
|
<input id="slack-webhook-url" v-model="$parent.notification.slackwebhookURL" type="text" class="form-control" required>
|
||||||
|
<label for="slack-username" class="form-label">{{ $t("Username") }}</label>
|
||||||
|
<input id="slack-username" v-model="$parent.notification.slackusername" type="text" class="form-control">
|
||||||
|
<label for="slack-iconemo" class="form-label">{{ $t("Icon Emoji") }}</label>
|
||||||
|
<input id="slack-iconemo" v-model="$parent.notification.slackiconemo" type="text" class="form-control">
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<fieldset v-if="$parent.notification.mode == 'app'">
|
||||||
|
<label for="slack-token" class="form-label">{{ $t("Token") }}</label>
|
||||||
|
<input id="slack-token" v-model="$parent.notification.botToken" type="text" class="form-control">
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
<label for="slack-channel" class="form-label">{{ $t("Channel Name") }}</label>
|
<label for="slack-channel" class="form-label">{{ $t("Channel Name") }}</label>
|
||||||
<input id="slack-channel-name" v-model="$parent.notification.slackchannel" type="text" class="form-control">
|
<input id="slack-channel-name" v-model="$parent.notification.slackchannel" type="text" class="form-control">
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue