How fetgrigory/rentalsapartment_bot works
1. Repository shape
This repo contains two clear application surfaces:
- a Telegram bot in src/, which appears to be the primary product;
- a Django project scaffold in config/ and apps/, likely for a separate web/admin or domain model layer.
The README focuses on the bot, so the bot is the main runtime path.
2. Primary runtime entry points
- main.py is the bot startup entry.
- manage.py plus config/ and apps/ are the Django entry points for migrations, admin, and any web-side functionality.
- Dockerfile and docker-compose.yml package the bot plus supporting services, especially Ollama.
3. Bot architecture: update-driven flow
The Telegram bot is structured around aiogram-style message/callback handlers:
- src/handlers/user_handlers.py for end users
- src/handlers/admin_panel/* for admin workflows
Incoming Telegram updates are routed to handlers, which then call services, database CRUD, NLP, or payment logic.
4. Business logic layer
src/services/ holds orchestration logic that sits between handlers and infrastructure:
- booking_service.py likely coordinates reservation creation and date-conflict checks
- ai_service.py wraps AI/NLP assistant behavior
- reservation_draft.py suggests multi-step booking state accumulation before final commit
This is the main boundary where user intent becomes domain actions.
5. Persistence layer
src/db/ is the bot’s data-access layer:
- database.py for DB connection/session setup
- models.py for ORM entities
- crud.py for queries and mutations
The README and dependencies indicate PostgreSQL via SQLAlchemy and psycopg, with pgvector for embedding storage/search.
6. Domain flows in the bot
The core domain is rentals:
- catalog browsing
- apartment/room management
- bookings with conflict validation
- review submission and moderation
- payment-triggered booking confirmation
These flows are split across handlers, services, DB, and utility modules rather than being centralized in one monolith.
7. Admin subsystem
The admin panel is implemented as a separate handler branch under src/handlers/admin_panel/.
It supports:
- adding apartments
- editing catalog entries
- viewing bookings
- reviewing user feedback
Admin actions mutate the shared database and then reflect back into catalog and booking views.
8. User-facing subsystem
src/handlers/user_handlers.py, src/keyboards/user_keyboard.py, src/utils/paginator.py, and src/utils/catalog_utils.py implement the user journey:
- browse listings
- page through catalog results
- initiate booking
- pay
- leave reviews
The keyboards and callback definitions in src/common/callbacks.py are the interaction glue for Telegram UI state.
9. AI/NLP subsystem
src/nlp/ is a distinct intelligence boundary:
- llm_client.py connects to a local LLM via Ollama
- sentiment_analyzer.py classifies review tone
- rag/vector_search.py and rag/add_document_handlers.py support retrieval-augmented answering over stored documents
This subsystem serves the “chat assistant” and review analysis features, and likely reads/writes embeddings to PostgreSQL/pgvector.
10. Payments and booking confirmation
src/payment.py isolates payment integration, which the README says uses Telegram payments/PayMaster.
The booking flow appears to be: user selects dates → booking service checks availability → payment is initiated → booking is finalized in the database.
This makes payment a gate for reservation confirmation rather than a separate billing domain.
11. Shared interaction primitives
- src/states.py defines FSM state machines for multi-step Telegram conversations.
- src/common/texts.py centralizes user-facing text.
- src/common/callbacks.py standardizes callback payloads across handlers and keyboards.
These files reduce coupling between handlers and make the conversational flows consistent.
12. Django side of the repo
apps/core and apps/room look like conventional Django apps with models, admin, views, and migrations.
They likely represent the same rental domain in a web framework, but the README does not describe them as the primary user path.
For an architecture diagram, treat this as a second framework boundary coexisting with the bot, not as the main event loop.
13. Infrastructure and execution environment
The repo is designed to run in containers and environment-driven configuration:
- .env supplies bot token, admin ID, payment token, and PostgreSQL connection info
- docker-compose.yml includes Ollama
- uv.lock / pyproject.toml define the Python environment
The external dependencies that materially shape the architecture are PostgreSQL, Telegram, PayMaster/Telegram Payments, and Ollama.
14. High-level dependency direction
The strongest dependency chain is:
Telegram update → handler → service → DB/NLP/payment integration → response back to Telegram.
Django, where used, would sit on a parallel path around its own models/views/admin, but the bot code is the clearest production flow in this repository.
Main entry points: database.py DB layer · user_handlers.py User handlers · booking_service.py Booking service · ai_service.py AI service
Components 21
Telegram bot
Primary runtime path for user and admin chat workflows.
- Bot entry · Starts the Telegram bot runtime.
main.py - User handlers · Routes end-user updates into catalog, booking, payment, and review flows.
src/handlers/user_handlers.py - Add apartments · Admin flow for creating new apartment listings.
src/handlers/admin_panel/add_apartment_handlers.py - Edit catalog · Admin flow for updating apartment and catalog data.
src/handlers/admin_panel/edit_apartment_handlers.py - Booking admin · Admin flow for viewing and managing bookings.
src/handlers/admin_panel/bookings_handlers.py - Review admin · Admin flow for moderating user feedback and reviews.
src/handlers/admin_panel/reviews_handlers.py - Catalog admin · Admin flow for catalog browsing and management tools.
src/handlers/admin_panel/catalog_handlers.py - Booking service · Coordinates availability checks, reservation creation, and confirmation.
src/services/booking_service.py - AI service · Orchestrates assistant behavior and AI-backed responses.
src/services/ai_service.py - Draft state · Holds multi-step reservation data before final booking.
src/services/reservation_draft.py - DB layer · Database connection, ORM models, and CRUD operations.
src/db/database.py - LLM client · Connects the bot to a local Ollama-backed model.
src/nlp/llm_client.py - Vector search · Retrieval-augmented lookup over stored documents and embeddings.
src/nlp/rag/vector_search.py - Sentiment · Classifies review tone and feedback sentiment.
src/nlp/sentiment_analyzer.py - Payments · Handles Telegram payments or PayMaster-style payment initiation.
src/payment.py - Shared glue · FSM states, callback payloads, and reusable text and UI helpers.
src/common/callbacks.py
Django scaffold
Parallel web/admin framework boundary and domain apps.
- Django entry · Runs Django management commands and project utilities.
manage.py - Django config · Project settings, URL routing, ASGI, and WSGI configuration.
config/settings.py - Core app · General Django domain app with models, admin, and views.
apps/core/models.py - Room app · Room-specific Django domain app with models, admin, and views.
apps/room/models.py
Infrastructure
Container, environment, and runtime entry points.
- Runtime stack · Container and compose setup for the bot, database, and Ollama.
docker-compose.yml
Connections 25
- Bot entry → User handlers: dispatches updates · Bot startup wires Telegram events into user and admin handlers.
- Bot entry → Shared glue: uses · Startup registers callback schemas, texts, and conversational state.
- User handlers → Booking service: books · User chat flow delegates booking logic to the service layer.
- User handlers → Payments: initiates · Booking confirmation can require payment setup.
- User handlers → AI service: assists · User-facing assistant queries route into AI orchestration.
- User handlers → DB layer: reads/writes · User actions ultimately persist catalog, bookings, and reviews.
- User handlers → Shared glue: formats · Handlers reuse shared texts, callbacks, and FSM states.
- Add apartments → DB layer: mutates · Admin listing creation writes directly to shared persistence.
- Edit catalog → DB layer: mutates · Admin edits update catalog and apartment records.
- Booking admin → DB layer: reads/writes · Admin booking views inspect and update reservation data.
- Review admin → Sentiment: analyzes · Moderation can use sentiment classification as support logic.
- Review admin → DB layer: moderates · Review moderation persists approval or rejection decisions.
- Catalog admin → DB layer: reads · Catalog admin reads listing data for management workflows.
- Booking service → Draft state: builds · Bookings are accumulated in draft state before commit.
- Booking service → DB layer: checks/confirms · Availability checks and final reservation writes go through persistence.
- AI service → LLM client: calls · AI orchestration depends on the local LLM client.
- AI service → Vector search: retrieves · Assistant responses can be grounded in vector search.
- Vector search → DB layer: embeds/searches · Retrieval layer reads stored vectors and document metadata.
- Sentiment → DB layer: reads · Sentiment analysis can consume stored review text.
- Payments → Booking service: gates confirmation · Payment completion is part of reservation finalization.
- Django entry → Django config: boots · Django management commands use the project configuration.
- Django config → Core app: includes · Project config wires the core domain app.
- Django config → Room app: includes · Project config wires the room domain app.
- Runtime stack → Bot entry: runs · Container setup includes the Telegram bot runtime.
- Runtime stack → LLM client: provisions · Compose setup brings up the Ollama-backed model service.