-
Notifications
You must be signed in to change notification settings - Fork 2
Ansari-WhatsApp Microservice #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Created a new test module `test_whatsapp_service.py` to implement integration tests for the WhatsApp service. - Added tests for service health, webhook verification, message processing, and phone number validation. - Implemented logging for test results with secure data masking. - Updated `uv.lock` to include `pytest` as a dependency for testing.
- Introduced `ALWAYS_RETURN_OK_TO_META` setting to control HTTP response for Meta webhooks. - Added `LOG_TEST_FILES_ONLY` setting to filter logs during testing. - Updated documentation to reflect new settings and their usage in CI/CD. - Refactored logger configuration to support module-specific logging and filtering.
… deprecated logger.catch usage
…p services - Added `AnsariClientMock` and `AnsariClientReal` for simulating and making real HTTP requests to the Ansari backend. - Introduced `MetaApiServiceMock` and `MetaApiServiceReal` for simulating and making real HTTP requests to the Meta WhatsApp API. - Created service provider functions to return appropriate client implementations based on configuration. - Enhanced configuration settings to toggle between mock and real services. - Updated tests to automatically switch between mock and real clients based on backend availability. - Improved logging for better traceability during API interactions.
…related components
…anager and related utilities - WhatsAppConversationManager for orchestrating conversation workflows, including user registration, message processing, and typing indicators. - time utilities for handling time-related calculations, including formatting time deltas and calculating time passed since the last message. - message splitter utility to handle long messages and ensure compliance with WhatsApp's character limits. - webhook parser for processing incoming WhatsApp webhook payloads from the Meta API.
… (due to GitHub Secrets)
…ks in configuration
- Created a comprehensive guide for configuring GitHub Secrets and understanding deployment workflows. - Documented required AWS credentials, IAM role ARNs, and SSM parameter paths. - Explained the deployment pipeline and environment variable injection process. - Added troubleshooting tips and best practices for deployments. feat: Add IAM role parameters access policy for SSM - Created a JSON policy file to allow the App Runner instance role to access SSM parameters for both staging and production environments. docs: Create GitHub Actions configuration guide - Developed a detailed guide on GitHub Actions CI/CD setup for the ansari-whatsapp repository. - Included sections on secrets and variables distribution, GitHub CLI commands, and workflow understanding. - Documented the ansari-whatsapp workflows and provided syntax for environment variables. docs: Update WhatsApp migration plan with deployment phases - Revised migration plan to reflect the completion of Phase 3 and readiness for Phase 4. - Added detailed documentation references for deployment and GitHub Actions setup. - Specified AWS resources to create and GitHub secrets to configure for deployment. fix: Set default values for WhatsApp chat retention settings - Updated WhatsAppSettings in config.py to set default values for chat retention hours and message age threshold.
…sing DEPLOYMENT_TYPE variable
- Consolidated SSM_ROOT staging and production paths into a single SSM_ROOT variable based on the environment. - Removed HOST and PORT from application settings in the migration plan. - Updated FastAPI app initialization in main.py to dynamically set host and reload options based on deployment type. - Added WhatsApp Web origin to CORS settings in config.py. - Created comprehensive documentation on GitHub Actions concepts, current setup, setup commands, and troubleshooting. - Added detailed steps for setting up GitHub Actions, including environment creation and secrets management.
- Updated `README.md` in GitHub Actions documentation by removing troubleshooting section. - Removed `troubleshooting.md` file from GitHub Actions documentation. - Revised `implementation_guide.md` to reflect updated architecture and service organization.
amrmelsayed
approved these changes
Oct 29, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 introduces the complete ansari-whatsapp microservice - a dedicated FastAPI service that handles WhatsApp Business API integration for the Ansari AI assistant. This represents the successful completion of Phase 3 of the WhatsApp migration plan, extracting WhatsApp functionality from the monolithic ansari-backend into an independent, scalable microservice.
What's Included:
/whatsapp/v2)Statistics:
Ansari-Backend's PR covering phases 1 & 2: ansari-project/ansari-backend#192
TOC:
src/ansari_whatsapp/app/)src/ansari_whatsapp/)src/ansari_whatsapp/utils/)tests/).github/workflows/)docs/)Migration Context
This microservice is the result of a comprehensive 4-phase migration plan to separate WhatsApp functionality from ansari-backend. For complete migration details, see the WhatsApp Migration Plan.
Architecture Transformation
Before Migration (Monolithic):
After Migration (Microservice Architecture):
Migration Phases Status
What's Included in This PR
1. Core Application (
src/ansari_whatsapp/app/)main.py(261 lines)GET /- Returns service status for App Runner health checksGET /whatsapp/v2- Validates Meta webhook subscriptionPOST /whatsapp/v2- Receives and processes incoming messagesKey Features:
2. Business Logic (
src/ansari_whatsapp/)services/whatsapp_conversation_manager.py(335 lines)presenters/whatsapp_message_formatter.py(280 lines)services/ansari_client_*.py(3 files, 510 lines total)services/meta_api_service_*.py(3 files, 420 lines total)services/service_provider.py(85 lines)3. Utilities (
src/ansari_whatsapp/utils/)config.py(141 lines)whatsapp_webhook_parser.py(125 lines)whatsapp_message_splitter.py(158 lines)app_logger.py(120 lines)time_utils.py(45 lines)language_utils.py(38 lines)general_helpers.py(51 lines)exceptions.py(28 lines)4. Comprehensive Test Suite (
tests/)test_whatsapp_service.py(315 lines)test_utils.py(84 lines)Test Features:
Test Configuration Options:
5. CI/CD Workflows (
.github/workflows/)perform-tests.ymldeploy-staging.ymldevelopbranchdeploy-production.ymlmainbranchWorkflow Features:
6. Documentation (
docs/)High-Level Design:
docs/hld/architecture.md- System architecture and component interactionsLow-Level Design:
docs/lld/implementation_guide.md- Detailed implementation notesAWS Deployment:
docs/lld/aws/deployment_guide.md- Complete deployment walkthroughdocs/lld/aws/aws-cli.md- All AWS CLI commands for setupdocs/lld/aws/github_actions_setup.md- GitHub secrets configurationdocs/lld/aws/README.md- AWS deployment overviewGitHub Actions:
docs/lld/github_actions/concepts.md- CI/CD concepts and patternsdocs/lld/github_actions/current_setup.md- Current workflow documentationdocs/lld/github_actions/setup_commands.md- Setup instructionsMigration Plan:
docs/whatsapp_migration_plan/migration_plan.md- Complete 4-phase migration planDevelopment:
CLAUDE.md- AI assistant instructions for code developmentdocs/CLAUDE.md- Additional development notesREADME.md- Quick start guide and overviewKey Features & Capabilities
1. WhatsApp Business API Integration
2. Message Processing
3. User & Thread Management
4. Backend Integration
5. Testing & Quality
6. Deployment & Operations
Technical Highlights
Clean Architecture
Modern Python
Error Handling
Performance
Environment Configuration
The service requires these environment variables (see
.env.examplefor full list):Required Variables
Optional Variables (Testing)
Testing & Quality Assurance
Test Coverage
Running Tests Locally
CI/CD Testing
Tests run automatically on every push/PR via GitHub Actions:
Development Workflow
Local Development
Clone repository:
Install uv and project dependencies:
Configure environment:
.env.exampleto.envStart the service:
Expose webhook (for Meta testing):
Testing with Meta Webhooks
/whatsapp/v2Code Quality Tools
Benefits of Microservice Architecture
1. Scalability
2. Maintainability
3. Development Velocity
4. Reliability
5. Security
Migration Impact
What Was Removed from ansari-backend
main_whatsapp.py(205 lines) - Old webhook implementationwhatsapp_presenter.py(862 lines) - Message formatting logicWhat Was Added to ansari-backend
whatsapp_router.py(271 lines)Net Result
Next Steps: Phase 4 Deployment
This PR completes Phase 3 (implementation). Next is Phase 4 (AWS deployment):
4.1 AWS Resources Setup
ansari-whatsapp4.2 GitHub Configuration
4.3 Code & Configuration
.github/workflows/deploy-staging.ymlwith correct values.github/workflows/deploy-production.ymlwith correct values4.4 Environment Variables Configuration
Staging SSM Parameters:
Production SSM Parameters:
4.5 Deployment Validation & Go-Live
Detailed instructions: See
docs/lld/aws/deployment_guide.mdBreaking Changes & Migration Notes
For Existing Deployments
If you have an existing WhatsApp integration in ansari-backend:
Meta Webhook URL Changes
https://staging-api.ansari.chat/whatsapp/v1https://api.ansari.chat/whatsapp/v1https://staging-whatsapp.ansari.chat/whatsapp/v2https://whatsapp.ansari.chat/whatsapp/v2Environment Variable Changes
WhatsApp credentials move from backend to this service:
META_*variablesMETA_*variables +BACKEND_SERVER_URLNo Data Migration Required
Documentation
Quick Start
README.md- Setup instructions and overviewCLAUDE.md- Development guidelines for AI assistantsArchitecture
docs/hld/architecture.md- High-level system designdocs/lld/implementation_guide.md- Implementation detailsDeployment
docs/lld/aws/deployment_guide.md- Complete AWS deployment guidedocs/lld/aws/aws-cli.md- AWS CLI commands referencedocs/lld/aws/github_actions_setup.md- CI/CD configurationMigration
docs/whatsapp_migration_plan/migration_plan.md- Full 4-phase planTesting
tests/README.md- Test suite documentationQuestions?
For Migration Questions:
For Deployment Questions:
docs/lld/aws/deployment_guide.mddocs/lld/aws/github_actions_setup.mdFor Development Questions:
CLAUDE.mdin this repositorytests/README.mdfor testing setupFor Architecture Questions:
docs/hld/architecture.mddocs/lld/implementation_guide.mdReady for Review: This PR represents 2+ months of development work to extract WhatsApp functionality into a production-ready microservice. All code is tested, documented, and ready for AWS deployment (Phase 4).