uptime-kuma/server/notification-providers/discord.js

132 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-09-07 14:42:46 +00:00
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
2024-10-21 00:28:40 +00:00
const { Settings } = require("../settings");
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
2021-09-07 14:42:46 +00:00
class Discord extends NotificationProvider {
name = "discord";
/**
* @inheritdoc
*/
2021-09-07 14:42:46 +00:00
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
2021-09-07 14:42:46 +00:00
try {
const discordDisplayName = notification.discordUsername || "Uptime Kuma";
const webhookUrl = new URL(notification.discordWebhookUrl);
if (notification.discordChannelType === "postToThread") {
webhookUrl.searchParams.append("thread_id", notification.threadId);
}
2021-09-07 14:42:46 +00:00
// If heartbeatJSON is null, assume we're testing.
2024-10-21 00:28:40 +00:00
const baseURL = await Settings.get("primaryBaseURL");
const address = this.extractAddress(monitorJSON);
const hasAddress = address !== "" && address !== monitorJSON.hostname;
2021-09-07 14:42:46 +00:00
if (heartbeatJSON == null) {
2024-10-21 00:28:40 +00:00
const discordtestdata = {
2021-09-07 14:42:46 +00:00
username: discordDisplayName,
content: msg,
2022-04-13 16:30:32 +00:00
};
if (notification.discordChannelType === "createNewForumPost") {
discordtestdata.thread_name = notification.postName;
}
await axios.post(webhookUrl.toString(), discordtestdata);
2021-09-07 14:42:46 +00:00
return okMsg;
}
const embedFields = [
{
name: "Service Name",
2024-10-21 00:28:40 +00:00
value: monitorJSON.name,
},
2024-10-21 00:28:40 +00:00
...(hasAddress ? [{
2024-10-10 01:35:26 +00:00
name: "Service URL",
value: address
}] : []),
{
2024-10-21 00:28:40 +00:00
name: `Time (${heartbeatJSON.timezone})`,
value: heartbeatJSON.localDateTime,
},
{
name: "Error",
value: msg,
},
];
2024-10-21 00:28:40 +00:00
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
}
];
2021-09-07 14:42:46 +00:00
// If heartbeatJSON is not null, we go into the normal alerting loop.
2024-10-21 00:28:40 +00:00
if (heartbeatJSON.status === DOWN) {
const discorddowndata = {
2021-09-07 14:42:46 +00:00
username: discordDisplayName,
2024-10-21 00:28:40 +00:00
content: notification.discordPrefixMessage || "",
2021-09-07 14:42:46 +00:00
embeds: [{
2024-10-21 00:28:40 +00:00
title: `❌ Your service ${monitorJSON.name} went down. ❌`,
2021-09-07 14:42:46 +00:00
color: 16711680,
2024-10-21 00:28:40 +00:00
timestamp: heartbeatJSON.time,
fields: embedFields,
2021-09-07 14:42:46 +00:00
}],
2024-10-21 00:28:40 +00:00
components: components,
2022-04-13 16:30:32 +00:00
};
2024-10-21 00:28:40 +00:00
if (notification.discordChannelType === "createNewForumPost") {
discorddowndata.thread_name = notification.postName;
}
await axios.post(webhookUrl.toString(), discorddowndata);
2021-09-07 14:42:46 +00:00
return okMsg;
2024-10-21 00:28:40 +00:00
}
2021-09-07 14:42:46 +00:00
2024-10-21 00:28:40 +00:00
if (heartbeatJSON.status === UP) {
const discordupdata = {
2021-09-07 14:42:46 +00:00
username: discordDisplayName,
2024-10-21 00:28:40 +00:00
content: notification.discordPrefixMessage || "",
2021-09-07 14:42:46 +00:00
embeds: [{
2024-10-21 00:28:40 +00:00
title: `✅ Your service ${monitorJSON.name} is up! ✅`,
2021-09-07 14:42:46 +00:00
color: 65280,
2024-10-21 00:28:40 +00:00
timestamp: heartbeatJSON.time,
fields: embedFields,
2021-09-07 14:42:46 +00:00
}],
2024-10-21 00:28:40 +00:00
components: components,
2022-04-13 16:30:32 +00:00
};
if (notification.discordChannelType === "createNewForumPost") {
discordupdata.thread_name = notification.postName;
}
await axios.post(webhookUrl.toString(), discordupdata);
2021-09-07 14:42:46 +00:00
return okMsg;
}
} catch (error) {
2024-10-21 00:28:40 +00:00
console.log(error);
2022-04-13 16:30:32 +00:00
this.throwGeneralAxiosError(error);
2021-09-07 14:42:46 +00:00
}
}
}
module.exports = Discord;