# bestony/chatgpt-feishu architecture

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

- Interactive diagram: https://gitdiagram.com/bestony/chatgpt-feishu
- Repository: https://github.com/bestony/chatgpt-feishu

## Overview

##### Project Type and Purpose
- This is a lightweight, serverless-style Node.js integration that turns a Feishu bot into a ChatGPT frontend. There is no separate UI: all interaction happens via Feishu messages and webhooks.
- It handles event subscriptions from the Feishu platform, invokes the OpenAI ChatGPT API, then replies back in Feishu.

##### High-Level Architecture Components
- Feishu Platform (event source & message sink)
- User chat → Feishu message events
- Bot reply endpoint ← Feishu REST API
- AirCode Node.js Runtime (hosted serverless environment)
- event.js – the single entry-point function
- Environment Variables – APPID, SECRET, BOTNAME, KEY
- Dependencies – @larksuiteoapi/node-sdk, axios
- OpenAI API (external LLM service)

##### Component Relationships and Data Flows
a. Feishu User sends a chat message in a group or 1:1.
b. Feishu Platform pushes an HTTP POST (webhook event) to the deployed AirCode endpoint.
c. event.js:
1. Loads APPID, SECRET, BOTNAME, KEY from env vars.
2. Uses @larksuiteoapi/node-sdk to validate signature and parse the event.
3. Extracts user message content.
4. Calls OpenAI ChatCompletion API via axios, passing the user prompt and KEY.
5. Receives AI response text.
6. Uses @larksuiteoapi/node-sdk client to send a reply message back to Feishu.
d. Feishu Platform delivers the reply into the chat.

##### Architectural Patterns and Design Principles
- Event-driven, serverless function: single function triggered by HTTP events.
- Separation of concerns:
- Webhook handling and security (Feishu SDK)
- Business logic and prompt orchestration (in event handler)
- External API integration (OpenAI via axios)
- Configuration via environment variables for secrets and bot identity.
- No persistent storage—stateless, each invocation is independent.

## Diagram (Mermaid)

```mermaid
flowchart TB
    Feishu["Feishu Platform"]:::external
    OpenAI["OpenAI API"]:::external

    subgraph "AirCode Node.js Runtime"
        AirCode["event.js\nNode.js v16 runtime"]:::internal
        SDK["@larksuiteoapi/node-sdk"]:::internal
        Axios["axios"]:::internal
        Env["Env Vars\n(APPID, SECRET,\nBOTNAME, KEY)"]:::config
        AirCode --> SDK
        AirCode --> Axios
        AirCode --> Env
    end

    Feishu -->|"Webhook: message event (HTTP POST)"| AirCode
    AirCode -->|"ChatCompletion request\n(HTTPS REST)"| OpenAI
    OpenAI -->|"Response text (HTTPS REST)"| AirCode
    AirCode -->|"Bot reply (SDK call) (HTTPS REST)"| Feishu

    subgraph "Project Files"
        README["README.md"]:::config
        LICENSE["LICENSE"]:::config
    end

    click AirCode "https://github.com/bestony/chatgpt-feishu/blob/master/event.js"
    click SDK "https://github.com/bestony/chatgpt-feishu/blob/master/event.js"
    click Axios "https://github.com/bestony/chatgpt-feishu/blob/master/event.js"
    click Env "https://github.com/bestony/chatgpt-feishu/blob/master/event.js"
    click README "https://github.com/bestony/chatgpt-feishu/blob/master/README.md"
    click LICENSE "https://github.com/bestony/chatgpt-feishu/tree/master/LICENSE"

    classDef external fill:#ADD8E6,stroke:#000,color:#000
    classDef internal fill:#90EE90,stroke:#000,color:#000
    classDef config fill:#FFFFE0,stroke:#000,color:#000
```
