mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-02-12 20:39:28 -06:00
Add Slack webhook integration.
This commit is contained in:
@@ -8,7 +8,7 @@ const NotificationSchema = mongoose.Schema(
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: ["email", "sms", "discord"],
|
||||
enum: ["email", "sms", "discord", "slack"],
|
||||
},
|
||||
address: {
|
||||
type: String,
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user