# daveebbelaar/python-whatsapp-bot architecture

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

- Interactive diagram: https://gitdiagram.com/daveebbelaar/python-whatsapp-bot
- Repository: https://github.com/daveebbelaar/python-whatsapp-bot

## Overview

##### Project Type and Purpose
- This is a backend service built with Flask (“run.py” entry point, “app/” package) that acts as a WhatsApp chatbot.
- It integrates with Meta’s WhatsApp Cloud API for sending and receiving messages and with OpenAI’s Assistants API to generate AI responses.

##### File Structure
- Top-level:
- run.py – starts the Flask server
- requirements.txt, example.env – dependency and env setup
- start/ – standalone Python scripts showing quickstart examples (whatsapp_quickstart.py and assistants_quickstart.py)
- data/, docs/, img/ – reference assets, not part of the runtime
- app/: contains core MVC-style layers
- config.py – environment/configuration loader
- views.py – Flask route definitions (webhook endpoint, health checks, etc.)
- decorators/security.py – request verification (webhook signature, token check)
- services/openai_service.py – encapsulates OpenAI API calls
- utils/whatsapp_utils.py – message parsing, response‐generation glue code

##### README for Additional Insights
- Describes the end-to-end flow:
1. Configure environment (APP_ID, SECRET, VERIFY_TOKEN, ACCESS_TOKEN)
2. Use ngrok to publish /webhook endpoint
3. Receive incoming messages via GET/POST at /webhook
4. Verify requests, parse payloads, call OpenAI, then POST reply back to WhatsApp API
- Mentions security patterns (SHA-256 payload signing)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Infrastructure & External
    User["User"]:::external
    APIWhats["WhatsApp Cloud API"]:::external
    APIOpen["OpenAI Assistants API"]:::external
    Ngrok["ngrok Tunnel"]:::infra
    Env[("ENV Variables")]:::infra
    Req["requirements.txt"]:::infra

    %% Flask Application Components
    subgraph "Flask Application"
        direction TB
        Entry["run.py"]:::internal
        Config[("Configuration Module")]:::infra
        Security["Security Decorator"]:::internal
        Controller["Flask Controller (/webhook)"]:::internal
        Service["OpenAI Service"]:::internal
        Utils["WhatsApp Utils"]:::internal
    end

    %% Example Scripts
    subgraph "Quickstart Examples"
        direction TB
        QA["whatsapp_quickstart.py"]:::infra
        AB["assistants_quickstart.py"]:::infra
    end

    %% Data Flow
    User -->|"Sends message"| APIWhats
    APIWhats -->|"HTTP GET/POST /webhook"| Ngrok
    Ngrok -->|"Tunnel to localhost"| Controller
    Controller -->|"verify request"| Security
    Security --> Controller
    Controller -->|"parse message"| Utils
    Utils --> Controller
    Controller -->|"generate_response()"| Service
    Service -->|"HTTP call"| APIOpen
    APIOpen -->|"AI response"| Service
    Service --> Controller
    Controller -->|"format reply"| Utils
    Utils --> Controller
    Controller -->|"POST /messages"| APIWhats
    APIWhats --> User

    %% Initialization & Config
    Entry --> Config
    Entry --> Req
    Config --> Controller
    Config --> Service
    Config --> Security

    %% Quickstart Example Flows
    QA -.-> APIWhats
    AB -.-> APIOpen

    %% Click Events
    click Entry "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/run.py"
    click Config "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/app/config.py"
    click Security "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/app/decorators/security.py"
    click Controller "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/app/views.py"
    click Service "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/app/services/openai_service.py"
    click Utils "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/app/utils/whatsapp_utils.py"
    click QA "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/start/whatsapp_quickstart.py"
    click AB "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/start/assistants_quickstart.py"
    click Env "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/example.env"
    click Req "https://github.com/daveebbelaar/python-whatsapp-bot/blob/main/requirements.txt"

    %% Styles
    classDef internal fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef external fill:#90EE90,stroke:#000,stroke-width:1px
    classDef infra fill:#FFA500,stroke:#000,stroke-width:1px
```
