WhatsApp Migration - Backend Separation #192
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements Phases 1 & 2 of the WhatsApp migration plan, transforming ansari-backend to support the new
ansari-whatsappmicroservice architecture. The migration extracts WhatsApp webhook handling into a dedicated service while maintaining clean API endpoints in the backend for message processing and user management.Key Changes:
/whatsapp/v2/*) for the microservicemain_whatsapp.py,whatsapp_presenter.py)Ansari-Whatsapp's PR covering phase 3: ansari-project/ansari-whatsapp#1
TOC:
Migration Context
This PR is part of a comprehensive 4-phase migration plan to separate WhatsApp functionality from ansari-backend into a dedicated microservice. For complete details, see the WhatsApp Migration Plan.
Architecture Overview
Before Migration:
After Migration (Current State):
Migration Phases Status
Detailed Changes
Commit 1: Phase 1 Implementation (6dc24fa)
Created new WhatsApp API router with 6 endpoints
Added
src/ansari/app/whatsapp_api_router.pywith comprehensive API endpoints for ansari-whatsapp microservice:POST /whatsapp/v2/users/register- Register new WhatsApp usersphone_num,preferred_languageuser_idon successGET /whatsapp/v2/users/exists- Check if user existsphone_num{"exists": boolean}POST /whatsapp/v2/threads- Create new message threadphone_num,titlethread_idGET /whatsapp/v2/threads/last- Get last active threadphone_numthread_id,last_message_timeGET /whatsapp/v2/threads/{thread_id}/history- Get thread historythread_idphone_numPOST /whatsapp/v2/messages/process- Process message with AI (streaming)phone_num,thread_id,messageFiles changed:
src/ansari/app/whatsapp_api_router.py(306 lines)src/ansari/app/main_api.pyto include WhatsApp router.gitignoreCommit 2: Router Improvements (aff16aa)
Fixed database calls and added health endpoint
GET /) for App Runner health checksCLAUDE.mdwith WhatsApp migration documentationFiles changed:
src/ansari/app/whatsapp_api_router.py(improved db compatibility)src/ansari/app/main_api.py(added root endpoint)CLAUDE.md(documentation)Commit 3: Merge from develop (0f31e97)
Merged latest changes from develop branch
Standard merge commit to keep whatsapp-migration branch up-to-date with develop.
Commit 4: Phase 2 Implementation (6bffe3c)
Removed legacy WhatsApp implementation
Major cleanup removing 1,108 lines of legacy code:
Deleted
src/ansari/app/main_whatsapp.py(205 lines)/whatsapp/v2)ansari-whatsappmicroserviceDeleted
src/ansari/presenters/whatsapp_presenter.py(862 lines)ansari-whatsapp/presenters/whatsapp_message_formatter.pyRemoved location tracking endpoint
PUT /whatsapp/v2/users/locationendpointWhatsAppLocationRequestmodelCleaned up configuration
src/ansari/config.py:META_WEBHOOK_VERIFY_TOKENMETA_ACCESS_TOKEN_FROM_SYS_USERMETA_BUSINESS_PHONE_NUMBER_IDMETA_API_VERSIONFiles changed:
src/ansari/app/main_whatsapp.py(-205 lines)src/ansari/presenters/whatsapp_presenter.py(-862 lines)src/ansari/app/whatsapp_api_router.py(-35 lines: location endpoint)src/ansari/config.py(-6 lines: removed WhatsApp config)Commit 5: V2 Endpoint Naming & Reorganization (42ea141)
Updated API structure and naming conventions
/whatsapp/v2/*naming conventionsrc/ansari/app/whatsapp_api_router.pysrc/ansari/routers/whatsapp_router.pysrc/ansari/app/main_api.py.env.exampleto reflect changesFiles changed:
whatsapp_api_router.py→whatsapp_router.py(moved torouters/)src/ansari/app/main_api.py.env.exampleBenefits
1. Microservice Architecture
2. Code Quality
3. Maintainability
4. Privacy & Security
5. Development Experience
Testing
Backend Tests
The WhatsApp API endpoints are covered by comprehensive tests:
ansari-backend/tests/unit/test_whatsapp_api_endpoints.pyMicroservice Tests
The ansari-whatsapp service has full test coverage:
ansari-whatsapp/tests/test_whatsapp_service.pyIntegration Tests
Both services tested together to verify:
Note: Tests use mock clients to avoid external dependencies, making CI/CD reliable and fast.
Migration Progress
Completed (This PR)
Already Completed (ansari-whatsapp repo)
Next Steps (Post-Merge)
Breaking Changes
For Backend Deployments
GET/POST /whatsapp/v2webhooks (moved to ansari-whatsapp)PUT /whatsapp/v2/users/location(privacy improvement)For WhatsApp Integration
https://backend.ansari.chat/whatsapp/v2https://whatsapp.ansari.chat/whatsapp/v2(after Phase 4 deployment)Migration Path
Note: Both services should be deployed before updating Meta webhook URL to avoid downtime.
Configuration Changes
Backend (.env)
Removed variables (no longer needed):
Microservice (.env)
New service requires (see ansari-whatsapp
.env.example):Documentation References
ansari-whatsapp/docs/aws/deployment_guide.mdansari-whatsapp/docs/aws/github_actions_setup.mdansari-whatsapp/docs/aws/aws-cli.mdQuestions?
For questions about:
ansari-whatsapp/docs/aws/deployment_guide.mdCLAUDE.mdin both repositories