How yonkosam/whatsapp-python-chatbot works
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.