Add Slack webhook integration.

This commit is contained in:
Skorpios
2025-01-22 18:46:50 -08:00
parent 28f5d355a7
commit 172597e768
2 changed files with 38 additions and 15 deletions

View File

@@ -8,7 +8,7 @@ const NotificationSchema = mongoose.Schema(
},
type: {
type: String,
enum: ["email", "sms", "discord"],
enum: ["email", "sms", "discord", "slack"],
},
address: {
type: String,

View File

@@ -37,6 +37,26 @@ class NotificationService {
}
}
async sendSlackNotification(networkResponse, webhookUrl) {
const { monitor, status } = networkResponse;
const message = {
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`
};
try {
await axios.post(webhookUrl, message);
return true;
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "sendSlackNotification",
stack: error.stack,
});
return false;
}
}
/**
* Sends an email notification for hardware infrastructure alerts
*
@@ -77,22 +97,25 @@ class NotificationService {
}
async handleStatusNotifications(networkResponse) {
try {
//If status hasn't changed, we're done
if (networkResponse.statusChanged === false) return false;
try {
// If status hasn't changed, we're done
if (networkResponse.statusChanged === false) return false;
// if prevStatus is undefined, monitor is resuming, we're done
if (networkResponse.prevStatus === undefined) return false;
const notifications = await this.db.getNotificationsByMonitorId(
networkResponse.monitorId
);
// if prevStatus is undefined, monitor is resuming, we're done
if (networkResponse.prevStatus === undefined) return false;
for (const notification of notifications) {
if (notification.type === "email") {
this.sendEmail(networkResponse, notification.address);
}
// Handle other types of notifications here
}
const notifications = await this.db.getNotificationsByMonitorId(networkResponse.monitorId);
for (const notification of notifications) {
if (notification.type === "email") {
this.sendEmail(networkResponse, notification.address);
} else if (notification.type === "discord") {
this.sendDiscordNotification(networkResponse, notification.address);
} else if (notification.type === "slack") {
this.sendSlackNotification(networkResponse, notification.address);
}
// Handle other types of notifications here
}
return true;
} catch (error) {
this.logger.warn({