uptime-kuma/server/notification-providers/rocket-chat.js

67 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-09-07 14:42:46 +00:00
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const Slack = require("./slack");
const { setting } = require("../util-server");
2022-04-13 16:30:32 +00:00
const { getMonitorRelativeURL, DOWN } = require("../../src/util");
2021-09-07 14:42:46 +00:00
class RocketChat extends NotificationProvider {
name = "rocket.chat";
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
2021-10-05 19:40:59 +00:00
let okMsg = "Sent Successfully.";
2021-09-07 14:42:46 +00:00
try {
if (heartbeatJSON == null) {
let data = {
"text": msg,
2021-09-07 14:42:46 +00:00
"channel": notification.rocketchannel,
"username": notification.rocketusername,
"icon_emoji": notification.rocketiconemo,
};
await axios.post(notification.rocketwebhookURL, data);
2021-09-07 14:42:46 +00:00
return okMsg;
}
const time = heartbeatJSON["time"];
2021-09-07 14:42:46 +00:00
let data = {
"text": "Uptime Kuma Alert",
"channel": notification.rocketchannel,
"username": notification.rocketusername,
"icon_emoji": notification.rocketiconemo,
"attachments": [
{
"title": "Uptime Kuma Alert *Time (UTC)*\n" + time,
"text": "*Message*\n" + msg,
}
]
};
// Color
if (heartbeatJSON.status === DOWN) {
data.attachments[0].color = "#ff0000";
} else {
data.attachments[0].color = "#32cd32";
}
if (notification.rocketbutton) {
await Slack.deprecateURL(notification.rocketbutton);
2021-09-07 14:42:46 +00:00
}
const baseURL = await setting("primaryBaseURL");
if (baseURL) {
data.attachments[0].title_link = baseURL + getMonitorRelativeURL(monitorJSON.id);
}
await axios.post(notification.rocketwebhookURL, data);
2021-09-07 14:42:46 +00:00
return okMsg;
} catch (error) {
this.throwGeneralAxiosError(error);
2021-09-07 14:42:46 +00:00
}
}
}
module.exports = RocketChat;