Repository navigation
Conversation
…stence - Add Agent.serve_whatsapp() and Server.serve_whatsapp() - Implement SQLiteSessionStore with WAL mode, session timeout, and message idempotency - Implement WhatsAppServer with HMAC-SHA256 signature verification and background task processing - Implement MetaWhatsAppClient with message chunking and read receipts - Implement MetaDiscoveryClient for automated Phone Number ID discovery and 3-tier webhook sync (adapted from WAVoice) - Add 17 unit and integration tests covering security, idempotency, session isolation, and auto-discovery
Author
|
LMK if you need any help in testing the PR via Meta a/c. Happy to hop on a call as well. |
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
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
Adds native Meta WhatsApp Cloud API serving capabilities to SDialog agents via
Agent.serve_whatsapp()andServer.serve_whatsapp(), as requested in #133.This implementation allows users to deploy and converse with SDialog agents directly over WhatsApp with production-grade reliability, multi-user session isolation, and zero-friction automated onboarding.
Closes #133
Key Features & Architecture
1. Developer Interface
Mirrors SDialog's existing
Agent.serve()idiom:Parameters fall back to standard environment variables (
WHATSAPP_PHONE_NUMBER_ID,WHATSAPP_ACCESS_TOKEN,WHATSAPP_APP_SECRET,WHATSAPP_VERIFY_TOKEN).2. Multi-User Session Isolation via SQLite
wa_id) has isolated state stored in a local SQLite database (whatsapp_sessions.db).PRAGMA journal_mode = WAL;) with a 5000ms busy timeout to prevent database locks under concurrent traffic.session_timeout, default 30 mins) or manual reset via slash commands (/reset,/clear,/restart).Agent.get_state()/Agent.set_state()API contract, with an internal message history bridge for immediate use.3. Webhook Architecture & Meta SLA Compliance
BackgroundTask+asyncio.to_thread._user_locks[wa_id]) so messages from the same user are processed in chronological order (handling "double-texting"), while different users run concurrently.4. Security & Edge Case Handling
X-Hub-Signature-256using constant-time comparison (hmac.compare_digest) on raw payload bytes; rejects spoofed requests with HTTP 401.wamid(WhatsApp Message ID) in aprocessed_messagestable to safely suppress duplicate webhook retries from Meta without re-invoking the LLM.sent,delivered,read) safely without crashing.5. Automated Onboarding (Adapted from WAVoice)
debug_token) to discoverapp_idandwaba_id.phone_number_idso users don't need to look up internal Meta IDs manually.tunnel=True.Provenance & Attribution
The auto-discovery and 3-tier webhook synchronization logic is adapted from WAVoice (Bhakt, 2026, DOI: 10.5281/zenodo.23537489), released under the MIT License, with full SPDX attribution headers included in
src/sdialog/whatsapp/discovery.py.Verification & Testing
tests/test_whatsapp.pywith 17 tests covering config validation, SQLite session persistence, TTL expiration, idempotency, HMAC validation, multi-user isolation, text chunking, and auto-discovery.tests/test_server.pycontinue to pass (47/47 tests total).