# fetgrigory/rentalsapartment_bot architecture

> Architecture diagram of the GitHub repository fetgrigory/rentalsapartment_bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fetgrigory/rentalsapartment_bot
- Repository: https://github.com/fetgrigory/rentalsapartment_bot

## Overview

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: [`src/db/database.py`](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/db/database.py) (DB layer), [`src/handlers/user_handlers.py`](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/user_handlers.py) (User handlers), [`src/services/booking_service.py`](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/booking_service.py) (Booking service), [`src/services/ai_service.py`](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/ai_service.py) (AI service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bot["Telegram bot"]
  node_main_py["Bot entry<br/>entrypoint<br/>[main.py]"]
  node_user_handlers["User handlers<br/>telegram handlers<br/>[user_handlers.py]"]
  node_admin_add["Add apartments<br/>admin handlers"]
  node_admin_edit["Edit catalog<br/>admin handlers"]
  node_admin_bookings["Booking admin<br/>admin handlers"]
  node_admin_reviews["Review admin<br/>admin handlers"]
  node_admin_catalog["Catalog admin<br/>admin handlers"]
  node_booking_service["Booking service<br/>domain service<br/>[booking_service.py]"]
  node_ai_service["AI service<br/>domain service<br/>[ai_service.py]"]
  node_reservation_draft["Draft state<br/>domain state"]
  node_db_layer[("DB layer<br/>persistence<br/>[database.py]")]
  node_nlp_llm["LLM client<br/>nlp integration<br/>[llm_client.py]"]
  node_nlp_rag["Vector search<br/>nlp retrieval<br/>[vector_search.py]"]
  node_sentiment["Sentiment<br/>nlp analysis"]
  node_payment["Payments<br/>payment integration<br/>[payment.py]"]
  node_shared["Shared glue<br/>interaction primitives<br/>[callbacks.py]"]
end

subgraph group_django["Django scaffold"]
  node_manage_py["Django entry<br/>entrypoint<br/>[manage.py]"]
  node_django_config["Django config<br/>project config<br/>[settings.py]"]
  node_core_app["Core app<br/>django app<br/>[models.py]"]
  node_room_app["Room app<br/>django app<br/>[models.py]"]
end

subgraph group_infra["Infrastructure"]
  node_docker["Runtime stack<br/>container setup<br/>[docker-compose.yml]"]
end

node_main_py -->|"dispatches updates"| node_user_handlers
node_main_py -.->|"uses"| node_shared
node_user_handlers -->|"books"| node_booking_service
node_user_handlers -->|"initiates"| node_payment
node_user_handlers -->|"assists"| node_ai_service
node_user_handlers -.->|"reads/writes"| node_db_layer
node_user_handlers -.->|"formats"| node_shared
node_admin_add -->|"mutates"| node_db_layer
node_admin_edit -->|"mutates"| node_db_layer
node_admin_bookings -->|"reads/writes"| node_db_layer
node_admin_reviews -.->|"analyzes"| node_sentiment
node_admin_reviews -->|"moderates"| node_db_layer
node_admin_catalog -.->|"reads"| node_db_layer
node_booking_service -->|"builds"| node_reservation_draft
node_booking_service -->|"checks/confirms"| node_db_layer
node_ai_service -->|"calls"| node_nlp_llm
node_ai_service -->|"retrieves"| node_nlp_rag
node_nlp_rag -.->|"embeds/searches"| node_db_layer
node_sentiment -.->|"reads"| node_db_layer
node_payment -->|"gates confirmation"| node_booking_service
node_manage_py -->|"boots"| node_django_config
node_django_config -.->|"includes"| node_core_app
node_django_config -.->|"includes"| node_room_app
node_docker -->|"runs"| node_main_py
node_docker -.->|"provisions"| node_nlp_llm

click node_main_py "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/main.py"
click node_user_handlers "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/user_handlers.py"
click node_admin_add "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/add_apartment_handlers.py"
click node_admin_edit "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/edit_apartment_handlers.py"
click node_admin_bookings "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/bookings_handlers.py"
click node_admin_reviews "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/reviews_handlers.py"
click node_admin_catalog "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/catalog_handlers.py"
click node_booking_service "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/booking_service.py"
click node_ai_service "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/ai_service.py"
click node_reservation_draft "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/reservation_draft.py"
click node_db_layer "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/db/database.py"
click node_nlp_llm "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/llm_client.py"
click node_nlp_rag "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/rag/vector_search.py"
click node_sentiment "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/sentiment_analyzer.py"
click node_payment "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/payment.py"
click node_shared "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/common/callbacks.py"
click node_manage_py "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/manage.py"
click node_django_config "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/config/settings.py"
click node_core_app "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/apps/core/models.py"
click node_room_app "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/apps/room/models.py"
click node_docker "https://github.com/fetgrigory/rentalsapartment_bot/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_py,node_user_handlers,node_admin_add,node_admin_edit,node_admin_bookings,node_admin_reviews,node_admin_catalog,node_booking_service,node_ai_service,node_reservation_draft,node_db_layer,node_nlp_llm,node_nlp_rag,node_sentiment,node_payment,node_shared toneBlue
class node_manage_py,node_django_config,node_core_app,node_room_app toneAmber
class node_docker toneMint
```

## Components

### Telegram bot

Primary runtime path for user and admin chat workflows.

- [Bot entry](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/main.py): entrypoint · Starts the Telegram bot runtime. · `main.py`
- [User handlers](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/user_handlers.py): telegram handlers · Routes end-user updates into catalog, booking, payment, and review flows. · `src/handlers/user_handlers.py`
- [Add apartments](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/add_apartment_handlers.py): admin handlers · Admin flow for creating new apartment listings. · `src/handlers/admin_panel/add_apartment_handlers.py`
- [Edit catalog](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/edit_apartment_handlers.py): admin handlers · Admin flow for updating apartment and catalog data. · `src/handlers/admin_panel/edit_apartment_handlers.py`
- [Booking admin](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/bookings_handlers.py): admin handlers · Admin flow for viewing and managing bookings. · `src/handlers/admin_panel/bookings_handlers.py`
- [Review admin](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/reviews_handlers.py): admin handlers · Admin flow for moderating user feedback and reviews. · `src/handlers/admin_panel/reviews_handlers.py`
- [Catalog admin](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/handlers/admin_panel/catalog_handlers.py): admin handlers · Admin flow for catalog browsing and management tools. · `src/handlers/admin_panel/catalog_handlers.py`
- [Booking service](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/booking_service.py): domain service · Coordinates availability checks, reservation creation, and confirmation. · `src/services/booking_service.py`
- [AI service](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/ai_service.py): domain service · Orchestrates assistant behavior and AI-backed responses. · `src/services/ai_service.py`
- [Draft state](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/services/reservation_draft.py): domain state · Holds multi-step reservation data before final booking. · `src/services/reservation_draft.py`
- [DB layer](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/db/database.py): persistence · Database connection, ORM models, and CRUD operations. · `src/db/database.py`
- [LLM client](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/llm_client.py): nlp integration · Connects the bot to a local Ollama-backed model. · `src/nlp/llm_client.py`
- [Vector search](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/rag/vector_search.py): nlp retrieval · Retrieval-augmented lookup over stored documents and embeddings. · `src/nlp/rag/vector_search.py`
- [Sentiment](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/nlp/sentiment_analyzer.py): nlp analysis · Classifies review tone and feedback sentiment. · `src/nlp/sentiment_analyzer.py`
- [Payments](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/payment.py): payment integration · Handles Telegram payments or PayMaster-style payment initiation. · `src/payment.py`
- [Shared glue](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/src/common/callbacks.py): interaction primitives · 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](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/manage.py): entrypoint · Runs Django management commands and project utilities. · `manage.py`
- [Django config](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/config/settings.py): project config · Project settings, URL routing, ASGI, and WSGI configuration. · `config/settings.py`
- [Core app](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/apps/core/models.py): django app · General Django domain app with models, admin, and views. · `apps/core/models.py`
- [Room app](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/apps/room/models.py): django app · Room-specific Django domain app with models, admin, and views. · `apps/room/models.py`

### Infrastructure

Container, environment, and runtime entry points.

- [Runtime stack](https://github.com/fetgrigory/rentalsapartment_bot/blob/main/docker-compose.yml): container setup · Container and compose setup for the bot, database, and Ollama. · `docker-compose.yml`

## Connections

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