Skip to content

glommmer/telegram-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🤖 High-Availability Oracle Cloud Telegram Bot

Python Quart Redis Oracle Cloud

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.

🏗 System Architecture

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
Loading

Key Features

  • Infrastructure:
    • OCI Load Balancer: Distributes webhook requests via Round Robin to active instances.
    • Redis Sentinel: Synchronizes ConversationHandler states (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.

📂 Project Structure

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

🚀 Installation & Setup

1. Prerequisites

  • Python 3.9+
  • Redis Server (Sentinel Configuration)
  • Oracle Database (ATP)
  • Oracle Instant Client (for cx_Oracle)

2. Install Dependencies

pip install quart python-telegram-bot redis cryptography cx_Oracle

3. Configuration

Create 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 IDs

4. Database Schema (Oracle ATP)

Ensure 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.

💡 Technical Highlights

Distributed Conversation Handler

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.py overrides the standard storage to serialize and save states into Redis. This allows any instance to retrieve the user's current context.

Async Webhook with Quart

Instead of long-polling, the bot uses Quart (an async Flask alternative) to receive high-concurrency webhooks from Telegram, processing updates efficiently.

📝 Usage

Run the bot application:

python telegram_bot.py

Note: Ensure your OCI Security Lists allow traffic on port 8000 (or your configured port) from the Load Balancer subnet.

About

Async Telegram Bot built with Quart and Python-Telegram-Bot. Integrated with Oracle DB for secure user authentication and Redis Sentinel for robust state management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages