Skip to main content

WhatsApp Channel

The WhatsApp channel enables ZeroClaw to communicate via WhatsApp Business Cloud API using Meta’s official platform with webhook-based message delivery.

Overview

  • Channel Name: whatsapp
  • Transport: Webhook (push-based)
  • Authentication: Access token (Meta Business)
  • Public Port Required: Yes (HTTPS webhook endpoint)
  • Supports: Text messages, phone number allowlist

Configuration

Required Settings

Complete Configuration

Environment Variables

Setup

Prerequisites

  1. Meta Business Account: Create at business.facebook.com
  2. WhatsApp Business App: Register in Meta Business Settings
  3. Phone Number: Add and verify a business phone number
  4. Public HTTPS Endpoint: Required for webhook callbacks

Getting Access Token

  1. Go to Meta for Developers
  2. Create new app (Business type)
  3. Add “WhatsApp” product
  4. Go to API Settings
  5. Generate Temporary Access Token (24 hours) or Permanent Token:
    • Navigate to System Users (Business Settings)
    • Create system user
    • Assign WhatsApp permissions
    • Generate token

Getting Phone Number ID

  1. In WhatsApp Business settings
  2. Find your phone number
  3. Copy the Phone Number ID (not the actual phone number)

Webhook Setup

  1. In WhatsApp product settings, go to Configuration
  2. Set Callback URL: https://your-domain.com/whatsapp
  3. Set Verify Token: Choose a secure random string
  4. Subscribe to webhook fields:
    • messages (required)
  5. Verify webhook (Meta will send GET request)
  6. Start receiving POST requests
Webhook Verification: Meta sends:
ZeroClaw responds with hub.challenge if hub.verify_token matches.

Features

Webhook Mode (Push-Based)

Unlike polling channels, WhatsApp uses webhooks: Incoming Message Flow:
  1. User sends WhatsApp message to your business number
  2. Meta WhatsApp API sends POST request to your webhook endpoint
  3. ZeroClaw validates webhook signature (if configured)
  4. Parses webhook payload
  5. Checks allowlist
  6. Converts to ChannelMessage
  7. Sends to agent for processing
No Active Polling:
  • The listen() method keeps channel alive but doesn’t poll
  • Actual message delivery happens via gateway webhook handler

Phone Number Allowlist

E.164 Format Required

All phone numbers must use E.164 international format:
Examples:
  • US: +1234567890
  • UK: +447911123456
  • India: +919876543210

Configuration

Allow All (testing only):
Specific Numbers:
Automatic Normalization: Incoming numbers without + prefix are normalized:

Message Type Support

Supported (Current)

  • Text messages: Full support

Unsupported (Skipped)

  • Images
  • Audio/Voice
  • Video
  • Documents
  • Stickers
  • Locations
  • Contacts
  • Reactions
Future versions will add multimedia support.

Webhook Payload Parsing

Meta sends nested JSON structure:
Extraction:
  1. Navigate: entry[].changes[].value.messages[]
  2. Get sender: from field
  3. Normalize: Add + prefix if missing
  4. Check allowlist
  5. Extract text: text.body
  6. Get timestamp: timestamp (Unix seconds)
  7. Create ChannelMessage

HTTPS Security

All API calls enforce HTTPS:
Checked Endpoints:
  • Message send: https://graph.facebook.com/v18.0/{phone_number_id}/messages
  • Health check: https://graph.facebook.com/v18.0/{phone_number_id}

Implementation Details

Source Location

src/channels/whatsapp.rs (1140 lines)

Key Components

WhatsAppChannel Struct

Gateway Integration

Webhook handler in gateway:

API Endpoints Used

Message Send Format

Note: Recipient number has + prefix stripped for API.

Error Handling

API errors are sanitized:
Removes:
  • Access tokens
  • Sensitive headers
  • Internal details

Common Errors

Webhook Verification Failed

Cause: verify_token doesn’t match Meta’s expected value Solution:
  1. Check token in Meta webhook settings
  2. Update config:
  3. Restart daemon

Unauthorized Number

Solution:

API Error 401

Cause: Invalid or expired access token Solution:
  1. Generate new permanent token (System User method)
  2. Update config:

API Error 403

Cause: Insufficient permissions Solution:
  • Verify WhatsApp Business Account is active
  • Check phone number is verified
  • Ensure System User has whatsapp_business_messaging permission

Webhook Not Receiving Messages

Check Webhook URL:
Check Firewall: Ensure port 443 (HTTPS) is open Check Logs:
Look for:
  • WhatsApp channel active (webhook mode).
  • WhatsApp: ignoring message from unauthorized number:
  • WhatsApp webhook verified successfully
Test Webhook: Send test message from your phone to business number.

Best Practices

  1. Use Permanent Token: Generate via System User, not temporary 24h token
  2. Secure Verify Token: Use long random string (32+ chars)
  3. HTTPS Required: Never use HTTP for webhook
  4. Allowlist Numbers: Start with *, then restrict to known users
  5. Monitor Quota: Meta has message limits (check Business Manager)
  6. Phone Number Verification: Ensure business number is verified
  7. Error Monitoring: Watch logs for API errors

Troubleshooting

No Messages Received

1. Check Gateway:
2. Check Webhook Subscription: In Meta settings, verify messages field is subscribed. 3. Check Allowlist:
4. Check Phone Format: Must be E.164: +1234567890

Messages Not Sending

1. Verify Access Token:
2. Check Recipient Number: Must be registered WhatsApp user. 3. Check Message Limits: Meta enforces rate limits and daily quotas. 4. Test with API Explorer: Use Meta’s Graph API Explorer to test send.

Performance

Message Latency

Inbound:
  • Webhook delivery: <1 second (Meta → your server)
  • Processing: Depends on agent/provider
  • Total: Usually <5 seconds
Outbound:
  • API call: <500ms (your server → Meta)
  • WhatsApp delivery: <2 seconds (Meta → recipient)
  • Total: Usually <3 seconds

Scalability

Webhook Concurrency:
  • Gateway handles multiple concurrent webhooks
  • Each message processed independently
Rate Limits:
  • Meta enforces per-number limits
  • Check Business Manager for current quota
  • Tier 1 (default): 1,000 messages/day
  • Higher tiers: Request from Meta

Security

Access Token Protection

  • Never log access tokens
  • Sanitized from error messages
  • Store in config with restricted permissions: chmod 600 config.toml

Webhook Verification

GET Request (initial setup):
POST Request (message delivery):
  • Verify hub.verify_token if signature checking is implemented
  • Check allowed_numbers allowlist

HTTPS Enforcement

  • All API calls use HTTPS
  • Webhook endpoint must be HTTPS
  • Certificate validation enabled

Comparison with WhatsApp Web Mode

ZeroClaw also supports WhatsApp Web mode (requires --features whatsapp-web): Use Cloud API for production, Web mode for personal/testing.

See Also