Added telegram notifications

This commit is contained in:
Skorpios committed 2025-01-25 16:13:16 -08:00
1 parent 172597e768
commit d3810db6cb
2 files changed
+51 -20

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@ const NotificationSchema = mongoose.Schema(
},
type: {
type: String,
enum: ["email", "sms", "discord", "slack"],
enum: ["email", "sms", "discord", "slack", "telegram"],
},
address: {
type: String,
+50 -19
View File
@@ -57,6 +57,37 @@ class NotificationService {
}
}
async sendTelegramNotification(networkResponse, address) {
const { monitor, status } = networkResponse;
const [botToken, chatId] = address.split('|').map(part => part?.trim());
if (!botToken || !chatId) {
return false;
}
const message = {
chat_id: chatId,
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`,
};
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
try {
await axios.post(url, message, {
headers: {
'Content-Type': 'application/json',
},
});
return true;
} catch (error) {
return false;
}
}
/**
* Sends an email notification for hardware infrastructure alerts
*
@@ -97,25 +128,25 @@ class NotificationService {
}
async handleStatusNotifications(networkResponse) {
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);
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
}
try {
if (networkResponse.statusChanged === false) return false;
if (networkResponse.prevStatus === undefined) return false;
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);
} else if (notification.type === "telegram") {
this.sendTelegramNotification(networkResponse, notification.address);
}
// Handle other types of notifications here
}
return true;
} catch (error) {
this.logger.warn({