This project is a scalable, asynchronous Telegram Bot deployed on Oracle Cloud Infrastructure (OCI). It features a Load Balanced architecture with Redis Sentinel for distributed state management, ensuring seamless conversation handling across multiple instances.
The bot operates in a High Availability (HA) environment using OCI Load Balancer and Redis Sentinel to synchronize user states between instances.
graph TD
User((User)) -->|Telegram API| TG[Telegram Server]
TG -->|Webhook| LB[OCI Load Balancer]
subgraph app_layer ["Application Layer (Round Robin)"]
LB -->|Request| InstanceA["Instance A (Quart + Bot)"]
LB -->|Request| InstanceB["Instance B (Quart + Bot)"]
end
subgraph data_layer ["Data & State Layer"]
InstanceA <-->|State Sync| RN[Redis Sentinel Cluster]
InstanceB <-->|State Sync| RN
InstanceA <-->|Data Persistence| ATP[(Oracle ATP)]
InstanceB <-->|Data Persistence| ATP
end
- Infrastructure:
- OCI Load Balancer: Distributes webhook requests via Round Robin to active instances.
- Redis Sentinel: Synchronizes
ConversationHandlerstates (context) across instances, preventing session loss during server switching. - Oracle ATP: Securely stores bot credentials (AES Encrypted), allowed users, and career data.
- Bot Functionalities:
- 🔐 Secure Auth: Password-based authentication with AES-256 decrypted tokens.
- 🎲 Interactive Tools: Random drawer (Drawing lots) with state persistance.
- ℹ️ Career Info: Dynamic career portfolio rendering from Oracle DB.
telegram-bot/
├── env/
│ └── telegram_bot.cfg # Configuration (DB, Redis, API Keys)
├── utils/
│ ├── redis_custom_module.py # Custom Redis Conversation Handler & Sentinel Logic
│ └── __init__.py
├── telegram_bot.py # Main Application Entry (Quart + Bot Logic)
└── __init__.py- Python 3.9+
- Redis Server (Sentinel Configuration)
- Oracle Database (ATP)
- Oracle Instant Client (for
cx_Oracle)
pip install quart python-telegram-bot redis cryptography cx_OracleCreate a configuration file at env/telegram_bot.cfg:
[ORACLE]
DSN=YOUR_ATP_DSN
USER=YOUR_DB_USER
PASSWORD=YOUR_DB_PASSWORD
[AES]
KEY=YOUR_AES_KEY
IV=YOUR_AES_IV
[SENTINEL]
SERVICE_NAME=mymaster
SERVICE_PASSWORD=redis_password
[BOT]
ID=YOUR_BOT_DATABASE_ID
[STICKER]
HELLO=sticker_id_here
PASS=sticker_id_here
# ... add other sticker IDsEnsure the following tables exist in your Oracle Database:
telegram.bot_credentials: Stores encrypted bot tokens.telegram.allowed_users: Whitelisted users.telegram.user_career: Career history data.
Unlike standard bots that store conversation states (WAITING_FOR_PASSWORD, etc.) in Python's local memory, this project implements a Custom Redis Conversation Store.
- Problem: In a Load Balanced environment, Request 1 (Start Auth) might hit Server A, and Request 2 (Send Password) might hit Server B. If state is local, Server B won't know the user is authenticating.
- Solution:
utils/redis_custom_module.pyoverrides the standard storage to serialize and save states into Redis. This allows any instance to retrieve the user's current context.
Instead of long-polling, the bot uses Quart (an async Flask alternative) to receive high-concurrency webhooks from Telegram, processing updates efficiently.
Run the bot application:
python telegram_bot.pyNote: Ensure your OCI Security Lists allow traffic on port 8000 (or your configured port) from the Load Balancer subnet.