Create a bot
Open @BotFather
Start a conversation with @BotFather in Telegram and send
/newbot.Name your bot
Follow the prompts to choose a name and username for the bot. BotFather replies with your
BOT_TOKEN — a string in the format <numeric_id>:<alphanumeric_hash>.Configuration
Opentelegram_alert.py and set the two constants at the top of the file:
telegram_alert.py
BOT_TOKEN— the credential that identifies your bot to the Telegram API.CHAT_IDS— a list of integers. Add multiple IDs to notify several administrators or groups with every alert.
How enviar_alerta works
enviar_alerta(mensaje) iterates over every ID in CHAT_IDS and sends an HTTP POST to the Telegram Bot API:
telegram_alert.py
application/x-www-form-urlencoded (the data= parameter), which is the format the Telegram Bot API expects.
Alert message format
Alerts are composed insideguardar_ataque in ids.py using the detected event fields:
tipo_final contains either the ML verdict with confidence score (e.g., SYN Flood (ML: 94.3%)) or the heuristic label (e.g., SYN Flood (Heurística)).
Async delivery
Telegram HTTP calls can take 1–5 seconds. Running them on the sniffer thread would drop packets during that window._enviar_alerta_async wraps enviar_alerta in a short-lived daemon thread so the call returns immediately:
ids.py
daemon=True means these threads are automatically killed when the main process exits — they will never hold the process open after the sniffer shuts down.
Error handling
Both network errors and API errors are caught insideenviar_alerta and printed to the console. A failed Telegram delivery does not raise an exception back to guardar_ataque, so a connectivity problem never interrupts detection or logging.
Test the bot
Runtelegram_alert.py directly to send a test message to every configured chat_id:
if __name__ == "__main__": block, which calls enviar_alerta("[ALERT] Prueba de alerta desde mi bot!"). If the message arrives in Telegram, your token and chat IDs are correct.