# devvsima/aiogram-peewee-template architecture

> Architecture diagram of the GitHub repository devvsima/aiogram-peewee-template, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devvsima/aiogram-peewee-template
- Repository: https://github.com/devvsima/aiogram-peewee-template

## Overview

##### Project Type and Purpose
- This is a Telegram Bot service built with Aiogram 3.
- It uses Peewee as the ORM, supports i18n (multiple locales), Redis for caching/session storage, and PostgreSQL (or SQLite) for persistent storage.
- Its purpose is to respond to user commands via Telegram, handle admin operations, manage user sessions, translate messages, and store user data.

##### File Structure
Top‑level
- main.py – bootstrap and run the bot
- loader.py – load environment, configuration, and initialize subsystems
- requirements.txt / Dockerfile / .env.dist – deployment and dependency manifests
app/
- filters/ – custom Aiogram filters (e.g., message type, user roles)
- handlers/ – request handlers subdivided into admin and user flows; bot_utils and msg_text helper modules
- keyboards/ – default and inline keyboard generators
- middlewares/ – Aiogram middlewares for admin checks, i18n, session/state initialization
- others/ – shared constants (commands, FSM states)
- routers.py – assemble Aiogram routers from handlers and middlewares
data/
- config.py – configuration loader (dotenv)
- locales/ – gettext files (.po/.mo) for supported languages
database/
- .py – database connection factory (PostgreSQL/SQLite)
- models/ – Peewee model definitions (User model)
- service/ – data‐access layer (CRUD wrappers for models)
utils/ – shared utilities (base62 encoder, custom logging)

##### Pulling Insights from the README
- Highlights the stack (Aiogram 3, i18n, Redis, Peewee, PostgreSQL/SQLite).
- Describes environment variables for Redis, Postgres, Bot token, and Admin IDs.

##### Core Components
a. Telemetry & External Interfaces
- Telegram Bot API (via HTTPS long‐polling or webhooks)
- Redis Server (cache, sessions)
- Database Server (PostgreSQL or SQLite)
b. Application Layers
1. Loader / Config – reads .env, sets up Redis and DB connections
2. Router – Aiogram Dispatcher + Routers + Middlewares
3. Filters & Middlewares – pre‐process updates (auth, i18n, states)
4. Handlers – business logic (admin flows, user flows, help, start, invite, language switch)
5. Service Layer – database service modules wrap Peewee models
6. Utilities – logging, base62 encoding, message templating
c. Data Stores
- Redis – per‑user/session state, caching
- PostgreSQL/SQLite – persistent user records
- gettext locales on disk – translation files
d. External Flows
- Admins interact via Telegram → Bot → Admin Handlers → Service → DB
- Users interact → Bot → User Handlers → Service → DB
- Middlewares inject i18n context and guard routes

##### Architectural Patterns & Principles
- Layered architecture (Configuration → Routing → Processing → Data‐Access)
- Separation of concerns (handlers vs. service vs. models vs. utils)
- Dependency injection in loader: centralized init of Redis, DB, i18n, Dispatcher
- FSM pattern for user state management via Aiogram

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Services
    Telegram["Telegram Bot API"]:::external
    Redis["Redis Server"]:::datastore
    DB["PostgreSQL/SQLite"]:::datastore
    Locales["Locales (.po/.mo files)"]:::datastore

    %% Application Entry and Configuration
    subgraph "Application"
        Main["main.py"]:::entry
        Loader["loader.py"]:::config
        Config["data/config.py"]:::config

        Main -->|"bootstrap"| Loader
        Loader -->|"load env"| Config
        Loader -->|"init cache"| Redis
        Loader -->|"init db"| DB
        Loader -->|"load i18n"| Locales

        Loader -->|"setup dispatcher"| Routers

        click Main "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/main.py"
        click Loader "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/loader.py"
        click Config "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/data/config.py"
    end

    %% Routing Layer
    Routers["Dispatcher & Routers\n(app/routers.py)"]:::routing
    click Routers "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/routers.py"

    %% Filters Layer
    subgraph "Filters"
        FiltersNode["Filters\n(app/filters/)"]:::filter
        click FiltersNode "https://github.com/devvsima/aiogram-peewee-template/tree/aiogram3/app/filters/"
    end

    %% Middlewares Layer
    subgraph "Middlewares"
        AdminMW["Admin Middleware"]:::middleware
        I18nMW["i18n Middleware"]:::middleware
        StartMW["Start Middleware"]:::middleware
        UserMW["User Middleware"]:::middleware

        click AdminMW "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/middlewares/admin.py"
        click I18nMW "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/middlewares/i18n.py"
        click StartMW "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/middlewares/start.py"
        click UserMW "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/middlewares/user.py"
    end

    %% Handlers Layer
    subgraph "Handlers"
        AdminHandlers["Admin Handlers\n(app/handlers/admin/admin.py)"]:::handler
        UserStart["Start Handler\n(app/handlers/user/start.py)"]:::handler
        UserHelp["Help Handler\n(app/handlers/user/help.py)"]:::handler
        UserInvite["Invite Handler\n(app/handlers/user/invite.py)"]:::handler
        UserLang["Language Switch Handler\n(app/handlers/user/lang.py)"]:::handler
        BotUtils["Bot Utilities\n(app/handlers/bot_utils.py)"]:::handler
        MsgText["Message Templates\n(app/handlers/msg_text.py)"]:::handler

        click AdminHandlers "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/admin/admin.py"
        click UserStart "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/user/start.py"
        click UserHelp "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/user/help.py"
        click UserInvite "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/user/invite.py"
        click UserLang "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/user/lang.py"
        click BotUtils "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/bot_utils.py"
        click MsgText "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/handlers/msg_text.py"
    end

    %% Service Layer
    subgraph "Service Layer"
        UserService["User Service\n(database/service/users.py)"]:::service
        click UserService "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/database/service/users.py"
    end

    %% Models and Database
    subgraph "Database Modules"
        DBConnect["DB Connect\n(database/connect.py)"]:::database
        UserModel["User Model\n(database/models/users.py)"]:::database
        click DBConnect "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/database/connect.py"
        click UserModel "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/database/models/users.py"
    end

    %% Keyboards
    subgraph "Keyboards"
        KBDefaultBase["Default KB Base\n(app/keyboards/default/base.py)"]:::ui
        KBDefaultGen["KB Generator\n(app/keyboards/default/kb_generator.py)"]:::ui
        KBInlineLang["Inline Lang KB\n(app/keyboards/inline/lang.py)"]:::ui

        click KBDefaultBase "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/keyboards/default/base.py"
        click KBDefaultGen "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/keyboards/default/kb_generator.py"
        click KBInlineLang "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/keyboards/inline/lang.py"
    end

    %% Shared Constants
    subgraph "Shared Constants"
        Commands["Commands\n(app/others/commands.py)"]:::constant
        States["FSM States\n(app/others/states.py)"]:::constant

        click Commands "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/others/commands.py"
        click States "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/app/others/states.py"
    end

    %% Utilities
    subgraph "Utilities"
        Base62["Base62 Encoder\n(utils/base62.py)"]:::utility
        Logging["Logging\n(utils/logging.py)"]:::utility

        click Base62 "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/utils/base62.py"
        click Logging "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/utils/logging.py"
    end

    %% Deployment
    subgraph "Deployment"
        Requirements["requirements.txt"]:::deploy
        DockerfileNode["Dockerfile"]:::deploy
        EnvDist[".env.dist"]:::deploy

        click Requirements "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/requirements.txt"
        click DockerfileNode "https://github.com/devvsima/aiogram-peewee-template/tree/aiogram3/Dockerfile"
        click EnvDist "https://github.com/devvsima/aiogram-peewee-template/blob/aiogram3/.env.dist"
    end

    %% Data Stores
    Loader --> Routers
    Routers --> FiltersNode
    FiltersNode --> AdminMW
    AdminMW --> I18nMW
    I18nMW --> StartMW
    StartMW --> UserMW
    UserMW --> AdminHandlers
    UserMW --> UserStart
    UserMW --> UserHelp
    UserMW --> UserInvite
    UserMW --> UserLang
    UserMW --> BotUtils
    UserMW --> MsgText

    AdminHandlers --> UserService
    UserStart --> UserService
    UserHelp --> UserService
    UserInvite --> UserService
    UserLang --> UserService

    UserService --> UserModel
    UserService --> DBConnect
    UserService <--> Redis
    DBConnect --> DB

    %% Utilities integration
    Loader --> Logging
    Routers --> Logging
    AdminHandlers --> Logging
    UserStart --> Logging
    UserService --> Logging

    %% External Flows
    Telegram -->|"get_update"| Main
    UserService -->|"persist or fetch"| DB
    Redis -->|"cache/session"| UserService

    %% Locales usage
    Locales --> I18nMW

    %% Styling
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px
    classDef datastore fill:#ffdddd,stroke:#aa0000
    classDef entry fill:#ffffcc,stroke:#999900
    classDef config fill:#fff2cc,stroke:#cc9900
    classDef routing fill:#ddffdd,stroke:#00aa00
    classDef filter fill:#e8e8ff,stroke:#0000aa
    classDef middleware fill:#f0e8ff,stroke:#8800aa
    classDef handler fill:#ddeeff,stroke:#0066cc
    classDef service fill:#ffe8cc,stroke:#cc6600
    classDef database fill:#ffeecc,stroke:#aa5500
    classDef ui fill:#ccffcc,stroke:#009900
    classDef constant fill:#f2f2f2,stroke:#666666
    classDef utility fill:#ffe8ff,stroke:#cc0099
    classDef deploy fill:#e0e0e0,stroke:#888888
```
