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