How dahshury/python-whatsapp-bot works
Project type and purpose
• This project is a full-stack application featuring a WhatsApp bot that uses FastAPI as the backend, integrating AI services (OpenAI and Anthropic) for natural language processing.
• It also includes a Streamlit-based frontend for managing reservations and viewing conversation details stored in an SQLite database.
• Additional automated scripts handle functions such as sending reminders and backing up the database.
File structure to identify main components
• Backend Application (FastAPI):
• Entry point is run.py, which starts the FastAPI server.
• app/ directory holds the core code.
▪ app/config.py (configuration settings from environment variables)
▪ app/db.py (SQLite database connections and schema definitions)
▪ app/views.py (defines FastAPI routes including the webhook endpoint for WhatsApp)
• Decorators inside app/decorators provide safety and security (e.g., signature verification for incoming webhook requests).
• Services in app/services handle business logic:
▪ openai_service.py – integrates OpenAI for processing WhatsApp messages.
▪ anthropic_service.py – integrates with Anthropic.
▪ assistant_functions.py – manages reservation operations (create, update, cancel).
• Utility modules in app/utils (e.g., logging, thread management, WhatsApp-specific functions).
• Frontend Components:
• Streamlit application (st_app.py) serves as the primary UI for managing reservations.
• The app/frontend directory contains UI assets, including HTML, CSS, and JavaScript for custom widgets like a clock and a datepicker.
• External Integrations:
• WhatsApp API (via Meta developer account) for messaging.
• AI services (OpenAI and Anthropic) for intelligent response generation.
• SQLite for persistent data storage.
• Additional Scripts & Infrastructure:
• send_reminders.py – script to send automated reservation reminders.
• sqlite_backup.sh – Bash script to perform periodic backup of the SQLite database.
• CI/CD configuration found under .github/workflows and a Procfile for deployment.
a. FastAPI Backend:
• Component: API Server
• Sub-components: Webhook Endpoint (views.py), Security & Verification (decorators/security.py), Database interactions (db.py), and AI Service integration (services/*).
▪ WhatsApp API (incoming requests and outgoing responses),
▪ AI services (OpenAI & Anthropic),
▪ SQLite database.
b. Database:
• Link between the backend’s business logic (assistant_functions.py) and the database.
c. Frontend (Streamlit App):
• Component: Frontend UI
• That the Streamlit app (st_app.py) interacts with the backend for viewing and CRUD operations on reservations.
• Custom UI widgets (clock and datepicker) under app/frontend, ensuring to note the authentication aspects (from app/frontend/__init__.py using streamlit_authenticator).
d. Automated Tasks:
• Component: Reminder Service (send_reminders.py)
• Component: Database Backup Service (sqlite_backup.sh)
• How these scripts periodically connect to the database and/or WhatsApp API as needed.
e. External Integrations:
Architectural Patterns and Design Principles
• Clearly separate the concerns:
• Frontend vs. Backend separation.
• Service layer (business logic) decoupled from route handlers.
• Security and verification via decorators.
• If applicable, show the periodic/scheduled nature of the reminder and backup scripts.