|
|
@ -1,6 +1,7 @@
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const NotificationProvider = require("./notification-provider");
|
|
|
|
const axios = require("axios");
|
|
|
|
const axios = require("axios");
|
|
|
|
const { DOWN, UP } = require("../../src/util");
|
|
|
|
const { Settings } = require("../settings");
|
|
|
|
|
|
|
|
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
|
|
|
|
|
|
|
|
|
|
|
|
class Discord extends NotificationProvider {
|
|
|
|
class Discord extends NotificationProvider {
|
|
|
|
name = "discord";
|
|
|
|
name = "discord";
|
|
|
@ -19,8 +20,13 @@ class Discord extends NotificationProvider {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If heartbeatJSON is null, assume we're testing.
|
|
|
|
// If heartbeatJSON is null, assume we're testing.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const baseURL = await Settings.get("primaryBaseURL");
|
|
|
|
|
|
|
|
const address = this.extractAddress(monitorJSON);
|
|
|
|
|
|
|
|
const hasAddress = address !== "" && address !== monitorJSON.hostname;
|
|
|
|
|
|
|
|
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
if (heartbeatJSON == null) {
|
|
|
|
let discordtestdata = {
|
|
|
|
const discordtestdata = {
|
|
|
|
username: discordDisplayName,
|
|
|
|
username: discordDisplayName,
|
|
|
|
content: msg,
|
|
|
|
content: msg,
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -33,20 +39,18 @@ class Discord extends NotificationProvider {
|
|
|
|
return okMsg;
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const address = this.extractAddress(monitorJSON);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const embedFields = [
|
|
|
|
const embedFields = [
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: "Service Name",
|
|
|
|
name: "Service Name",
|
|
|
|
value: monitorJSON["name"],
|
|
|
|
value: monitorJSON.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
...((address !== "" && address !== monitorJSON["hostname"]) ? [{
|
|
|
|
...(hasAddress ? [{
|
|
|
|
name: "Service URL",
|
|
|
|
name: "Service URL",
|
|
|
|
value: address
|
|
|
|
value: address
|
|
|
|
}] : []),
|
|
|
|
}] : []),
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: `Time (${heartbeatJSON["timezone"]})`,
|
|
|
|
name: `Time (${heartbeatJSON.timezone})`,
|
|
|
|
value: heartbeatJSON["localDateTime"],
|
|
|
|
value: heartbeatJSON.localDateTime,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
name: "Error",
|
|
|
@ -54,50 +58,70 @@ class Discord extends NotificationProvider {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const components = [
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
type: 1, // Action Row
|
|
|
|
|
|
|
|
components: [
|
|
|
|
|
|
|
|
baseURL && {
|
|
|
|
|
|
|
|
type: 2, // Button
|
|
|
|
|
|
|
|
style: 5, // Link Button,
|
|
|
|
|
|
|
|
label: "Visit Uptime Kuma",
|
|
|
|
|
|
|
|
url: baseURL + getMonitorRelativeURL(monitorJSON.id)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
hasAddress && {
|
|
|
|
|
|
|
|
type: 2, // Button
|
|
|
|
|
|
|
|
style: 5, // Link Button,
|
|
|
|
|
|
|
|
label: "Visit Service URL",
|
|
|
|
|
|
|
|
url: address
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
].filter(Boolean) // remove invalid data
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// If heartbeatJSON is not null, we go into the normal alerting loop.
|
|
|
|
// If heartbeatJSON is not null, we go into the normal alerting loop.
|
|
|
|
if (heartbeatJSON["status"] === DOWN) {
|
|
|
|
if (heartbeatJSON.status === DOWN) {
|
|
|
|
let discorddowndata = {
|
|
|
|
const discorddowndata = {
|
|
|
|
username: discordDisplayName,
|
|
|
|
username: discordDisplayName,
|
|
|
|
|
|
|
|
content: notification.discordPrefixMessage || "",
|
|
|
|
embeds: [{
|
|
|
|
embeds: [{
|
|
|
|
title: "❌ Your service " + monitorJSON["name"] + " went down. ❌",
|
|
|
|
title: `❌ Your service ${monitorJSON.name} went down. ❌`,
|
|
|
|
color: 16711680,
|
|
|
|
color: 16711680,
|
|
|
|
timestamp: heartbeatJSON["time"],
|
|
|
|
timestamp: heartbeatJSON.time,
|
|
|
|
fields: embedFields,
|
|
|
|
fields: embedFields,
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
|
|
|
|
components: components,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (notification.discordChannelType === "createNewForumPost") {
|
|
|
|
if (notification.discordChannelType === "createNewForumPost") {
|
|
|
|
discorddowndata.thread_name = notification.postName;
|
|
|
|
discorddowndata.thread_name = notification.postName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (notification.discordPrefixMessage) {
|
|
|
|
|
|
|
|
discorddowndata.content = notification.discordPrefixMessage;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await axios.post(webhookUrl.toString(), discorddowndata);
|
|
|
|
await axios.post(webhookUrl.toString(), discorddowndata);
|
|
|
|
return okMsg;
|
|
|
|
return okMsg;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else if (heartbeatJSON["status"] === UP) {
|
|
|
|
if (heartbeatJSON.status === UP) {
|
|
|
|
let discordupdata = {
|
|
|
|
const discordupdata = {
|
|
|
|
username: discordDisplayName,
|
|
|
|
username: discordDisplayName,
|
|
|
|
|
|
|
|
content: notification.discordPrefixMessage || "",
|
|
|
|
embeds: [{
|
|
|
|
embeds: [{
|
|
|
|
title: "✅ Your service " + monitorJSON["name"] + " is up! ✅",
|
|
|
|
title: `✅ Your service ${monitorJSON.name} is up! ✅`,
|
|
|
|
color: 65280,
|
|
|
|
color: 65280,
|
|
|
|
timestamp: heartbeatJSON["time"],
|
|
|
|
timestamp: heartbeatJSON.time,
|
|
|
|
fields: embedFields,
|
|
|
|
fields: embedFields,
|
|
|
|
}],
|
|
|
|
}],
|
|
|
|
|
|
|
|
components: components,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (notification.discordChannelType === "createNewForumPost") {
|
|
|
|
if (notification.discordChannelType === "createNewForumPost") {
|
|
|
|
discordupdata.thread_name = notification.postName;
|
|
|
|
discordupdata.thread_name = notification.postName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (notification.discordPrefixMessage) {
|
|
|
|
|
|
|
|
discordupdata.content = notification.discordPrefixMessage;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await axios.post(webhookUrl.toString(), discordupdata);
|
|
|
|
await axios.post(webhookUrl.toString(), discordupdata);
|
|
|
|
return okMsg;
|
|
|
|
return okMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.log(error);
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
this.throwGeneralAxiosError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|