# artifait/top.telegramframework architecture

> Architecture diagram of the GitHub repository artifait/top.telegramframework, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/artifait/top.telegramframework
- Repository: https://github.com/artifait/top.telegramframework

## Overview

##### Project Type and Purpose
- This repository is a modular framework designed for building Telegram bots with complex dialogue scenarios.
- The README and file structure show that the framework supports scenarios, modular blocks (both composite and simple), user state management (using EF Core with SQLite), and a centralized BotEngine to tie everything together.
- It is intended for reusability and extensibility, with several example implementations (e.g., EyeSaver, LearnPinger, MeowSchedulerB_Bot, Simple).

##### File Structure
- The solution (Top.TelegramFramework.sln) contains multiple projects:
- BotDataEncryptor: likely a utility for encrypting bot data.
- Core: contains the main framework code with directories such as Blocks, Data, Utilities, and Scenario support.
- Examples: different implementations that show how to use the framework’s modular components.
- Key directories/ files:
- Top.TelegramFramework.Core: Contains the main engine (BotEngine), block definitions (CompositeBlock, HandlerBlock, etc.), and user state management classes.
- Each example project (under Top.TelegramFramework.Examples.*) uses the core framework to build a sample bot.
- Configuration and build files (Dockerfile, .gitignore) indicate deployment and source control practices.

##### README for Architectural Insights
- The README clearly outlines core architectural components:
- CompositeBlock and SimpleBlock as the modular dialogue units.
- Scenario and ScenarioSelector, which manage the dialogue workflow.
- UserState and related repository classes for session management with SQLite.
- BotEngine, serving as the central coordinator responding to Telegram updates.
- It describes data flows, cloning of blocks (to isolate user sessions), and how state is captured and restored.

a. Main Components to Represent:
- Telegram Bot Client (external integration via ITelegramBotClient)
- BotEngine – the central message handler that integrates all parts.
- ScenarioSelector and Scenarios – managing and picking dialogue scenarios.
- CompositeBlock(s) – representing an individual conversation stage with methods like EnterAsync, HandleAsync, and Clone; include the additional SimpleBlock components inside.
- UserState, UserStateRepository, and UserStateContext – handling persistence via EF Core/SQLite.
- Utility classes like AESCryptography, ConsoleLogger, and ServiceRegistry (shown under Utilities in Core).
- Examples projects (EyeSaver, LearnPinger, etc.) are clients demonstrating how scenarios and blocks are composed.

b. Relationships and Flow:
- BotEngine receives incoming Telegram messages.
- It calls ScenarioSelector to choose a Scenario for the user (using chatId).
- The Scenario returns a cloned instance of the appropriate CompositeBlock.
- CompositeBlock (potentially containing embedded SimpleBlock(s)) handles message processing.
- After processing, the CompositeBlock captures its state (via CaptureState) which is saved through UserStateRepository (using EF Core’s SQLite context).
- Data flows:
- The BotEngine will subsequently interact with the UserStateRepository and ScenarioSelector.

c. Key Architectural Patterns & Design Principles:
- Modularity: Each dialogue phase is split into independent blocks.
- Isolation: Cloning of CompositeBlocks to prevent shared state among users.
- Separation of Concerns: Division between message processing (BotEngine), scenario management (Scenario/ScenarioSelector), and state persistence (UserState/Repository).
- Extensibility: Easily adding new block types or scenarios without impacting the existing flow.

d. Relevant Technologies & Frameworks:
- .NET (C#) – entire project is built in C#.
- EF Core with SQLite – for persisting the user session state.
- Telegram Bot API – integration via ITelegramBotClient.
- Docker – as evidenced by the Dockerfile, for containerization or deployment.

##### Incorporating Recent Repository Updates
- All new or modified directories/files (e.g., additional examples or changes in the utility structure).
- Updated relationships if there are any changes in how blocks are cloned or how BotEngine interacts with newer block versions.
- Any new components or refactored parts that might not have been present in previous versions.
- Verify that the updated repository structure (as seen in the file tree) is now clearly represented – for example, by showing separate branches for BotDataEncryptor, Core, and multiple Examples.

- Each major component with clear labels.
- Notations for key methods (like Clone, EnterAsync, HandleAsync, CaptureState) that implement the isolation and state management patterns.
- Updated structure based on the repository’s latest changes – ensuring that all directories, projects, and examples are represented and their relationships (especially those paths clarified in the file tree) are explicit.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Component
    TC["Telegram Bot Client"]:::external

    %% Core Framework Subgraph
    subgraph "Core Framework"
        BE["BotEngine"]:::core
        SS["ScenarioSelector / Scenarios"]:::core
        S["Scenario"]:::core
        B["CompositeBlock (with Clone, EnterAsync, HandleAsync)"]:::core
        SB["SimpleBlock (with Validator and Handler Delegates)"]:::core
        US["UserState & Repository (EF Core, SQLite)"]:::core
        U["Utilities (AESCryptography, ConsoleLogger, etc.)"]:::utility
    end

    %% Bot Data Encryptor Subgraph
    subgraph "Bot Data Encryptor"
        BDE["Bot Data Encryptor"]:::utility
    end

    %% Example Projects Subgraph
    subgraph "Example Projects"
        E1["EyeSaver"]:::example
        E2["LearnPinger"]:::example
        E3["MeowSchedulerB_Bot"]:::example
        E4["Simple"]:::example
    end

    %% Data Flow Connections
    TC -->|"message"| BE
    BE -->|"selects"| SS
    SS -->|"returns"| S
    BE -->|"calls"| B
    B -->|"invokes"| SB
    B -->|"capturesState"| US
    BE -->|"updatesState"| US
    BE -->|"uses"| U
    BE -->|"encryptsData"| BDE

    %% Example Projects Usage
    E1 -->|"implements"| BE
    E2 -->|"implements"| BE
    E3 -->|"implements"| BE
    E4 -->|"implements"| BE

    %% Styles
    classDef core fill:#f9f,stroke:#333,stroke-width:2px;
    classDef utility fill:#ccf,stroke:#333,stroke-width:2px;
    classDef external fill:#cfc,stroke:#333,stroke-width:2px;
    classDef example fill:#fc9,stroke:#333,stroke-width:2px;

    %% Click Events
    click BE "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Core/BotEngine.cs"
    click S "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Core/Scenario.cs"
    click SS "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Core/ScenarioSelector.cs"
    click B "https://github.com/artifait/top.telegramframework/tree/master/Top.TelegramFramework.Core/Blocks"
    click US "https://github.com/artifait/top.telegramframework/tree/master/Top.TelegramFramework.Core/Data"
    click U "https://github.com/artifait/top.telegramframework/tree/master/Top.TelegramFramework.Core/Utilities"
    click BDE "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.BotDataEncryptor"
    click E1 "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Examples.EyeSaver"
    click E2 "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Examples.LearnPinger"
    click E3 "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Examples.MeowSchedulerB_Bot"
    click E4 "https://github.com/artifait/top.telegramframework/blob/master/Top.TelegramFramework.Examples.Simple"
```
