Tutoriales
Bot de Notificaciones

Building a Notification Bot

Learn how to consume AuditorIA's Server-Sent Events (SSE) to forward task updates to Slack, Discord, or any other platform.

Prerequisites

  • A running instance of AuditorIA.
  • Node.js installed on your machine.
  • A Webhook URL (e.g., from Slack Incoming Webhooks).

Step 1: Subscribe to the SSE Endpoint

We will use the eventsource library to listen for updates from http://localhost:8000/notifications/events.

const EventSource = require('eventsource');
const axios = require('axios');
 
const API_URL = 'http://localhost:8000/notifications/events';
const SLACK_WEBHOOK = 'YOUR_SLACK_WEBHOOK_URL';
 
const eventSource = new EventSource(API_URL);
 
console.log('Listening for AuditorIA events...');
 
eventSource.onmessage = (event) => {
  const notification = JSON.parse(event.data);
  forwardToSlack(notification);
};
 
eventSource.onerror = (err) => {
  console.error('Connection error:', err);
};

Step 2: Forward to Webhook

Implement the logic to send the data to your chat app.

async function forwardToSlack(notification) {
  if (!notification.text) return;
 
  const payload = {
    text: `📢 **AuditorIA Update**\n${notification.text}`
  };
 
  try {
    await axios.post(SLACK_WEBHOOK, payload);
    console.log('Notification sent to Slack!');
  } catch (error) {
    console.error('Failed to send webhook', error);
  }
}

Step 3: Run the Bot

Save the file as bot.js and run it:

node bot.js

This script can be easily adapted to run as a Cloud Function or a sidecar container in your Kubernetes cluster.