# nicolasgutar/bakeatbot architecture

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

- Interactive diagram: https://gitdiagram.com/nicolasgutar/bakeatbot
- Repository: https://github.com/nicolasgutar/bakeatbot

## Overview

##### Main Components
a. Flask Web Server – The heart of the application that handles incoming HTTP requests, routes them, and orchestrates the flow.
b. Webhook Endpoint – Exposed through the Flask application (located in run.py and app/views.py) for handling Meta’s webhook verification and inbound messages.
c. Security & Validation – Represented by the security decorators (in app/decorators/security.py) that validate incoming webhook requests (e.g., verifying tokens and signatures).
d. Configuration Module – Managed via app/config.py and environment variables (example.env) that store API credentials (APP_ID, APP_SECRET, ACCESS_TOKEN, VERIFY_TOKEN, etc.).
e. Service Layer – Includes the OpenAI service (app/services/openai_service.py) responsible for generating responses using AI, and potentially other services as needed.
f. Utility Modules – For tasks such as processing audio (app/utils/audio_processing.py) and handling WhatsApp-specific functionalities (app/utils/whatsapp_utils.py).
g. External & Third-Party Components – External Meta WhatsApp Cloud API, ngrok for exposing the local server, and the OpenAI API. Optionally, some documentation support components like the assets in the docs and data directories can be mentioned as supporting documentation.

##### Relationships and Interactions
- The webhook endpoint in the Flask app receives and delegates the processing. Show a component for “Webhook Handler and View” that calls security decorators before processing the payload.
- From the Message Processor, present a pathway to the “OpenAI Service” that generates appropriate AI responses.

##### Architectural Patterns and Design Principles
- Separation of Concerns – Different directories (controllers/views, services, utilities, and decorators) emphasize a modular approach.
- Security Best Practices – Include validation at entry points (using the security decorator) and secure configuration management (with environment variables).

##### Final Advice
- That all external dependencies (WhatsApp API, OpenAI, ngrok) are distinctly represented.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Services
    subgraph "External Services"
        A["WhatsApp User"]:::external
        B["Meta WhatsApp Cloud API"]:::external
        C["ngrok Tunnel"]:::external
    end

    %% Application Core
    subgraph "Application Core"
        E["Flask App (run.py & views)"]:::backend
        F["Security Decorators"]:::backend
        G["Message Processor"]:::backend
        H1["WhatsApp Utilities"]:::backend
        H2["Audio Processing"]:::backend
        I["OpenAI Service Module"]:::backend
        J["Response Formatter"]:::backend
    end

    %% Configuration and Documentation
    subgraph "Configuration & Supporting Modules"
        K["Configuration & Environment Variables"]:::config
        L["Documentation & Supporting Assets"]:::docs
    end

    %% Data Flow
    A -->|"IncomingMessage"| B
    B -->|"WebhookCall"| C
    C -->|"ForwardRequest"| E
    E -->|"SecurityCheck"| F
    F -->|"Validates"| G
    G -->|"Utilizes"| H1
    G -->|"ProcessesAudio"| H2
    G -->|"AIRequest"| I
    I -->|"GenerateResponse"| J
    J -->|"SendResponse"| E
    E -->|"OutboundMessage"| B

    %% Configuration Dependencies (dashed arrows)
    K ---|"Configures"| E
    K ---|"Configures"| F
    K ---|"Configures"| I
    K ---|"Configures"| H1
    K ---|"Configures"| H2
    L ---|"Supports"| K

    %% Click Events
    click E "https://github.com/nicolasgutar/bakeatbot/blob/main/run.py"
    click E "https://github.com/nicolasgutar/bakeatbot/blob/main/app/views.py"
    click F "https://github.com/nicolasgutar/bakeatbot/blob/main/app/decorators/security.py"
    click K "https://github.com/nicolasgutar/bakeatbot/blob/main/app/config.py"
    click K "https://github.com/nicolasgutar/bakeatbot/blob/main/example.env"
    click I "https://github.com/nicolasgutar/bakeatbot/blob/main/app/services/openai_service.py"
    click H1 "https://github.com/nicolasgutar/bakeatbot/blob/main/app/utils/whatsapp_utils.py"
    click H2 "https://github.com/nicolasgutar/bakeatbot/blob/main/app/utils/audio_processing.py"
    click L "https://github.com/nicolasgutar/bakeatbot/tree/main/docs"
    click L "https://github.com/nicolasgutar/bakeatbot/tree/main/data"

    %% Styles
    classDef external fill:#FFDD57,stroke:#333,stroke-width:2px;
    classDef backend fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef config fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef docs fill:#C8E6C9,stroke:#333,stroke-width:2px;
```
