Added telegram notifications
This commit is contained in:
1 parent
172597e768
commit
d3810db6cb
2 files changed
+48
-17
No files matched your search
@@ -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,
|
||||||
|
|||||||
@@ -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;
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
for (const notification of notifications) {
|
// Handle other types of notifications here
|
||||||
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;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
|
|||||||
Reference in new issue
Block a user