Telegram
Overview
The OpenAlgo Telegram Bot provides a convenient interface to access your trading data through Telegram. It offers read-only access to your trading account, allowing you to view positions, orders, holdings, P&L, and generate charts directly from your Telegram app.
Features
Account Linking: Securely link your OpenAlgo account using API keys
Real-time Data Access: View orderbook, tradebook, positions, holdings, and funds
P&L Tracking: Monitor realized and unrealized profit/loss
Quote Information: Get real-time quotes for any symbol
Chart Generation: Generate intraday and daily charts with technical indicators
Interactive Menu: Easy-to-use button interface for quick access
Setup
1. Enable Telegram Bot in OpenAlgo
Navigate to the Telegram section in your OpenAlgo dashboard
Click on "Configure Bot"
Enter your Telegram Bot Token (obtained from BotFather)
Click "Save Configuration"
Click "Start Bot" to activate the bot
2. Create Your Telegram Bot
Open Telegram and search for @BotFather
Send
/newbot
commandFollow the instructions to create your bot
Copy the bot token provided by BotFather
Use this token in the OpenAlgo configuration
3. Link Your Account
Start a conversation with your bot on Telegram
Send the
/link
command with your API key and host URL:/link your_api_key_here http://your-openalgo-host:5000
The bot will confirm successful linking
Available Commands
Account Management
/start
- Initialize the bot and see welcome message/link <api_key> <host_url>
- Link your OpenAlgo account/unlink
- Unlink your account/status
- Check connection status
Trading Data
/orderbook
- View all orders/tradebook
- View executed trades/positions
- View open positions/holdings
- View holdings/funds
- View available funds/pnl
- View profit & loss summary
Market Data
/quote <symbol> [exchange]
- Get quote for a symbolExample:
/quote RELIANCE
Example:
/quote NIFTY NSE_INDEX
Charts
/chart <symbol> [exchange] [type] [interval] [days]
- Generate price chartsType:
intraday
(default),daily
, orboth
Intervals:
1m
,3m
,5m
,15m
,30m
,1h
,D
Examples:
/chart RELIANCE
- 5-minute intraday chart/chart RELIANCE NSE intraday 15m 10
- 15-minute chart for 10 days/chart RELIANCE NSE daily D 100
- Daily chart for 100 days/chart RELIANCE NSE both
- Both intraday and daily charts
Interactive Interface
/menu
- Display interactive button menu/help
- Show help message with all commands
Chart Features
Intraday Charts
Default interval: 5 minutes
Default period: 5 days
Includes candlestick pattern and volume bars
No gaps for non-trading hours
Daily Charts
Default interval: Daily (D)
Default period: 252 trading days
Includes moving averages (MA20, MA50, MA200)
Shows candlestick patterns with volume
Chart Customization
Intervals: 1m, 3m, 5m, 15m, 30m, 1h for intraday; D for daily
Days: Customize the lookback period
Exchange: Supports NSE, BSE, NFO, CDS, MCX, NSE_INDEX, BSE_INDEX
Security
API Key Encryption
API keys are encrypted using Fernet encryption before storage
Keys are never stored in plain text
Each user's API key is isolated
Authentication
Bot requires valid API key for account linking
API key is validated against OpenAlgo server
Session-based authentication for all data requests
Privacy
Each Telegram user can only access their own linked account
No cross-user data access
Command usage is logged for security audit
Database Schema
The bot uses SQLAlchemy ORM with the following tables:
TelegramUser
Stores user-bot linkage information
Encrypted API key storage
User preferences and settings
BotConfig
Bot configuration settings
Webhook URL and polling mode
Bot state management
CommandLog
Audit trail of all commands
Usage analytics
Error tracking
Technical Architecture
Components
TelegramBotService (
services/telegram_bot_service.py
)Core bot logic and command handlers
OpenAlgo SDK integration
Chart generation using Plotly
Database Layer (
database/telegram_db.py
)SQLAlchemy models and queries
Encryption/decryption utilities
Configuration management
Blueprint (
blueprints/telegram.py
)Flask routes for bot management
Web interface for configuration
Bot lifecycle management
Auto-start Feature
Bot automatically starts on application launch if previously active
State persistence across restarts
Configured in
app.py
Threading Model
Bot runs in a separate thread with its own event loop
Non-blocking operation with main Flask application
Graceful shutdown handling
Chart Generation
Uses Plotly for chart creation
Kaleido engine for image export
Pandas for data manipulation
Category-type x-axis to handle gaps
Troubleshooting
Bot Not Responding
Check if bot is running in OpenAlgo dashboard
Verify bot token is correct
Check network connectivity
Review logs for errors
Chart Generation Issues
Ensure market data is available for the symbol
Check if the exchange is correct
Verify interval is supported
Check date range is valid
Linking Issues
Verify API key is correct
Ensure host URL is accessible
Check if API key has necessary permissions
Verify OpenAlgo server is running
Environment Variables
The bot respects the following environment variables:
DATABASE_URL
- Database connection stringENCRYPTION_KEY
- Fernet encryption key for API keysAPP_KEY
- Flask application secret keyHOST_SERVER
- OpenAlgo server URL
API Endpoints
Web Interface
GET /telegram/
- Bot management dashboardPOST /telegram/config
- Update bot configurationPOST /telegram/start
- Start the botPOST /telegram/stop
- Stop the botPOST /telegram/restart
- Restart the bot
Error Handling
All errors are logged with context
User-friendly error messages in Telegram
Automatic retry for transient failures
Graceful degradation for missing data
Performance Considerations
Charts are generated asynchronously
API calls use connection pooling
Database queries are optimized with indexes
Image generation uses efficient Kaleido backend
Future Enhancements
Support
For issues or questions:
Check the logs in OpenAlgo dashboard
Review this documentation
Contact OpenAlgo support
Last updated