# iwyxdxl/wechatbot_wxauto_se architecture

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

- Interactive diagram: https://gitdiagram.com/iwyxdxl/wechatbot_wxauto_se
- Repository: https://github.com/iwyxdxl/wechatbot_wxauto_se

## Overview

##### Project type and purpose
- This is a monolithic full‑stack Python application implementing an intelligent WeChat chat‑bot with an embedded WebUI.
- It automates WeChat via the Mwxauto library, processes incoming messages through LLMs (GPT, Gemini, etc.), handles reminders, memory, emotion/picture recognition, and exposes a Flask‑based UI for configuration and Prompt management.

##### File structure
- Top‑level scripts:
- Run.bat – bootstrap & launch
- bot.py – core message loop & automation entrypoint
- config_editor.py – Flask application for editing settings & prompts
- updater.py – self‑update logic
- Key directories:
- Mwxauto/ – Python package wrapping wxauto and related utilities (WeChat automation)
- emojis/ – emotion categories with image assets for replies
- prompts/ – markdown files defining initial Prompt templates
- templates/ – Jinja2 HTML templates for the WebUI (config editor, login, prompt CRUD)
- libs/ – vendored Python wheel dependencies (Flask, OpenAI SDK, SQLAlchemy, etc.)
- Config & data files:
- config.py & version.json & recurring_reminders.json – JSON or Python modules storing settings, scheduled tasks, version info
- Deployment:
- requirements.txt – fallback for pip
- Run.bat – installs deps from libs/ and invokes the bot

##### README for additional insights
- Describes features (multi‑user/group chat, image & emotion recognition, timed reminders, memory, WebUI, auto‑update).
- Points to original KouriChat repo and licensing.
- Quick‑start instructions reveal a two‑phase launch: WebUI for config → “Start Bot” → automation begins.

a. Main Components
- WeChat Client (Windows)
- Mwxauto Automation Layer (Python package)
- Bot Core
– Message Dispatcher
– Prompt Manager (loads/saves Prompt templates)
– Memory Manager (summarizes chat history)
– Reminder Scheduler
– Emotion/Picture Recognizers (Pillow, OCR)
- LLM & External Services
– GPT/Gemini/DeepSeek API endpoints
– Optional Web Search API
- WebUI (Flask App)
– Config Editor & Prompt CRUD interfaces
- Data Storage
– JSON files (config, reminders, version)
– Markdown folder (prompts)
b. Relationships & Interactions
1. Incoming chat message from WeChat Client → Mwxauto listens via UI automation.
2. Mwxauto forwards event to Bot Core → Bot Core selects Prompt from Prompt Manager → packages input for LLM.
3. Bot Core calls external LLM API → receives text response (or image/emotion result) → Memory Manager may update history → Bot Core instructs Mwxauto to send reply.
4. Reminder Scheduler triggers proactive messages via Bot Core & Mwxauto.
5. WebUI ↔ Bot Core: user edits config & Prompt via HTTP endpoints (Flask). Changes persist to JSON/MD files. Bot periodically reloads.
6. Updater checks remote version → downloads & applies updates.
c. Architectural Patterns
- Layered monolith: presentation (WebUI) / application (Bot Core) / automation (Mwxauto)
- MVC for WebUI (Flask + Jinja2 templates)
- Observer/Event‑Driven for message events and scheduler
d. Technologies & Libraries
- Python 3.9+, Flask & Flask‑CORS, Jinja2
- wxauto (via Mwxauto package) for UI automation
- OpenAI SDK, httpx, requests for API calls
- Pillow for image processing
- SQLAlchemy (if DB used) or file‑based storage
- asyncio/anyio for concurrency

##### Key points to capture
- Clearly distinguish the automation layer (Mwxauto) from the Bot logic.
- Both reactive (message→reply) and proactive (scheduled reminders) flows.
- WebUI as a sidecar component that modifies runtime behavior via shared storage or direct API.
- Don’t omit the Updater sub‑component checking version.json and downloading code.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Chat Automation Pipeline
    subgraph "Chat Automation Pipeline"
        A[WeChat Client]:::external
        B["Mwxauto Automation Layer"]:::internal
        C["Bot Core"]:::internal

        A -->|"UI Automation event"| B
        B -->|"Event object"| C

        subgraph "Bot Core Components"
            MD["Message Dispatcher"]:::internal
            PM["Prompt Manager"]:::internal
            MM["Memory Manager"]:::internal
            RS["Reminder Scheduler"]:::internal
            ER["Emotion/Picture Recognizers"]:::internal
        end

        C -->|"Prepare prompt"| PM
        PM -->|"Prompt template"| C
        C -->|"LLM prompt/response"| LLM(("LLM APIs")):::external
        LLM --> C
        C -->|"Send command"| B
        B -->|"Send reply"| A
        RS -->|"Trigger proactive message"| C
        ER -->|"Load assets"| EmojiAssets(("Image Assets")):::storage
    end

    %% Configuration Pipeline
    subgraph "Configuration Pipeline"
        Browser[Browser UI]:::external
        UI["Flask WebUI"]:::internal

        Browser -->|"HTTP GET/POST"| UI

        UI -->|"Read/Write"| ConfigFile[(config.py)]:::storage
        UI -->|"Read/Write"| PromptsDir[(prompts folder)]:::storage
        UI -->|"Read/Write"| RemindersFile[(recurring_reminders.json)]:::storage

        ConfigFile -->|"Reload config"| C
        PromptsDir -->|"Reload prompts"| C
        RemindersFile -->|"Reload reminders"| C
    end

    %% Updater
    subgraph "Updater"
        UpdaterComp["Updater"]:::internal
        VersionFile[(version.json)]:::storage

        UpdaterComp -->|"Check version"| VersionFile
        UpdaterComp -->|"Download & apply updates"| UpdaterComp
        VersionFile -->|"Version info"| UpdaterComp
    end

    %% Vendored Dependencies
    libs["Vendored Dependencies"]:::storage
    libs --> C
    libs --> UI

    %% Click Events
    click B "https://github.com/iwyxdxl/wechatbot_wxauto_se/tree/main/Mwxauto/"
    click C "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/bot.py"
    click UI "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/config_editor.py"
    click templates "https://github.com/iwyxdxl/wechatbot_wxauto_se/tree/main/templates/"
    click PM "https://github.com/iwyxdxl/wechatbot_wxauto_se/tree/main/prompts/"
    click ConfigFile "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/config.py"
    click RemindersFile "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/recurring_reminders.json"
    click VersionFile "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/version.json"
    click UpdaterComp "https://github.com/iwyxdxl/wechatbot_wxauto_se/blob/main/updater.py"
    click EmojiAssets "https://github.com/iwyxdxl/wechatbot_wxauto_se/tree/main/emojis/"
    click libs "https://github.com/iwyxdxl/wechatbot_wxauto_se/tree/main/libs/"

    %% Styles
    classDef internal fill:#b3cde0,stroke:#06588d,color:#000;
    classDef external fill:#c7e9c0,stroke:#1a6532,color:#000;
    classDef storage fill:#fdae61,stroke:#e55a1f,color:#000;
```
