Overview
The Telegram bot service is a standalone Go application that handles bidirectional communication between PriceSignal and Telegram users. It sends price alerts to users and captures chat IDs for new subscribers.Architecture
Implemented insrc/telegram-bot/main.go, the service runs as an independent microservice that communicates with the main application via NATS JetStream.
Dependencies
Data Structures
ChatIDMessage
Sent to the main application when users register:Notification
Received from the main application to send alerts:Configuration
The service requires two environment variables:Loading Environment
NATS JetStream Setup
Connection and Stream Creation
Telegram Bot Initialization
Notification Consumer
The service consumes notifications from NATS and sends them to Telegram users:User Registration Flow
Handles incoming messages from users to capture their chat ID:Graceful Shutdown
Message Flow
Outbound (Alerts to Users)
- Main application publishes notification to
notifications.telegram - Telegram bot consumes message from JetStream
- Bot sends message to user’s chat ID
- Message acknowledged in NATS
Inbound (User Registration)
- User sends message to bot
- Bot captures chat ID and username
- Bot publishes to
notifications.init.telegram - Main application processes and stores user info
- Bot confirms registration to user
Deployment
Building the Service
Running the Service
Docker Deployment
Error Handling
- Failed message sends are logged but don’t stop the service
- Malformed notifications are logged and skipped
- Connection errors cause the service to exit (managed by orchestrator)
Related Components
- NATS Messaging - Message broker for notifications
- Rule Engine - Triggers alerts that get sent via Telegram