Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | /**
* Service Webhook Notifier — Envoie des notifications vers Discord, Slack, Telegram.
*
* Formate le message selon la plateforme et l'envoie via HTTP POST.
*
* @module services/webhookNotifier
*/
const { pool: db } = require('../db');
/**
* Formate un message pour Discord.
*/
function formatDiscord(event, data) {
const colors = {
server_alert: 0xff4444, server_recovery: 0x22c55e,
uptime_down: 0xff0000, uptime_up: 0x22c55e,
backup_error: 0xeab308, backup_success: 0x22c55e,
ssl_expiring: 0xf59e0b, ssl_expired: 0xef4444, ssl_recovery: 0x22c55e,
http_error: 0xdc2626,
};
const titles = {
server_alert: 'Alerte serveur', server_recovery: 'Serveur OK',
uptime_down: 'Site DOWN', uptime_up: 'Site UP',
backup_error: 'Backup echoue', backup_success: 'Backup reussi',
ssl_expiring: 'Certificat SSL bientot expire', ssl_expired: 'Certificat SSL expire', ssl_recovery: 'Certificat SSL renouvele',
http_error: 'Erreur HTTP',
};
return {
embeds: [{
title: titles[event] || event,
description: data.message,
color: colors[event] || 0x6366f1,
fields: data.fields || [],
timestamp: new Date().toISOString(),
footer: { text: 'IliaCloud' },
}],
};
}
/**
* Formate un message pour Slack.
*/
function formatSlack(event, data) {
const emojis = {
server_alert: ':warning:', server_recovery: ':white_check_mark:',
uptime_down: ':red_circle:', uptime_up: ':large_green_circle:',
backup_error: ':x:', backup_success: ':white_check_mark:',
ssl_expiring: ':warning:', ssl_expired: ':rotating_light:', ssl_recovery: ':white_check_mark:',
http_error: ':boom:',
};
return {
text: `${emojis[event] || ':bell:'} *${data.title || event}*\n${data.message}`,
};
}
/**
* Formate un message pour Telegram (via Bot API).
* L'URL du webhook doit être : https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<CHAT_ID>
*/
function formatTelegram(event, data) {
const emojis = {
server_alert: '⚠️', server_recovery: '✅',
uptime_down: '🔴', uptime_up: '🟢',
backup_error: '❌', backup_success: '✅',
ssl_expiring: '⚠️', ssl_expired: '🚨', ssl_recovery: '✅',
http_error: '💥',
};
return {
text: `${emojis[event] || '🔔'} *${data.title || event}*\n${data.message}`,
parse_mode: 'Markdown',
};
}
/**
* Formate un message custom (JSON brut).
*/
function formatCustom(event, data) {
return { event, ...data, timestamp: new Date().toISOString() };
}
const FORMATTERS = {
discord: formatDiscord,
slack: formatSlack,
telegram: formatTelegram,
custom: formatCustom,
};
/**
* Envoie une notification à tous les webhooks d'un utilisateur pour un événement donné.
*
* @param {string} userId
* @param {string} event — 'server_alert' | 'uptime_down' | 'uptime_up' | 'backup_error'
* @param {{ title?: string, message: string, fields?: Array }} data
*/
async function notify(userId, event, data) {
try {
const { rows: webhooks } = await db.query(
"SELECT * FROM webhooks WHERE user_id = $1 AND enabled = TRUE AND $2 = ANY(events)",
[userId, event],
);
for (const wh of webhooks) {
const formatter = FORMATTERS[wh.platform] || formatCustom;
const body = formatter(event, data);
try {
await fetch(wh.url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
} catch (err) {
console.error(`[webhook] Erreur envoi ${wh.platform} (${wh.label}):`, err.message);
}
}
} catch (err) {
console.error('[webhook] Erreur:', err.message);
}
}
module.exports = { notify };
|