|
| 1 | +/** |
| 2 | + * Fire-and-forget Discord webhook notifications for internal monitoring. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * const { notify } = require('../lib/notify'); |
| 6 | + * notify('agent_created', { agent_id: 'agt_xxx', name: 'My Agent' }); |
| 7 | + * |
| 8 | + * Env vars (optional — if not set, the notification is silently skipped): |
| 9 | + * DISCORD_WEBHOOK_AGENT_CREATED |
| 10 | + * DISCORD_WEBHOOK_EMAIL_SENT |
| 11 | + * DISCORD_WEBHOOK_ERROR |
| 12 | + */ |
| 13 | + |
| 14 | +const CHANNELS = { |
| 15 | + agent_created: 'DISCORD_WEBHOOK_AGENT_CREATED', |
| 16 | + email_sent: 'DISCORD_WEBHOOK_EMAIL_SENT', |
| 17 | + error: 'DISCORD_WEBHOOK_ERROR', |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Post a notification to Discord. Never blocks, never throws. |
| 22 | + * |
| 23 | + * @param {'agent_created'|'email_sent'|'error'} event |
| 24 | + * @param {object} data — included in the Discord message |
| 25 | + */ |
| 26 | +function notify(event, data = {}) { |
| 27 | + const envKey = CHANNELS[event]; |
| 28 | + if (!envKey) return; |
| 29 | + |
| 30 | + const url = process.env[envKey]; |
| 31 | + if (!url) return; |
| 32 | + |
| 33 | + const lines = Object.entries(data) |
| 34 | + .map(([k, v]) => `**${k}:** ${v}`) |
| 35 | + .join('\n'); |
| 36 | + |
| 37 | + const content = `[${event}] ${new Date().toISOString()}\n${lines}`; |
| 38 | + |
| 39 | + // Fire and forget — no await, no .then() |
| 40 | + fetch(url, { |
| 41 | + method: 'POST', |
| 42 | + headers: { 'Content-Type': 'application/json' }, |
| 43 | + body: JSON.stringify({ content: content.slice(0, 2000) }), |
| 44 | + }).catch(() => {}); // swallow silently |
| 45 | +} |
| 46 | + |
| 47 | +module.exports = { notify }; |
0 commit comments