Task 9 — Telegram Notifications
The Threat Detection System implements real-time security alerts directly to Telegram using the Telegram Bot API. By utilizing the backend's access to both relational event logs and S3 image storage, threat alerts are dispatched under 1 second with a photo of the captured threat.
1. Alerting Architecture & Data Flow
sequenceDiagram
participant Edge as Pi4 Edge Camera
participant MQTT as tds-mqtt Broker
participant API as tds-api (FastAPI)
participant MinIO as tds-minio (S3)
participant Telegram as Telegram Bot API
participant User as User Mobile Client
Edge->>MQTT: Publish Event (base64 image) to cluster/camera/events
MQTT->>API: Relay Event payload
API->>MinIO: Decodes & uploads JPEG to bucket/detection-images/
API->>API: Save event log in PostgreSQL
API->>MinIO: Retrieve raw JPEG bytes
API->>Telegram: HTTP POST sendPhoto (chat_id, caption, photo binary)
Telegram->>User: Renders rich photo notification in chat
- Edge Trigger: When the edge camera parses a threat detection (
weaponorfire) from the IMX500 hardware TPU stream, it publishes the event payload containing the base64-encoded image frame tocluster/camera/events. - Backend Processing: The backend API MQTT subscriber receives the payload, uploads the image to the distributed
tds-minioobject store, and persists the metadata database record. - Async Dispatch: If the event has a severity of
HIGHorCRITICAL(e.g. weapons, fire, vandalism), the backend initiates an asynchronous, non-blocking background task. - Image Retrieval & API Call: The background task fetches the raw JPEG bytes from MinIO and triggers the Telegram Bot API
sendPhotoendpoint, forwarding the photo along with the threat metadata (confidence, node, type, timestamp) to the configured Telegram Chat.
2. Configuration & Credentials
The Telegram integration is configured in backend.yaml via the following environment variables:
| Variable | Description | Source / Reference |
|---|---|---|
TELEGRAM_BOT_TOKEN |
The unique API HTTP Token of your bot from @BotFather. |
Loaded securely from Kubernetes Secret telegram-credentials |
TELEGRAM_CHAT_ID |
Your Telegram User ID or Group Chat ID. | Loaded securely from Kubernetes Secret telegram-credentials |
3. Bot Commands & Setup
To reconfigure or create a new alert bot:
- Create Bot: Message
@BotFatheron Telegram, send/newbot, choose a name and a username ending inbot. Copy the returned API Token. - Find Chat ID: Message
@userinfobotor@myidboton Telegram, click Start, and copy the returned numericalId. - Authorize Chat: Search for your bot username (e.g.,
@threatalert2000bot) and click Start to allow it to send you private DMs. - Deploy: Update the keys in
backend.yamland runkubectl apply -f k8s/backend.yaml.
4. Verification
To verify that Telegram notifications are working:
- Confirm that the tds-api logs print Telegram threat alert delivered successfully when an alert triggers.
- Trigger a threat manually by presenting a weapon (e.g. scissors/knife) or fire (e.g. a match) to the camera.
- The bot will send a notification structured as follows:
⚠️ CRITICAL threat detected: EventType.FIRE (confidence: 96%)
[JPEG attachment]