Add discord support for SLOW/NOMINAL notification cards

This commit is contained in:
Stephen Papierski 2023-11-28 10:50:17 -07:00
parent bdfe95a0f1
commit 7d5297df9d
No known key found for this signature in database

View file

@ -1,10 +1,11 @@
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");
const { DOWN, UP, SLOW, NOMINAL } = require("../../src/util");
class Discord extends NotificationProvider {
name = "discord";
supportSlowNotifications = true;
/**
* @inheritdoc
@ -115,12 +116,93 @@ class Discord extends NotificationProvider {
await axios.post(notification.discordWebhookUrl, discordupdata);
return okMsg;
} else if (heartbeatJSON["status"] === SLOW) {
let discordslowdata = {
username: discordDisplayName,
embeds: [{
title: "🐌 Your service " + monitorJSON["name"] + " responded slow. 🐌",
color: 16761095,
timestamp: heartbeatJSON["time"],
fields: [
{
name: "Service Name",
value: monitorJSON["name"],
},
{
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
value: monitorJSON["type"] === "push" ? "Heartbeat" : address,
},
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
},
{
name: "Ping",
value: heartbeatJSON["calculatedResponse"],
},
{
name: "Threshold",
value: heartbeatJSON["calculatedThreshold"],
},
],
}],
};
if (notification.discordPrefixMessage) {
discordslowdata.content = notification.discordPrefixMessage;
}
await axios.post(notification.discordWebhookUrl, discordslowdata);
return okMsg;
} else if (heartbeatJSON["status"] === NOMINAL) {
let discordnominaldata = {
username: discordDisplayName,
embeds: [{
title: "🚀 Your service " + monitorJSON["name"] + " is responding normally! 🚀",
color: 65280,
timestamp: heartbeatJSON["time"],
fields: [
{
name: "Service Name",
value: monitorJSON["name"],
},
{
name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL",
value: monitorJSON["type"] === "push" ? "Heartbeat" : address,
},
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
},
{
name: "Ping",
value: heartbeatJSON["calculatedResponse"],
},
{
name: "Threshold",
value: heartbeatJSON["calculatedThreshold"],
},
{
name: "Slow For",
value: heartbeatJSON["slowFor"],
},
],
}],
};
if (notification.discordPrefixMessage) {
discordnominaldata.content = notification.discordPrefixMessage;
}
await axios.post(notification.discordWebhookUrl, discordnominaldata);
return okMsg;
} else {
this.throwGeneralAxiosError("Not sure why we're here");
}
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
}
module.exports = Discord;