# yonkosam/whatsapp-python-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/yonkosam/whatsapp-python-chatbot
- Repository: https://github.com/yonkosam/whatsapp-python-chatbot

## Overview

##### Project type and purpose
- This is a lightweight, webhook-driven, Python-based chatbot microservice.
- It integrates WhatsApp via WaSenderAPI and uses Google’s Gemini AI for message generation.

##### File structure
- Top level:
- script.py – the Flask application and webhook handler
- requirements.txt – Python dependencies
- .env & .env.example – environment-variable configuration
- persona.json & persona.json.example – AI persona definitions
- No separate “frontend” or database directories; the service is stateless between requests (context is kept in memory per webhook call).

##### Extract architecture details from the README
- Incoming messages arrive via WaSenderAPI webhooks.
- Flask (script.py) receives them on POST /webhook.
- The bot loads persona.json to build a base prompt.
- It calls the Gemini AI API over HTTPS, receives a response, splits long responses into chunks, then calls WaSenderAPI to send replies back to WhatsApp.
- Uses Python’s logging module for error handling.
- In production, it’s recommended to run under Gunicorn behind an Nginx reverse proxy; in development you may use ngrok to expose localhost.

##### Main components and their interactions
A. External Actors & Services
1. WhatsApp end user – sends and receives messages
2. WaSenderAPI – webhook ingress and message-send egress
3. Google Gemini AI API – generative AI backend
4. (Dev only) ngrok – exposes local server to the internet
5. (Prod only) Gunicorn & Nginx – WSGI server and reverse proxy
B. Core Internal Service
1. Flask App (script.py)
– Loads configuration from .env
– Reads persona.json for AI behavior
– Exposes /webhook endpoint
– Orchestrates calls to Gemini and WaSenderAPI
– Splits and formats outgoing messages
– Logs events and errors
C. Configuration Artifacts
– .env (API keys, ports)
– persona.json (AI persona)

Data flow summary:
WhatsApp User → WaSenderAPI Webhook → Flask /webhook
→ [Load persona + env]
→ Call Gemini API → Receive AI response
→ Split into chunks
→ Call WaSenderAPI Send endpoint
→ WaSenderAPI → WhatsApp User

##### Architectural patterns & design principles
- Webhook-driven event handler: stateless per request, easily scalable horizontally.
- Configuration-as-code: separation of secrets (.env) and persona (.json).
- Layered external integrations: clearly separate messaging ingress/egress (WaSenderAPI) from AI logic (Gemini).
- Optional reverse proxy and WSGI layer for production robustness.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Actor
    User["WhatsApp User"]:::actor

    %% External Services
    subgraph "External Messaging Service" 
        direction TB
        WaIngress["WaSenderAPI\nWebhook Ingress"]:::external
        WaSend["WaSenderAPI\nSend Endpoint"]:::external
    end

    subgraph "External AI Service"
        direction TB
        Gemini["Google Gemini AI API"]:::external
    end

    %% Internal Service
    subgraph "Internal Chatbot Service"
        direction TB
        Flask["Flask App\n(script.py)"]:::internal
        Env["Env Config\n(.env)"]:::config
        Persona["AI Persona\n(persona.json)"]:::config
        Requirements["Python Deps\n(requirements.txt)"]:::config
    end

    %% Development & Production
    subgraph "Development" 
        direction TB
        Ngrok["ngrok"]:::dev
    end

    subgraph "Production"
        direction TB
        Nginx["Nginx"]:::prod
        Gunicorn["Gunicorn Workers"]:::prod
    end

    %% Data Flow
    User -->|"Sends WhatsApp message"| WaIngress
    WaIngress -->|"HTTPS POST /webhook"| Flask
    Ngrok -->|"tunnels requests to"| Flask
    Flask -->|"Loads at startup"| Env
    Flask -->|"Loads at startup"| Persona
    Flask -->|"Imports"| Requirements
    Flask -->|"HTTPS POST /v1/chat"| Gemini
    Gemini -->|"AI response"| Flask
    Flask -->|"Split & format"| WaSend
    WaSend -->|"Delivers message"| User
    Flask -->|"WSGI interface"| Gunicorn
    Gunicorn -->|"Reverse proxy"| Nginx
    Nginx -->|"Proxies to"| Flask

    %% Click Events
    click Flask "https://github.com/yonkosam/whatsapp-python-chatbot/blob/main/script.py"
    click Env "https://github.com/yonkosam/whatsapp-python-chatbot/blob/main/.env.example"
    click Persona "https://github.com/yonkosam/whatsapp-python-chatbot/blob/main/persona.json.example"
    click Requirements "https://github.com/yonkosam/whatsapp-python-chatbot/blob/main/requirements.txt"
    click Nginx "https://github.com/yonkosam/whatsapp-python-chatbot/blob/main/README.md"

    %% Styles
    classDef actor fill:#f9f,stroke:#333,stroke-width:2px
    classDef external fill:#cff,stroke:#036,stroke-width:2px
    classDef internal fill:#fcf,stroke:#903,stroke-width:2px
    classDef config fill:#ffc,stroke:#975,stroke-width:2px
    classDef dev fill:#efe,stroke:#070,stroke-width:2px
    classDef prod fill:#eef,stroke:#007,stroke-width:2px
```
