Architecture Overview
The Telegram integration consists of:- Go-based Telegram bot (
src/telegram-bot/main.go) - Handles message sending/receiving - NATS JetStream - Message broker for reliable notification delivery
- Backend API - Manages notification channels and triggers alerts
Prerequisites
- Telegram account
- Bot token from @BotFather
- NATS server running and accessible
- Authenticated PriceSignal account
Bot Configuration
Create a Telegram bot
- Open Telegram and message @BotFather
- Send
/newbotcommand - Follow prompts to name your bot
- Save the bot token provided (format:
123456789:ABCdefGHIjklMNOpqrsTUVwxyz)
Configure environment variables
Create a Environment Variables:
.env file in the src/telegram-bot/ directory:TELEGRAM_BOT_TOKEN: Your bot token from BotFatherNATS_URL: Connection string for NATS server
How It Works
Registration Flow
When you message the bot, the following happens:-
Bot receives message with your user ID (
main.go:99-128): -
Message published to NATS on
notifications.init.telegramsubject: -
Backend receives registration (
Program.cs:216-240):
Notification Delivery
When a price rule is triggered:-
Backend publishes notification to NATS on
notifications.telegramsubject -
Bot consumes message via JetStream consumer (
main.go:72-92): - Message sent to your Telegram chat
Message Format
Registration Message
Sent from backend to bot onnotifications.init.telegram:
Notification Message
Sent from backend to bot onnotifications.telegram:
NATS Configuration
The bot creates a JetStream stream for reliable message delivery:- Name:
notifications - Subjects:
notifications.*(wildcard for all notification types) - Consumer: Durable consumer named
telegramfor persistent subscription
Managing Notification Channels
You can manage your notification channels via GraphQL:Query Notification Channels
Delete Notification Channel
To unlink your Telegram account:Multiple Accounts
You can link multiple Telegram accounts or chats to the same PriceSignal user:- Send the bot command from different Telegram accounts
- Each account will be registered as a separate notification channel
- Alerts will be sent to all registered channels
Troubleshooting
Bot not responding
- Verify bot token is correct in
.env - Check bot is running:
ps aux | grep telegram-bot - Ensure bot is not blocked in Telegram settings
Not receiving notifications
- Confirm notification channel is registered:
- Check NATS server is running and accessible
- Verify price rules are enabled
- Check bot logs for errors
Chat ID not recorded
- Ensure user ID is passed correctly in the command
- Verify user exists in database
- Check backend logs for NATS subscription errors
Production Deployment
Systemd Service Example
Create/etc/systemd/system/pricesignal-telegram.service:
Docker Deployment
Security Considerations
- Never commit bot tokens to version control
- Use environment variables for all sensitive configuration
- Validate user IDs before creating notification channels
- Consider implementing rate limiting for bot commands
- Use NATS authentication in production environments
- Enable TLS for NATS connections
Next Steps
Creating Rules
Set up price rules to trigger notifications
Real-time Charts
Monitor prices with live charting