How kukiman010/tg_gpt works
Purpose and execution surface
This is a Python Telegram AI-assistant bot. It accepts user text/media, routes requests to selectable LLM providers, supports speech and localization, and manages subscription/payment state. The likely application bootstrap is main.py, with setup centralized in configure.py and cross-component events in signals.py.
Telegram interaction and user state
The control layer represents bot-facing domain state: user identity, active environment/model settings, subscription data, payment records, and media context. These objects form the boundary between Telegram handlers and persistence/provider calls.
Paths: Control/user.py, Control/environment.py, Control/context_model.py, Control/user_media.py.
Conversation/model orchestration
main.py coordinates incoming commands/messages with user configuration and model invocation. data_models.py defines shared data structures, while signals.py likely decouples state changes or asynchronous workflow notifications. Model-specific modules are isolated behind provider adapters rather than embedded in bot handling.
Paths: main.py, data_models.py, signals.py.
Multi-provider AI gateway
The Gpt_models package is the external-model boundary. Separate adapters cover OpenAI/GPT, Claude, Google, YandexGPT, Sber GigaChat, DeepSeek, xAI, and MetaGPT. The runtime selects an adapter based on user/environment settings, sends prompts and context, and returns provider responses to the Telegram interaction layer.
Paths: Gpt_models/gpt_api.py, Gpt_models/claude_api.py, Gpt_models/yandexgpt.py, Gpt_models/sbergpt.py.
Persistence and database boundary
PostgreSQL is the intended persistent store; README requires UTF-8 setup and the repository includes a schema. Database access is separated into connection/core and API-facing layers. Persistent concerns include user profiles, environment/model choices, subscriptions, and payment information.
Paths: database.py, databaseapi.py, tools/tg_base.sql, Control/subscription_data.py.
Billing and entitlement flow
Payments are abstracted by a base provider contract and coordinated by a payment manager. Provider implementations include YooKassa and Crypto Pay. A successful payment should update payment/subscription domain state through the database boundary, enabling or extending access controls for AI services.
Paths: payments/base_pay_system.py, payments/payment_manager.py, payments/yookassa_api.py, payments/crypto_pay_api.py.
Supporting content services and deployment
Speech processing and file handling sit alongside the primary text flow, likely converting or processing user audio/uploads before response generation. Localization is gettext-style catalog based and translated through a dedicated module. The service is containerized with Docker Compose; deployment requires Python 3.8–3.10, PostgreSQL dependencies, and provider credentials/certificates for Yandex and Sber integrations.
Paths: speech.py, file_worker.py, translator.py, locale/LC_MESSAGES, Dockerfile, docker-compose.yml.
Main entry points: main.py Telegram bot coordinator · databaseapi.py Persistence API · payment_manager.py Payment manager · base_pay_system.py Payment provider contract
Components 21
Bot runtime
- Telegram bot coordinator · Routes commands, messages, and responses.
main.py - Application setup
configure.py - Event signals · Decouples cross-component state and workflow events.
signals.py - Shared data models
data_models.py
Domain and persistence
- User identity
Control/user.py - Active model settings · Stores user environment and provider selection.
Control/environment.py - Conversation context
Control/context_model.py - User media context
Control/user_media.py - Subscription state
Control/subscription_data.py - Persistence API · Persists users, settings, subscriptions, and payments.
databaseapi.py - PostgreSQL connection
database.py - Database schema
tools/tg_base.sql
AI services
- LLM provider gateway · Selects and invokes the configured external model.
Gpt_models/gpt_api.py
Billing
- Payment manager · Coordinates payment providers and entitlement updates.
payments/payment_manager.py - Payment provider contract
payments/base_pay_system.py - YooKassa adapter
payments/yookassa_api.py - Crypto Pay adapter
payments/crypto_pay_api.py
Supporting services
- Speech processing
speech.py - File worker
file_worker.py - Localization
translator.py - Container deployment · Runs the bot with PostgreSQL dependencies.
docker-compose.yml
Connections 25
- Application setup → Telegram bot coordinator: initializes
- Telegram bot coordinator → User identity: loads user
- Telegram bot coordinator → Active model settings: reads selection
- Telegram bot coordinator → Conversation context: uses context
- Telegram bot coordinator → User media context: accepts media
- Telegram bot coordinator → LLM provider gateway: sends prompts
- LLM provider gateway → Active model settings: uses provider choice
- Telegram bot coordinator → Speech processing: processes audio
- Telegram bot coordinator → File worker: processes uploads
- Telegram bot coordinator → Localization: localizes replies
- Telegram bot coordinator → Event signals: emits events
- Telegram bot coordinator → Shared data models: uses contracts
- User identity → Persistence API: persists through
- Active model settings → Persistence API: persists through
- Conversation context → Persistence API: persists through
- User media context → Persistence API: persists through
- Subscription state → Persistence API: persists through
- Persistence API → PostgreSQL connection: queries
- Database schema → PostgreSQL connection: defines
- Telegram bot coordinator → Payment manager: starts payment
- Payment manager → Payment provider contract: uses
- Payment provider contract → YooKassa adapter: implemented by
- Payment provider contract → Crypto Pay adapter: implemented by
- Payment manager → Subscription state: updates entitlement
- Container deployment → Telegram bot coordinator: runs