MPTB_vshell is a robust and modular Telegram bot framework designed to integrate the power of OpenAI (ChatGPT) with advanced media management capabilities. Its flexible architecture allows for easy extension and customization.
- π€ Deep Telegram Integration: Built on
python-telegram-botfor fluid and reactive interaction. - π§ Artificial Intelligence: Native support for OpenAI (ChatGPT), enabling natural conversations and language processing.
- π Advanced Media Management: Download and upload large files with
Pyrogramefficiency. - π¬ Media Compression: Built-in video compression capabilities leveraging FFmpeg (H.265/HEVC).
- π Web Interface: Integrated Flask-based web dashboard for file management (
web/app.py). - π³ Docker Ready: Includes a configured
Dockerfilewith all dependencies and optimizations (jemalloc). - π€ User & Role System: Complete management of users, states, and permissions (Admin, User, Banned, etc.).
- π§© Modular Architecture: Code organized into independent modules (
brain,core,downup,compress, etc.) for easy maintenance and scalability. - πΎ Data Persistence: Simple yet effective database system to maintain state.
- π Python 3.8 or higher.
- π¬ FFmpeg (Required for media processing).
- π± A Telegram account and a Bot Token (obtained from @BotFather).
- π Telegram API ID & API Hash (for Pyrogram, get it from my.telegram.org).
- π€ OpenAI API Key (optional, for AI features).
-
Clone the repository:
git clone <repository-url> cd MPTB_vshell
-
Create a virtual environment (recommended):
python -m venv .venv source .venv/bin/activate # On Linux/Mac # .venv\Scripts\activate # On Windows
-
Install dependencies:
pip install -r requirements.txt
Note: Ensure
ffmpegis installed on your system if you plan to use media features.
-
Build the image:
docker build -t mptb_vshell . -
Run the container:
docker run -d --env-file .env --name mptb_vshell mptb_vshell
The bot uses environment variables for configuration. You can set them in your system, create a .env file, or pass them to Docker.
Required variables:
| Variable | Description |
|---|---|
TOKEN |
Telegram Bot Token. |
API_ID |
Telegram Application ID. |
API_HASH |
Telegram API Hash. |
OPEN_AI |
OpenAI API Key. |
ADMIN |
Numerical ID of the main administrator. |
HTTP_PROXY |
(Optional) HTTP Proxy. |
HTTPS_PROXY |
(Optional) HTTPS Proxy. |
Once configured, start the bot with the following command:
python bot.pyMPTB_vshell/
βββ bot.py # π Main bot entry point
βββ clean.sh # π§Ή Script to clean temporary files
βββ database.csv # πΎ Simple database (CSV)
βββ Dockerfile # π³ Docker configuration
βββ requirements.txt # π¦ Dependency list
βββ modules/ # π§© Modular bot logic
β βββ brain.py # π§ Brain: Message processing
β βββ chatgpt.py # π€ AI: OpenAI integration
β βββ database.py # ποΈ DB: Database handling
β βββ gvar.py # βοΈ Config: Global variables & env
β βββ utils.py # π οΈ Utils: Various tools
β βββ compress/ # π¬ Compress: Video compression logic
β βββ core/ # β‘ Core: Commands, queues, and workers
β βββ downup/ # π₯π€ DownUp: Media download & upload
β βββ entity/ # π€ Entity: Object definitions (User, etc.)
β βββ fuse/ # π Fuse: Additional/experimental modules
βββ web/ # π Web: Administration web interface
βββ app.py # π Flask application
βββ static/ # π¨ Static assets
βββ templates/ # π HTML templates
This section provides a deep dive into the architecture, design patterns, and internal data flow of MPTB_vshell, intended for developers who wish to understand or extend the system.
The bot.py file serves as the system's entry point and orchestrator. It manages concurrent execution using Python's threading module to ensure the bot remains responsive while performing heavy tasks:
- Bot Application: Initializes the
python-telegram-botapplication and starts the polling loop to listen for updates. - Database Saver: A daemon thread that periodically (every
DB_SAVE_TIMEOUTseconds) commits the in-memory database state to thedatabase.csvfile on disk, ensuring data persistence without blocking the main event loop. - Web Server: Launches the Flask application (
web/app.py) in a separate thread. This allows the admin dashboard to run side-by-side with the Telegram bot process. - Initialization Routine: The
__init__function waits for the bot's event loop to be ready and verifies that the configured administrators (ADMINS_ID) are properly registered in the database with the correct privileges (ADMIN | LLM).
This module acts as the central Message Router. When an update (Message) is received, brain.py analyzes its content and delegates responsibility:
- Command Handling: If the text starts with a slash
/(or the configuredBOT_HANDLER), it matches the command against the registry inmodules/core/commands.pyand executes the corresponding function. - Media Detection: It scans messages for URLs from supported platforms (like YouTube, Instagram, Facebook). If a match is found, it triggers the extraction and download logic in
modules/downup. - AI Processing: If the message is not a command or media link, and the user possesses the
LLMflag authorization, the text is forwarded tomodules/chatgpt.pyor configured LLM provider for natural language processing.
The project uses a lightweight, custom CSV-based persistence layer designed for speed and simplicity.
- In-Memory Cache: Upon startup, the entire CSV file is loaded into a dictionary (
self.dic). This ensures that user lookups are typically O(1) operations, extremely fast. - Serialization: The
database.pymodule handles the conversion betweenpeer(User) objects and their CSV string representation. - Thread-Safety: While simple, the
save()method is called from a dedicated thread, minimizing I/O blocking on the main execution path.
To manage permissions efficiently, the system utilizes Bitwise Flags. This allows for complex role combinations within a single integer field.
- Role Flags:
ADMIN(16): Full system control.LLM(8): Authorized to interact with the AI models.BANNED(1): Restricted access.
- Message Type Flags: Used to quickly categorize content type (VIDEO, DOCUMENT, URL, etc.) internally.
- User State: A user's state is simply the sum of these flags (e.g.,
ADMIN | LLM= 24). Checks are performed using bitwise AND operations (e.g.,if user.state & ADMIN:).
This module handles the heavy lifting of file operations, specifically designed to bypass standard bot API limits where possible.
downloader.py: A custom Multi-threaded HTTP Downloader. It requests files in "chunks" (byte ranges) concurrently, saturating the available bandwidth for faster downloads.- Pyrogram Integration: For uploading files larger than 50MB (up to 2GB or 4GB for premium), the bot leverages
Pyrogram(a MTProto client) instead of the standard HTTP Bot API. This requires theAPI_IDandAPI_HASHcredentials.
Automated media optimization pipeline using FFmpeg.
- HEVC/H.265: The default encoding profile targets
libx265, providing significant size reduction (often 50%+) compared to standard H.264 while maintaining visual quality. - Concurrency: The compressor class manages FFmpeg processes, potentially running them in separate threads to avoid blocking the bot's responsiveness during long encoding tasks.
The central configuration hub.
- It loads all environment variables (
os.getenv). - It initializes shared singletons like the OpenAI client or the Database instance.
- It defines constants used across the application to avoid magic numbers or hardcoded strings.
| Variable | Criticality | Technical Description |
|---|---|---|
TOKEN |
Critical | The HTTP API Token generated by BotFather. The main python-telegram-bot instance uses this for polling and standard API calls. |
API_ID |
Critical | The Application ID from my.telegram.org. Required to initialize the MTProto client (Pyrogram). |
API_HASH |
Critical | The API Hash paired with the API_ID. Essential for the MTProto handshake. |
OPEN_AI |
Optional | API Key for OpenAI. If absent, the chatgpt.py module will gracefully fail or disable AI features. |
ADMIN |
Critical | The numeric Telegram ID of the Superuser. During the boot sequence in bot.py, this ID is automatically granted ADMIN and LLM privileges in the database. |
BOT_HANDLER |
Optional | A cleaner prefix for commands (e.g., .start vs /start). Useful when multiple bots share a group chat to prevent command naming collisions. |
HTTP_PROXY |
Optional | Proxy URL. If set, the bot routes traffic through this proxy, useful for hosting in restricted network environments. |