43 - Telegram Bot Configuration

Overview

OpenAlgo integrates with Telegram to provide real-time trading notifications, account information, and bot commands. Users can configure their Telegram bot to receive order alerts, position updates, and execute queries.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                       Telegram Bot Architecture                              │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────┐
│                           Telegram Cloud                                     │
│                                                                              │
│  ┌─────────────────┐              ┌─────────────────┐                       │
│  │  User's         │              │   Bot Father    │                       │
│  │  Telegram App   │              │   @BotFather    │                       │
│  └────────┬────────┘              └────────┬────────┘                       │
│           │                                │                                 │
│           │  Messages/Commands             │  Create Bot Token              │
│           │                                │                                 │
│           └────────────────┬───────────────┘                                │
│                            │                                                 │
│                    Bot API Gateway                                          │
│                            │                                                 │
└────────────────────────────┼────────────────────────────────────────────────┘

                             │ Webhook / Long Polling


┌─────────────────────────────────────────────────────────────────────────────┐
│                        OpenAlgo Backend                                      │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                    Telegram Blueprint                                │   │
│  │                    /telegram/*                                       │   │
│  │                                                                      │   │
│  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐              │   │
│  │  │ /settings    │  │ /webhook     │  │ /test        │              │   │
│  │  │ Configure    │  │ Receive      │  │ Send test    │              │   │
│  │  │ bot token    │  │ updates      │  │ message      │              │   │
│  │  └──────────────┘  └──────────────┘  └──────────────┘              │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                    Telegram Service                                  │   │
│  │                                                                      │   │
│  │  ┌──────────────────────────────────────────────────────────────┐  │   │
│  │  │  Command Handler                                              │  │   │
│  │  │                                                               │  │   │
│  │  │  /start    - Initialize bot                                   │  │   │
│  │  │  /help     - Show commands                                    │  │   │
│  │  │  /funds    - Account balance                                  │  │   │
│  │  │  /positions- Open positions                                   │  │   │
│  │  │  /orders   - Order book                                       │  │   │
│  │  │  /holdings - Portfolio holdings                               │  │   │
│  │  │  /trades   - Trade book                                       │  │   │
│  │  │  /pnl      - P&L summary                                      │  │   │
│  │  │  /quote    - Get LTP                                          │  │   │
│  │  │  /status   - Connection status                                │  │   │
│  │  │  /alerts   - Toggle alerts                                    │  │   │
│  │  │  /settings - Preferences                                      │  │   │
│  │  │  /logout   - Disconnect                                       │  │   │
│  │  └──────────────────────────────────────────────────────────────┘  │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                    Database Layer                                    │   │
│  │                                                                      │   │
│  │  telegram_users │ bot_config │ command_log │ notification_queue     │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘

Database Schema

telegram_users Table

bot_config Table

notification_queue Table

user_preferences Table

Bot Commands

Command Reference

Command
Description
Example

/start

Initialize bot and link account

/start

/help

Display available commands

/help

/funds

Get account balance and margin

/funds

/positions

View open positions with P&L

/positions

/orders

Get today's order book

/orders

/holdings

View portfolio holdings

/holdings

/trades

Get executed trades

/trades

/pnl

Get P&L summary

/pnl

/quote SYMBOL

Get last traded price

/quote SBIN

/status

Check broker connection

/status

/alerts on/off

Toggle notifications

/alerts on

/settings

View/modify preferences

/settings

/logout

Disconnect bot

/logout

Configuration Flow

Service Implementation

Bot Token Security

Command Handler

Notification Service

API Endpoints

Save Configuration

Test Connection

Response:

Webhook Endpoint

Notification Types

Order Notifications

Position Alerts

P&L Summary

Error Handling

Rate Limiting

Retry Logic

Key Files Reference

File
Purpose

blueprints/telegram.py

Telegram routes and webhook

services/telegram_bot_service.py

Bot command handlers

services/telegram_alert_service.py

Alert/notification service

database/telegram_db.py

Database models

restx_api/telegram_bot.py

REST API endpoints

frontend/src/pages/TelegramSettings.tsx

Configuration UI

Last updated