This service handles WhatsApp integration for the Ansari backend, providing a dedicated service for processing WhatsApp messages and communicating with the main Ansari backend.
The Ansari WhatsApp service is designed as a separate microservice that:
- Receives webhook events from the WhatsApp Business API
- Processes incoming messages
- Communicates with the main Ansari backend API
- Sends responses back to WhatsApp users
By separating the WhatsApp functionality into its own service, we can:
- Scale the WhatsApp service independently
- Simplify the main Ansari backend codebase
- Make deployments and updates easier
- Improve testing and maintenance
- Python 3.8 or higher
- Access to the WhatsApp Business API
- A running instance of the Ansari backend API
-
Clone the repository:
git clone <repository-url> cd ansari-whatsapp -
Run the setup script:
- On Linux/Mac:
./setup.sh - On Windows:
setup.bat
- On Linux/Mac:
-
Edit the
.envfile with your configuration settings:# Update these with your values META_BUSINESS_PHONE_NUMBER_ID=your_phone_number_id META_ACCESS_TOKEN_FROM_SYS_USER=your_access_token META_WEBHOOK_VERIFY_TOKEN=your_verify_token BACKEND_SERVER_URL=http://your-ansari-backend:8000
python -m src.ansari_whatsapp.app.main
This will start the service on port 8001 (or the port specified in your .env file).
To build and run the Docker container:
docker build -t ansari-whatsapp .
docker run -p 8001:8001 --env-file .env ansari-whatsapp
The service exposes the following endpoints:
GET /: Health check endpointGET /whatsapp/v2: Webhook verification endpoint for WhatsAppPOST /whatsapp/v2: Main webhook endpoint for receiving WhatsApp messages
- Quick Start: Read this README for setup and basic usage
- Development Guide: See
CLAUDE.mdfor detailed development instructions, testing, and commands - Architecture Overview: See
docs/hld/architecture.mdfor system design and data flow - Implementation Details: See
docs/lld/implementation_guide.mdfor technical deep-dive
- AWS Setup: See
docs/lld/aws/README.mdfor complete deployment walkthrough - GitHub Actions: See
docs/lld/github_actions/README.mdfor CI/CD configuration
- Migration Plan: See
docs/whatsapp_migration_plan/migration_plan.mdfor the 4-phase migration from monolith to microservice
The service follows a clean architecture pattern:
app/: FastAPI application with webhook endpointsservices/: Business logic layer (conversation manager, API clients with mock support)presenters/: Message formatting layer (WhatsApp-specific formatting, RTL support)utils/: Utility modules (config, logging, parsing, splitting)
The WhatsApp service communicates with the Ansari backend via 6 API endpoints:
- POST /whatsapp/v2/users/register - User registration
- GET /whatsapp/v2/users/exists - Check user existence
- POST /whatsapp/v2/threads - Create new threads
- GET /whatsapp/v2/threads/last - Get last thread info
- GET /whatsapp/v2/threads/{thread_id}/history - Get thread history
- POST /whatsapp/v2/messages/process - Process messages (streaming)
| Variable | Description | Default |
|---|---|---|
| HOST | Host to bind the server to | 0.0.0.0 |
| PORT | Port to run the server on | 8001 |
| BACKEND_SERVER_URL | URL of the Ansari backend API | http://localhost:8000 |
| META_API_VERSION | WhatsApp API version | v21.0 |
| META_BUSINESS_PHONE_NUMBER_ID | Your WhatsApp business phone number ID | (required) |
| META_ACCESS_TOKEN_FROM_SYS_USER | Access token for WhatsApp API | (required) |
| META_WEBHOOK_VERIFY_TOKEN | Verify token for WhatsApp webhook | (required) |
| WHATSAPP_CHAT_RETENTION_HOURS | Hours to retain chat history | 3 |
| DEV_MODE | Enable development mode | false |
The project includes comprehensive integration tests for WhatsApp webhook endpoints.
# Run all tests with detailed output
pytest tests/ -v
# Run with logs displayed
pytest tests/ -v -s
# Run only integration tests
pytest tests/ -m integration -vTests require environment variables to be configured. Key settings:
- MOCK_ANSARI_CLIENT: Set to
Trueto use mock client (no backend needed),Falseto use real backend - BACKEND_SERVER_URL: URL of the backend service (required if
MOCK_ANSARI_CLIENT=False) - Test-specific variables:
WHATSAPP_DEV_PHONE_NUM,WHATSAPP_DEV_MESSAGE_ID, etc.
Important: If MOCK_ANSARI_CLIENT=False and the backend is not available, tests will fail with clear instructions on how to fix the issue.
See tests/README.md for detailed testing documentation and troubleshooting.
Logs are stored in the logs/ directory. Check these logs for any errors or issues.
Common issues:
- WhatsApp API connection problems
- Missing or invalid environment variables
- Connection issues with the Ansari backend
- Test failures due to backend availability (see
tests/README.mdfor solutions)