π‘ WhatsApp Webhook Documentation
Introduction
The WhatsApp webhook is the central component that receives and processes real-time events from the WhatsApp Business API. This endpoint handles all types of interactions, including incoming messages, status updates, template events, and more.
π Table of Contents
- Webhook Configuration
- Webhook Verification
- Event Structure
- Supported Message Types
- Status Handling
- Template Events
- Security
- Payload Examples
- Event Logging
Webhook Configuration
To configure the webhook in your Meta Developers application:
- Required environment variables:
WHATSAPP_VERIFY_TOKEN="your_secret_token" WHATSAPP_API_URL="https://graph.facebook.com" WHATSAPP_API_VERSION="v18.0" - Register the endpoint:
- URL: https://yourdomain.com/whatsapp/webhook
- Verification field: hub.verify_token
- Events to subscribe:
- Messages
- Message statuses
- Templates
Webhook Verification
When WhatsApp sends a GET request to verify the webhook:
GET /whatsapp/webhook?hub.mode=subscribe&hub.challenge=123456789&hub.verify_token=your_secret_token
The system responds with the hub.challenge if the token is valid:
return response()->make($request->input('hub_challenge'), 200);
Event Structure
All POST events have this basic structure:
{
"entry": [
{
"id": "WEBHOOK_ID",
"changes": [
{
"value": {
// Event-specific data
},
"field": "messages" // or "message_template"
}
]
}
]
}
Supported Message Types
-
Text Messages
- Type: text
- Processing:
- Extracts text content
- Stores in messages table
- Fires TextMessageReceived event
-
Media Messages
- Types: image, audio, video, document, sticker
- Processing:
- Downloads file from WhatsApp
- Stores in file system
- Saves reference in media_files
- Fires MediaMessageReceived event
-
Interactive Messages
- Types: interactive (buttons or lists)
- Processing:
- Extracts user selection
- Stores as INTERACTIVE type
- Fires InteractiveMessageReceived event
-
Locations
- Type: location
- Processing:
- Saves coordinates and place name
- Fires LocationMessageReceived event
-
Shared Contacts
- Type: contacts
- Processing:
- Extracts name, phones, and emails
- Stores as CONTACT type
- Fires ContactMessageReceived event
-
Reactions
- Type: reaction
- Processing:
- Links to original message
- Stores the emoji
- Fires ReactionReceived event
-
System Messages
- Type: system
- Cases:
- User number change
- Account updates
-
Unsupported Message Handling
- Type: Unsupported
- Cases:
- Circular videos
- Poll messages
- Event messages
Status Handling
Updates the status of sent messages:
Status
- delivered - "Updates delivered_at and fires MessageDelivered"
- read - Updates read_at and fires MessageRead
- failed - Updates failed_at and fires MessageFailed
- opt-out - Marks contact as marketing opt-out (code 131050)
Template Events
Handles template lifecycle:
Event
- APPROVED - Updates status and creates new version
- REJECTED - Records rejection reason
- PENDING - Marks as pending review
- CREATE - Creates new template in database
- UPDATE - Updates template and creates new version
- DELETE - Soft deletes template
- DISABLE - Disables template and versions
β€οΈ Support
If you find this project useful, consider supporting its development:
π License
MIT License - See LICENSE for details