pyTMbot Architecture¶
This document describes the current runtime architecture.
Source of truth:
pytmbot/main.pypytmbot/pytmbot_instance.pypytmbot/handlers/pytmbot/middleware/pytmbot/adapters/pytmbot/plugins/pytmbot/webhook.pypytmbot/health_system/
Runtime Shape¶
pyTMbot is a synchronous Telegram bot built on pyTelegramBotAPI.
Core entrypoints:
pytmbot/main.py: process launcher, signal handling, health startup, runtime supervisionpytmbot/pytmbot_instance.py: bot construction, middleware setup, handler registration, plugin loading, polling / webhook start
Runtime modes:
- polling mode
- webhook mode
Startup Sequence¶
High-level flow:
- CLI arguments are parsed.
BotLaunchervalidates the environment and buildsPyTMBot.PyTMBotcreates theTeleBotinstance.- Middleware chain is registered.
- Handler chain is registered.
- Requested plugins are loaded.
- Health monitoring is initialized.
- Runtime enters polling or webhook mode.
Major Subsystems¶
Configuration¶
pytmbot/settings.pypytmbot/models/settings_model.pypytmbot/globals.py
Responsibilities:
- load
pytmbot.yaml - validate and migrate config
- expose shared runtime settings objects
Core Runtime¶
pytmbot/main.pypytmbot/pytmbot_instance.py
Responsibilities:
- process lifecycle
- startup / shutdown
- polling supervision and restart strategy
- webhook fallback to polling
Handlers¶
pytmbot/handlers/bot_handlers/pytmbot/handlers/docker_handlers/pytmbot/handlers/server_handlers/pytmbot/handlers/auth_processing/
Responsibilities:
- Telegram command handling
- callback handling
- Docker UI flows
- server status views
- 2FA interaction flows
Middleware¶
update_dedupaccess_controlrate_limitsession_managersession_wrapper
Responsibilities:
- duplicate update rejection
- allowlist enforcement
- request throttling
- authentication state
- 2FA protection for sensitive handlers
Adapters¶
pytmbot/adapters/docker/pytmbot/adapters/psutil/pytmbot/db/influxdb_interface.py
Responsibilities:
- Docker API access
- host metrics collection
- InfluxDB storage and queries
Parsers And Templates¶
pytmbot/parsers/pytmbot/templates/
Responsibilities:
- Jinja2 template rendering
- render validation
- cache management
- output formatting for Telegram responses
Plugins¶
pytmbot/plugins/plugin_manager.pypytmbot/plugins/plugin_interface.py- built-in packages in
pytmbot/plugins/monitor/andpytmbot/plugins/outline/
Responsibilities:
- dynamic extension loading
- plugin metadata registration
- plugin UI integration
Webhook Runtime¶
pytmbot/webhook.py
Responsibilities:
- FastAPI / Uvicorn server
- webhook registration
- request authentication
- Telegram IP validation
- rate limiting and ban persistence
Health System¶
pytmbot/health_system/pytmbot/parsers/health_checker.py
Responsibilities:
- component-level health checks
- periodic summary generation
- startup and runtime observability
Request Flow¶
Polling mode:
- Telegram update enters
TeleBot. - Class middleware runs.
- Matching handler executes.
- Templates / adapters / plugins provide response data.
- Response is sent through Telegram API.
Webhook mode:
- FastAPI route validates webhook path and secret token.
- Client IP is validated against Telegram ranges.
- Webhook rate limiting is applied.
- Update payload is validated into
UpdateModel. TeleBot.process_new_updates()handles the update.
Shared State¶
The codebase intentionally uses a few shared singletons / cached objects:
- global settings and derived config objects
SessionManagerHealthStatus- plugin manager
- parser caches
These are part of the runtime design and are referenced by both application code and tests.