Integration Architecture
The WhatsApp Business OS connects to the WhatsApp Cloud API (Meta) to turn generated config packs into live, two-way WhatsApp conversations. The architecture has four layers:
Meta WhatsApp Cloud API
Transport: send/receive messages, template approvals, status webhooks
Webhook Layer (whatsappEventHub)
Verify signature, verify-token handshake, route by event type
Resolution Layer
whatsappTenantResolver + whatsappPersonaResolver + whatsappSessionManager
WhatsApp OS Core
Persona detection, scoring, evolution, regeneration, activity engine
Persistence
WhatsAppConversationLog + WhatsAppPersonaProfile + WhatsAppConfig
The OS generates and optimizes the configuration (greeting, templates, quick replies); the WhatsApp API handles the transport (sending/receiving actual messages). This separation lets the OS stay autonomous while Meta owns delivery.
Credentials & Secrets
The following secrets are already provisioned in the app and power the integration:
| Secret | Purpose |
|---|---|
| WHATSAPP_BUSINESS_TOKEN | Bearer token for outbound API calls + webhook HMAC secret |
| WHATSAPP_PHONE_NUMBER_ID | The sender phone number ID (path param for /messages) |
| WHATSAPP_VERIFY_TOKEN | Shared secret for the webhook subscription handshake |
| TWILIO_WHATSAPP_FROM | Twilio WhatsApp sender (fallback channel) |
The verify token must match exactly between the Meta app dashboard and the WHATSAPP_VERIFY_TOKEN secret. Webhook subscription fails silently if they differ.
Inbound Webhook Flow
Meta pushes message + status events to your webhook endpoint. The flow is:
Meta → Webhook
POST to /functions/whatsappEventHub with X-Hub-Signature-256 header.
Verify Signature
Recompute HMAC-SHA256 of the raw body using WHATSAPP_BUSINESS_TOKEN; reject on mismatch.
Verify Token (GET)
Meta calls the same endpoint with hub.verify_token on subscription — echo hub.challenge back.
Route by Type
whatsappEventHub classifies: message, status, template_status, reaction.
Resolve Tenant & Persona
whatsappTenantResolver + whatsappPersonaResolver map the phone number to a tenant + WhatsAppPersonaProfile.
Persist + Emit
Store in WhatsAppConversationLog; emit an event to the Activity Engine for suggestion generation.
Auto-Reply
If a quick-reply keyword matches, the session manager sends the mapped response back via the API.
Required subscription fields: messages message_template_status_update message_template_quality_update phone_number_quality_update account_update
Outbound Messaging
Outbound messages are sent via the Cloud API using the configured phone number ID. Two message types:
Template Messages
Used for proactive outreach (requires Meta-approved template). Maps directly to the templates array in each WhatsAppConfig version.
Text Messages
Free-form replies within the 24-hour customer service window. Used by the auto-reply quick-reply matcher.
{
"messaging_product": "whatsapp",
"to": "<customer E.164>",
"type": "template",
"template": {
"name": "application_ack",
"language": { "code": "en" },
"components": [
{ "type": "body", "parameters": [
{ "type": "text", "text": "Maria" },
{ "type": "text", "text": "Housemaid" }
]}
]
}
}The WhatsApp OS already generates the name, language, and body for every template — so sending is just a parameter substitution. Component parameters map to {{1}}, {{2}} placeholders.
Template Management Lifecycle
WhatsApp message templates must be submitted to Meta for approval before use. The OS produces the template definitions; the integration handles the lifecycle:
Generate
whatsappOsEngine builds template objects per persona category (name, category, language, body).
Submit to Meta
POST /v21.0/{waba_id}/message_templates with each template's name, category, language, and components.
Await Approval
Meta reviews (minutes to hours). Status flows: PENDING → APPROVED / REJECTED.
Status Sync
message_template_status_update webhooks update the template status; store on the WhatsAppConfig.
Send
Only APPROVED templates can be sent. Rejected templates need a content fix + resubmit.
Quality Monitoring
message_template_quality_update webhooks flag templates paused by Meta for low quality — feed back into regeneration.
OS Layer Mapping
Each OS component maps to a concrete WhatsApp API capability:
| OS Component | WhatsApp API Capability |
|---|---|
| Greeting Message | Business greeting (set via Meta dashboard) |
| Away Message | Auto-reply outside hours (Meta dashboard) |
| Quick Replies | Interactive button messages (type: button) |
| Labels | Chat labels (organize inbox, no API send) |
| Catalog | WhatsApp Commerce catalog items |
| Templates | Message templates (POST /message_templates) |
| Autonomy Brain | Decides when to regenerate / suggest |
| Activity Engine | Consumes message + status webhooks |
Security & Rate Limits
- Webhook signature verification — every inbound POST is HMAC-SHA256 verified against
WHATSAPP_BUSINESS_TOKEN; unverified requests are rejected with 401. - Verify token handshake — the GET subscription handshake only succeeds when
hub.verify_tokenmatchesWHATSAPP_VERIFY_TOKEN. - 24-hour window — free-form text replies only within 24h of the last inbound customer message; outside that, only approved templates are allowed.
- Rate limits — Cloud API allows 80 messages/sec per phone number; burst protection backs off automatically.
- Tenant isolation — inbound messages are resolved to a tenant via the recipient phone number; RLS ensures one tenant never sees another's conversations.
- PII handling — message bodies are stored in WhatsAppConversationLog but never logged to the activity engine in plaintext; only aggregate stats feed the brain.
Implementation Roadmap
P1 — Connect
Complete- Secrets provisioned
- whatsappEventHub webhook endpoint live
- Verify-token handshake wired
P2 — Send
Complete- whatsappSessionManager sends templates
- Quick-reply auto-reply matcher
- Outbound status logging
P3 — Templates
In Progress- Bulk submit OS-generated templates to Meta
- Sync approval status back to configs
- Quality-update feedback loop
P4 — Intelligence
Planned- Brain-driven auto-send of optimal templates
- Friction-triggered proactive check-ins
- Persona-aware escalation routing
P5 — Scale
Planned- Per-tenant phone number provisioning
- Shared WABA multi-brand routing
- Conversation analytics export to BI