# shunhsiung/linebot architecture

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

- Interactive diagram: https://gitdiagram.com/shunhsiung/linebot
- Repository: https://github.com/shunhsiung/linebot

## Overview

##### Project Type and Purpose
- The project is structured as a LINE bot. The README indicates “linebot,” and the file tree shows a clear split between core bot components (in the app directory) and additional functionality through plugins.
- It is a server-side PHP project where HTTP requests likely hit the public/index.php, which then routes to start-up scripts and core bot classes.

##### File Structure
- Top-Level Files: Environment configuration (.env.example), version control (.gitignore), licensing (LICENSE), and dependency definition (composer.json/lock) are present.
- app/ Directory: This holds the core bot logic.
- The subdirectory app/MyBot contains classes such as Dependency.php, Message.php, Route.php, Setting.php, and User.php. These files likely provide the central bot functionalities (for handling messages, routing commands, managing settings and user data, etc.).
- Files like app/autoload.php and app/start.php manage the bootstrapping of the bot application.
- plugins/ Directory: This directory is used for extending the bot’s functionality.
- For example, plugins/Oursweb and plugins/YouBike contain additional PHP classes (e.g., YouBike.php, GeoYouBike.php, search_youbike.php, and Oursweb.php) that enable integration with external systems or additional features.
- A separate plugins/autoload.php may be used for loading plugin modules.
- public/ Directory: This directory contains the web entry point.
- Files like public/index.php (and .htaccess for URL rewriting) are intended to receive incoming HTTP requests and forward them to the application’s bootstrap (app/start.php).

a. Main Components:
- Web Frontend / Entry Point: Represented by the public/ folder with index.php and .htaccess.
- Bot Core Logic: Contained in the app/ directory. Within this, the “MyBot” module encapsulates the primary messaging, routing, and dependency management (Dependency.php, Message.php, Route.php, Setting.php, User.php).
- Plugin System: Illustrated by the plugins/ folder. Each subdirectory (e.g., Oursweb, YouBike) corresponds to a plugin that extends or customizes bot behavior.
- External Libraries/Dependencies: Handled by Composer (composer.json, composer.lock) and autoloaders in both the app/ and plugins/ directories.
- Configuration: Environment files (.env.example) and settings modules in app/MyBot contribute to setup and configuration.

b. Interactions and Data Flow:
- Incoming requests (likely from the LINE platform or an HTTP request) are received at public/index.php (web server layer).
- The request flows from public/index.php to app/start.php, which sets up the environment and loads the “MyBot” core.
- The MyBot classes (e.g., Route.php, Message.php) then process messaging, manage routing, and interact with plugins when necessary.
- The plugin autoloader and individual plugin classes are called to extend or modify the bot’s behavior, e.g., responding with YouBike or Oursweb data.
- Composer ensures all dependencies and autoloaded classes are correctly integrated.

c. Patterns/Design Principles:
- Modularity & Extensibility: The separation between the core bot (app/MyBot) and plugins illustrates a modular design, allowing for easy expansion of functionality.
- Dependency Injection / Loose Coupling: The presence of a Dependency.php suggests that dependencies are managed in a way that limits tight coupling of components.
- Separation of Concerns: Distinct responsibilities are assigned to web request handling (public/), core processing (app/), and feature extensions (plugins/).

d. Technologies/Frameworks:
- PHP serves as the primary backend language.
- Composer is used for dependency management.
- The use of .htaccess in the public directory suggests Apache (or a compatible server) is used in deployment.
- Custom autoloader scripts in both app/ and plugins/ indicate the project relies on self-managed class loading aside from (or in addition to) Composer.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Request Layer
    ext["LINE Platform"]:::external

    %% Support Configuration and Dependencies
    config[".env.example"]:::configStyle
    extDep["Composer Dependencies"]:::depStyle

    %% Web Entry Point Subgraph
    subgraph "Web Entry Point"
        idx["index.php"]:::web
        htxt[".htaccess"]:::web
    end

    %% Bot Startup / Bootstrap Subgraph
    subgraph "Bot Startup / Bootstrap"
        start["start.php"]:::bootstrap
        autoload_app["autoload.php (app)"]:::bootstrap
    end

    %% Core Bot Logic (MyBot Module) Subgraph
    subgraph "MyBot Module"
        dep["Dependency.php"]:::core
        msg["Message.php"]:::core
        route["Route.php"]:::core
        setting["Setting.php"]:::core
        user["User.php"]:::core
    end

    %% Plugin Manager
    pluginManager["Plugin Autoloader (plugins)"]:::plugin

    %% Oursweb Plugin Subgraph
    subgraph "Oursweb Plugin"
        ours["Oursweb.php"]:::plugin
    end

    %% YouBike Plugin Subgraph
    subgraph "YouBike Plugin"
        youbike["YouBike.php"]:::plugin
        geoyb["GeoYouBike.php"]:::plugin
        searchyb["search_youbike.php"]:::plugin
    end

    %% Data Flow
    ext -->|"sends request"| idx
    ext --> htxt

    idx -->|"forwards request"| start
    htxt --> start

    config -->|"loads config"| start
    extDep -->|"manages dependencies"| autoload_app

    start -->|"boots core"| dep

    dep -->|"processes"| msg
    msg -->|"routes"| route
    route -->|"configures"| setting
    setting -->|"manages"| user

    user -->|"invokes plugins"| pluginManager

    pluginManager -->|"loads"| ours
    pluginManager -->|"loads"| youbike

    %% YouBike Plugin internal connections (if any)
    youbike -->|"provides service"| geoyb
    youbike -->|"supports search"| searchyb

    %% Styling
    classDef external fill:#f9e79f,stroke:#000,stroke-width:2px;
    classDef web fill:#aed6f1,stroke:#000,stroke-width:2px;
    classDef bootstrap fill:#d7bde2,stroke:#000,stroke-width:2px;
    classDef core fill:#abebc6,stroke:#000,stroke-width:2px;
    classDef plugin fill:#f5cba7,stroke:#000,stroke-width:2px;
    classDef depStyle fill:#fadbd8,stroke:#000,stroke-width:2px;
    classDef configStyle fill:#d5f5e3,stroke:#000,stroke-width:2px;

    %% Click Events
    click idx "https://github.com/shunhsiung/linebot/blob/master/public/index.php"
    click htxt "https://github.com/shunhsiung/linebot/blob/master/public/.htaccess"
    click start "https://github.com/shunhsiung/linebot/blob/master/app/start.php"
    click autoload_app "https://github.com/shunhsiung/linebot/blob/master/app/autoload.php"
    click dep "https://github.com/shunhsiung/linebot/blob/master/app/MyBot/Dependency.php"
    click msg "https://github.com/shunhsiung/linebot/blob/master/app/MyBot/Message.php"
    click route "https://github.com/shunhsiung/linebot/blob/master/app/MyBot/Route.php"
    click setting "https://github.com/shunhsiung/linebot/blob/master/app/MyBot/Setting.php"
    click user "https://github.com/shunhsiung/linebot/blob/master/app/MyBot/User.php"
    click pluginManager "https://github.com/shunhsiung/linebot/blob/master/plugins/autoload.php"
    click ours "https://github.com/shunhsiung/linebot/blob/master/plugins/Oursweb/Oursweb.php"
    click youbike "https://github.com/shunhsiung/linebot/blob/master/plugins/YouBike/YouBike.php"
    click geoyb "https://github.com/shunhsiung/linebot/blob/master/plugins/YouBike/GeoYouBike.php"
    click searchyb "https://github.com/shunhsiung/linebot/blob/master/plugins/YouBike/search_youbike.php"
    click extDep "https://github.com/shunhsiung/linebot/blob/master/composer.json"
    click config "https://github.com/shunhsiung/linebot/blob/master/.env.example"
```
