Compare commits

...

8 commits

Author SHA1 Message Date
Dj Isaac
cee0b62d2f
Merge 64f528456d into 8a432ac937 2024-11-12 18:00:25 +00:00
Dj Isaac
64f528456d
Merge branch 'master' into dj/discord-service-url 2024-10-20 19:29:05 -05:00
Dj Isaac
cd69146f33
add discord buttons to payload 2024-10-20 19:28:47 -05:00
Dj Isaac
ecac2917a0
Merge branch 'master' into dj/discord-service-url 2024-10-09 20:36:06 -05:00
Dj Isaac
8b82520d3d
Conditionally add service url 2024-10-09 20:35:26 -05:00
Dj Isaac
af230ba5fb
Use msg rather than parse it out of the heartbeat
Co-authored-by: Frank Elsinga <frank@elsinga.de>
2024-10-09 20:02:11 -05:00
Dj Isaac
c28710cdc6
fix: remove service name/type from discord notifier 2024-10-09 20:02:07 -05:00
Dj Isaac
0d328e7c8e
fix: misspelling of 'address' (#5180) 2024-10-09 20:01:51 -05:00
3 changed files with 72 additions and 53 deletions

View file

@ -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,84 +39,89 @@ class Discord extends NotificationProvider {
return okMsg; return okMsg;
} }
const embedFields = [
{
name: "Service Name",
value: monitorJSON.name,
},
...(hasAddress ? [{
name: "Service URL",
value: address
}] : []),
{
name: `Time (${heartbeatJSON.timezone})`,
value: heartbeatJSON.localDateTime,
},
{
name: "Error",
value: msg,
},
];
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: [ fields: embedFields,
{
name: "Service Name",
value: monitorJSON["name"],
},
{
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
value: this.extractAddress(monitorJSON),
},
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
},
{
name: "Error",
value: heartbeatJSON["msg"] == null ? "N/A" : heartbeatJSON["msg"],
},
],
}], }],
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: [ fields: embedFields,
{
name: "Service Name",
value: monitorJSON["name"],
},
{
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
value: this.extractAddress(monitorJSON),
},
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
},
{
name: "Ping",
value: heartbeatJSON["ping"] == null ? "N/A" : heartbeatJSON["ping"] + " ms",
},
],
}], }],
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);
} }
} }

View file

@ -4,6 +4,12 @@
<input id="discord-webhook-url" v-model="$parent.notification.discordWebhookUrl" type="text" class="form-control" required autocomplete="false"> <input id="discord-webhook-url" v-model="$parent.notification.discordWebhookUrl" type="text" class="form-control" required autocomplete="false">
<div class="form-text"> <div class="form-text">
{{ $t("wayToGetDiscordURL") }} {{ $t("wayToGetDiscordURL") }}
<i18n-t keypath="wayToGetDiscordURLEnhanced">
<a
href="https://discord.com/developers/docs/resources/webhook#create-webhook"
target="_blank"
>{{ $t("withADiscordBot") }}</a>
</i18n-t>
</div> </div>
</div> </div>

View file

@ -531,6 +531,8 @@
"smtpBCC": "BCC", "smtpBCC": "BCC",
"Discord Webhook URL": "Discord Webhook URL", "Discord Webhook URL": "Discord Webhook URL",
"wayToGetDiscordURL": "You can get this by going to Server Settings -> Integrations -> View Webhooks -> New Webhook", "wayToGetDiscordURL": "You can get this by going to Server Settings -> Integrations -> View Webhooks -> New Webhook",
"wayToGetDiscordURLEnhanced": "Using the Discord API, you may also create the webhook {0} for button support.",
"withADiscordBot": "with a bot",
"Bot Display Name": "Bot Display Name", "Bot Display Name": "Bot Display Name",
"Prefix Custom Message": "Prefix Custom Message", "Prefix Custom Message": "Prefix Custom Message",
"Hello @everyone is...": "Hello {'@'}everyone is…", "Hello @everyone is...": "Hello {'@'}everyone is…",