Add Slack webhook integration.

This commit is contained in:
Skorpios committed 2025-01-22 18:46:50 -08:00
1 parent 28f5d355a7
commit 172597e768
2 files changed
+38 -15

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ const NotificationSchema = mongoose.Schema(
}, },
type: { type: {
type: String, type: String,
enum: ["email", "sms", "discord"], enum: ["email", "sms", "discord", "slack"],
}, },
address: { address: {
type: String, type: String,
+37 -14
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 * Sends an email notification for hardware infrastructure alerts
* *
@@ -77,22 +97,25 @@ class NotificationService {
} }
async handleStatusNotifications(networkResponse) { async handleStatusNotifications(networkResponse) {
try { try {
//If status hasn't changed, we're done // If status hasn't changed, we're done
if (networkResponse.statusChanged === false) return false; if (networkResponse.statusChanged === false) return false;
// if prevStatus is undefined, monitor is resuming, we're done // if prevStatus is undefined, monitor is resuming, we're done
if (networkResponse.prevStatus === undefined) return false; if (networkResponse.prevStatus === undefined) return false;
const notifications = await this.db.getNotificationsByMonitorId(
networkResponse.monitorId
);
for (const notification of notifications) { const notifications = await this.db.getNotificationsByMonitorId(networkResponse.monitorId);
if (notification.type === "email") {
this.sendEmail(networkResponse, notification.address); for (const notification of notifications) {
} if (notification.type === "email") {
// Handle other types of notifications here 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; return true;
} catch (error) { } catch (error) {
this.logger.warn({ this.logger.warn({