Alert Dispatcher Agent
Overview
The Alert Dispatcher Agent is a critical component of the traffic management system, responsible for real-time distribution of traffic alerts, accidents, congestion warnings, and citizen reports to multiple channels including email, SMS, push notifications, and webhooks.
Features
- Multi-Channel Dispatch: Email, SMS, push notifications, webhooks, WebSocket
- Priority Routing: Critical alerts bypass queuing for immediate delivery
- Template Engine: Customizable alert templates with dynamic content
- Subscription Management: User preferences and channel subscriptions
- Rate Limiting: Prevent alert fatigue with intelligent throttling
- Delivery Tracking: Monitor alert delivery status and failures
- Retry Mechanism: Automatic retry for failed deliveries
Architecture
graph TB
A[Alert Source] --> B[Alert Dispatcher]
B --> C{Priority Level}
C -->|Critical| D[Immediate Queue]
C -->|High/Medium/Low| E[Standard Queue]
D --> F[Channel Router]
E --> F
F --> G{Channel Type}
G -->|Email| H[Email Service]
G -->|SMS| I[SMS Service]
G -->|Push| J[Push Service]
G -->|Webhook| K[Webhook Service]
G -->|WebSocket| L[WebSocket Service]
H --> M[Delivery Tracker]
I --> M
J --> M
K --> M
L --> M
M --> N[MongoDB Logs]
Configuration
File: config/alert_dispatcher_config.yaml
alert_dispatcher:
channels:
email:
enabled: true
provider: "smtp"
smtp_host: "${SMTP_HOST}"
smtp_port: 587
from_address: "alerts@hcmc-traffic.vn"
sms:
enabled: true
provider: "twilio"
account_sid: "${TWILIO_ACCOUNT_SID}"
auth_token: "${TWILIO_AUTH_TOKEN}"
from_number: "+84123456789"
push:
enabled: true
provider: "firebase"
project_id: "${FIREBASE_PROJECT_ID}"
webhook:
enabled: true
timeout: 10
retry_attempts: 3
websocket:
enabled: true
server_url: "ws://localhost:8765"
priority_levels:
critical:
max_queue_time: 0 # Immediate dispatch
retry_attempts: 5
retry_delay: 10 # seconds
high:
max_queue_time: 30 # seconds
retry_attempts: 3
retry_delay: 30
medium:
max_queue_time: 120
retry_attempts: 2
retry_delay: 60
low:
max_queue_time: 300
retry_attempts: 1
retry_delay: 120
rate_limiting:
enabled: true
max_alerts_per_user_hour: 10
max_alerts_per_user_day: 50
cooldown_period: 300 # 5 minutes for same alert type
templates:
accident_critical:
subject: "🚨 Critical Accident Alert: {location}"
body: "accident_critical.html"
congestion_heavy:
subject: "⚠️ Heavy Traffic Alert: {location}"
body: "congestion_heavy.html"
citizen_report_verified:
subject: "✅ Your Report Has Been Verified"
body: "citizen_verified.html"
Usage
Basic Alert Dispatch
from src.agents.notification.alert_dispatcher_agent import AlertDispatcherAgent
# Initialize agent
agent = AlertDispatcherAgent()
# Dispatch critical accident alert
alert_id = agent.dispatch_alert(
alert_type="accident_critical",
priority="critical",
location={"lat": 10.7769, "lon": 106.7009, "address": "Nguyen Hue Blvd"},
severity="high",
description="Multi-vehicle collision blocking 3 lanes",
image_url="https://cdn.example.com/accident_001.jpg",
channels=["email", "sms", "push", "webhook"]
)
print(f"Alert dispatched: {alert_id}")