Store bot token and chat id in their own respective fields.

This commit is contained in:
Skorpios committed 2025-02-02 23:50:09 -08:00
1 parent d3fb41acfe
commit 8f9406261f
2 files changed
+14 -6

No files matched your search

+10
View File
@@ -10,6 +10,15 @@ const NotificationSchema = mongoose.Schema(
type: String, type: String,
enum: ["email", "sms", "discord", "slack", "telegram"], enum: ["email", "sms", "discord", "slack", "telegram"],
}, },
webhookUrl: {
type: String,
},
botToken: {
type: String,
},
chatId: {
type: String,
},
address: { address: {
type: String, type: String,
}, },
@@ -76,4 +85,5 @@ NotificationSchema.pre("findOneAndUpdate", function (next) {
} }
next(); next();
}); });
export default mongoose.model("Notification", NotificationSchema); export default mongoose.model("Notification", NotificationSchema);
+4 -6
View File
@@ -18,17 +18,16 @@ class NotificationService {
this.networkService = networkService; this.networkService = networkService;
} }
async sendWebhookNotification(networkResponse, address, platform) { async sendWebhookNotification(networkResponse, address, platform, botToken, chatId) {
const { monitor, status } = networkResponse; const { monitor, status } = networkResponse;
let message; let message;
let url = address; let url = address;
if (platform === 'slack') { if (platform === 'slack') {
message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` }; message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
} else if (platform === 'discord') { } else if (platform === 'discord') {
message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` }; message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
} else if (platform === 'telegram') { } else if (platform === 'telegram') {
const [botToken, chatId] = address.split('|').map(part => part?.trim());
if (!botToken || !chatId) { if (!botToken || !chatId) {
return false; return false;
} }
@@ -38,7 +37,7 @@ class NotificationService {
}; };
url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`; url = `${TELEGRAM_API_BASE_URL}${botToken}/sendMessage`;
} }
try { try {
const response = await this.networkService.requestWebhook(platform, url, message); const response = await this.networkService.requestWebhook(platform, url, message);
return response.status; return response.status;
@@ -56,9 +55,8 @@ class NotificationService {
return false; return false;
} }
} }
/** /**
* Sends an email notification for hardware infrastructure alerts * Sends an email notification for hardware infrastructure alerts
* *