Refactor webhook integrations functions.

This commit is contained in:
Skorpios committed 2025-01-26 14:11:36 -08:00
1 parent d83773abd1
commit 84fe58b361
1 file changed
+20 -52
+20 -52
View File
@@ -17,77 +17,45 @@ class NotificationService {
this.logger = logger; this.logger = logger;
} }
async sendDiscordNotification(networkResponse, webhookUrl) { async sendWebhookNotification(networkResponse, address, platform) {
const { monitor, status } = networkResponse; const { monitor, status } = networkResponse;
const message = { let message;
content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` let url = address;
};
try { if (platform === 'slack') {
await axios.post(webhookUrl, message); message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
return true; } else if (platform === 'discord') {
} catch (error) { message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
this.logger.error({ } else if (platform === 'telegram') {
message: error.message, const [botToken, chatId] = address.split('|').map(part => part?.trim());
service: this.SERVICE_NAME, if (!botToken || !chatId) {
method: "sendDiscordNotification", console.error('Invalid Telegram address format');
stack: error.stack,
});
return false; return false;
} }
} message = {
chat_id: chatId,
async sendSlackNotification(networkResponse, webhookUrl) {
const { monitor, status } = networkResponse;
const message = {
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`
}; };
url = `https://api.telegram.org/bot${botToken}/sendMessage`;
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;
} }
}
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 { try {
await axios.post(url, message, { await axios.post(url, message, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json'
}, }
}); });
return true; return true;
} catch (error) { } catch (error) {
console.error(`Error sending ${platform} notification:`, error.toJSON());
console.error(`URL: ${url}`);
console.error(`Message:`, message);
return false; return false;
} }
} }
/** /**
* Sends an email notification for hardware infrastructure alerts * Sends an email notification for hardware infrastructure alerts
* *