# jolymmiels/remnawave-telegram-shop architecture

> Architecture diagram of the GitHub repository jolymmiels/remnawave-telegram-shop, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jolymmiels/remnawave-telegram-shop
- Repository: https://github.com/jolymmiels/remnawave-telegram-shop

## Overview

##### Project type and purpose
- This is a Go-based backend service implementing a Telegram bot (“Remnawave Telegram Shop”) for selling and managing VPN subscriptions via Remnawave.
- It integrates with multiple payment providers (Remnawave API, CryptoPay, YooKassa, Telegram Stars) and PostgreSQL for data persistence.
- It supports scheduled tasks: user-subscription synchronization (`/sync` admin command) and daily notification dispatch.

##### File structure
- Top-level:
- cmd/app/main.go — bot entry point
- internal/… — business logic packages (config, handler, database, payment, notification, sync, translation, external clients)
- db/migrations — SQL migrations for PostgreSQL schema
- translations/en.json, ru.json — UI text resources
- Dockerfile & docker-compose.yaml — containerization
- Pattern: single, modular monolith with clear package boundaries.
- Configured via environment variables (.env.sample).

##### Extract architecture details from README
- Uses pgx driver for PostgreSQL.
- Polls and synchronizes users from Remnawave.
- Sends notifications at 16:00 UTC daily.
- Multi-language support (i18n).
- UI delivered as Telegram inline keyboards.

a. Main components
- Telegram Bot Service (Go monolith)
– Config loader
– Handler layer (Telegram command and callback processing)
– Business modules: payment orchestration, subscription management, translation, notification scheduler, sync service
– Database access (repository layer)
- PostgreSQL Database
- External APIs
– Telegram Bot API
– Remnawave API
– CryptoPay API
– YooKassa API
– (Optional) Telegram Stars payment gateway
b. Relationships and interactions
1. Telegram user ↔ Telegram Bot API ↔ Bot Service
2. Bot Service ↔ PostgreSQL for CRUD on customers, purchases, referrals
3. Bot Service ↔ Remnawave API for plan retrieval & user synchronization
4. Bot Service ↔ CryptoPay / YooKassa for payment initiation & callbacks
5. Cron/ Scheduler inside Bot Service triggers:
– Daily notification module → fetch expiring subs → send messages via Telegram Bot API
– `/sync` admin command or scheduled sync → fetch users from Remnawave → update DB
c. Architectural patterns & principles
- Modular monolith: internal packages separate concerns
- Repository pattern for database access (internal/database)
- Adapter pattern for each external service client (internal/cryptopay, internal/yookasa, internal/remnawave)
- Scheduler for recurring tasks (internal/notification & internal/sync)
- Configuration via environment variables (12-factor app)
- Internationalization via JSON resource files and translation layer
d. Key technologies & libraries
- Go (modules)
- go-telegram-bot library
- pgx PostgreSQL driver
- Docker & Docker Compose
- External REST APIs (Remnawave, CryptoPay, YooKassa)
- JSON for translation resources

##### Final recommendations
- Data flows for both user-driven (commands, payments) and system-driven (sync, notifications) paths.
- Decision logic (e.g., language selection, inbound UUID filtering).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External User and APIs
    User["Telegram User"]:::external
    TelegramAPI["Telegram Bot API"]:::external
    RemnawaveAPI["Remnawave API"]:::external
    CryptoPayAPI["CryptoPay API"]:::external
    YooKassaAPI["YooKassa API"]:::external

    %% Database and Resources
    DB["PostgreSQL Database"]:::db
    Migrations["DB Migrations"]:::resource
    TranslationFiles["Translation Resources"]:::resource

    %% Bot Service Subgraph
    subgraph "Bot Service" 
        direction TB
        Main["Bot Entry Point\n(main.go)"]:::backend
        Config["Configuration Loader"]:::backend
        Handler["Command & Callback Handler"]:::backend
        CallbackTypes["Callback Types"]:::backend
        subgraph "Repository Layer"
            direction TB
            CusRepo["Customer Repository"]:::backend
            PurRepo["Purchase Repository"]:::backend
            RefRepo["Referral Repository"]:::backend
            Persistence["DB Initialization"]:::backend
        end
        Payment["Payment Orchestration Module"]:::backend
        subgraph "External Clients"
            direction TB
            RemnClient["Remnawave Client"]:::backend
            CryptoClient["CryptoPay Client"]:::backend
            CryptoModels["CryptoPay Models"]:::backend
            YooClient["YooKassa Client"]:::backend
            YooModels["YooKassa Models"]:::backend
        end
        Sync["Subscription Sync Service"]:::scheduler
        Notification["Notification Scheduler"]:::scheduler
        Translation["Translation Module"]:::backend
    end

    %% Data Flows
    User -->|sends commands| Handler
    Handler -->|calls API| TelegramAPI
    TelegramAPI -->|delivers updates| Handler

    Handler -->|reads/writes| Persistence
    Payment -->|reads/writes| DB
    Sync -->|reads/writes| DB
    Notification -->|reads| DB

    Persistence -->|connects to| DB

    Payment -->|initiates| CryptoPayAPI
    Payment -->|initiates| YooKassaAPI
    Sync -->|fetches plans & users| RemnawaveAPI
    Notification -->|sends messages via| TelegramAPI

    %% External Clients Usage
    RemnClient --> RemnawaveAPI
    CryptoClient --> CryptoPayAPI
    YooClient --> YooKassaAPI

    %% Resources
    Migrations -.-> Persistence
    TranslationFiles -.-> Translation

    %% Click Events
    click Main "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/cmd/app/main.go"
    click Config "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/config/cofig.go"
    click Handler "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/handler/handler.go"
    click CallbackTypes "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/handler/callback_type.go"
    click CusRepo "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/database/customer.go"
    click PurRepo "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/database/purchase.go"
    click RefRepo "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/database/referal.go"
    click Persistence "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/database/persistance.go"
    click Payment "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/payment/payment.go"
    click RemnClient "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/remnawave/client.go"
    click CryptoClient "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/cryptopay/client.go"
    click CryptoModels "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/cryptopay/models.go"
    click YooClient "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/yookasa/client.go"
    click YooModels "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/yookasa/models.go"
    click Sync "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/sync/sync.go"
    click Notification "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/notification/subscription.go"
    click Translation "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/internal/translation/translation.go"
    click Migrations "https://github.com/jolymmiels/remnawave-telegram-shop/tree/main/db/migrations"
    click TranslationFiles "https://github.com/jolymmiels/remnawave-telegram-shop/blob/main/translations/en.json"

    %% Styles
    classDef backend fill:#E3F2FD,stroke:#1E88E5,stroke-width:1px
    classDef db fill:#E8F5E9,stroke:#43A047,stroke-width:1px
    classDef external fill:#FFF3E0,stroke:#FB8C00,stroke-width:1px
    classDef scheduler fill:#F3E5F5,stroke:#8E24AA,stroke-width:1px
    classDef resource fill:#ECEFF1,stroke:#546E7A,stroke-width:1px
```
