A webhook is a method that allows WAMM.chat to provide real-time information to your application by automatically sending an HTTP POST request to a specific URL (your server) when a certain event occurs. In the context of messengers and chatbots, such events include receiving a new message, sending a message, status changes, etc. This eliminates the need for constant API polling and enables instant reactions to new messages. It is ideal for creating interactive services and chatbots that must process messages without delays.
This approach, often called a "reverse API" or "push API," is fundamentally different from traditional polling, where your server constantly queries the server's API with questions like: "Are there new messages? How about now? And now?". Webhooks save computational resources, reduce traffic, and ensure an instant response.
Setting Up the Webhook URL
To receive messages via Webhook, you need to specify your server's URL in the section Settings > Select Communication Channel > "Data and API" Tab > Webhook URL. POST requests with data about incoming and outgoing messages in JSON format will be sent to this URL.
Your Webhook handler URL must be accessible on the Internet and respond with HTTP 200 to POST requests. The request processing time on your side should not exceed 10 seconds. Messages via Webhook are sent sequentially, maintaining the order of events. If your server does not respond or returns an error code, WAMM.chat will retry the requests up to 5 more times, increasing the interval between attempts.
Message Data Format
{
"tip": "msg",
"msg_data": {
"msg_id": 1234567,
"qoute_msg_id": null,
"from_api": 0,
"from_me": 0,
"phone_acc": "79XXXXXXXXX",
"phone": "79001234567",
"chat_name": "Ivan Petrov",
"tip_msg": "textMessage",
"msg_text": "Good afternoon, I'm interested in your offer",
"msg_link": null,
"date_ins": "2023-05-24 12:35:29",
"state": "received",
"senderId": null,
"senderName": null
}
} Message Field Descriptions
| Field | Description |
|---|---|
| tip | Event type: "msg" - message, "msg_state" - message status |
| msg_id | Unique message identifier |
| qoute_msg_id | ID of the quoted message (if the message is a reply) |
| from_api | Flag indicating sending via API: 0 - no, 1 - yes |
| from_me | Message direction: 0 - incoming, 1 - outgoing |
| phone_acc | Phone number connected to WAMM.chat |
| phone | Phone number or chat ID of the recipient/sender |
| chat_name | Contact name or chat title |
| tip_msg | Message type: textMessage (text), documentMessage (file), imageMessage (image), audioMessage (voice/audio), videoMessage (video), location (location), file_link (file/image/voice) |
| msg_text | Message text |
| msg_link | File link (if applicable) |
| date_ins | Date and time of sending/receiving |
| state | Message status (sending, viewed, received, delivered, and others) |
| senderId | Sender ID or phone number (for group chats) |
| senderName | Sender name (for group chats) |
Message Status Data Format
{
"tip": "msg_state",
"msg_data": {
"msg_id": "1234567",
"date_upd": "2023-05-24 22:46:03",
"state": "delivered"
}
} Message Status Field Descriptions
| Field | Description |
|---|---|
| msg_id | Message number |
| date_upd | Date the status was received |
| state | Message status (sending, viewed, received, delivered, deleted, not sent, and others) |
Example of Webhook Processing in PHP
// Get data from POST request
$input = file_get_contents('php://input');
$data = json_decode($input, true);
// Check if data was successfully decoded
if ($data === null) {
http_response_code(400); // Bad Request
exit('Invalid JSON data');
}
// Always return status 200 so WAMM.chat knows the request was processed successfully
header("HTTP/1.0 200 OK");
echo 'OK';
// Send a response to the WAMM.chat server and then process the request for as long as needed
fastcgi_finish_request();
// Log for debugging (optional)
file_put_contents('webhook_log.txt', date('Y-m-d H:i:s') . " - " . $input . PHP_EOL, FILE_APPEND);
// Process the message
if (isset($data['tip'])) {
switch ($data['tip']) {
case 'msg':
// Process new message
processChatMessage($data['msg_data']);
break;
case 'msg_state':
// Process message status
processMessageStatus($data['msg_data']);
break;
default:
// Unknown event type
logUnknownEvent($data);
break;
}
}
// Function to process a new message
function processChatMessage($message) {
// Check if the message is incoming or outgoing
if ($message['from_me'] == 0) {
// Incoming message (from client)
$phone = $message['phone'];
$text = $message['msg_text'];
$msgId = $message['msg_id'];
// Example: saving to database
// saveMessageToDatabase($phone, $text, $msgId, 'incoming');
// Example: automatic reply to specific messages
if (strtolower($text) === 'hello') {
// Send automatic reply via WAMM.chat API
// sendReply($phone, 'Hello! How can I help you?');
}
} else {
// Outgoing message (from operator)
// saveMessageToDatabase($message['phone'], $message['msg_text'], $message['msg_id'], 'outgoing');
}
}
// Function to process message status
function processMessageStatus($status) {
$msgId = $status['msg_id'];
$state = $status['state'];
$dateUpd = $status['date_upd'];
// Example: update message status in the database
// updateMessageStatus($msgId, $state, $dateUpd);
// Log status change
$logMessage = "Message ID: $msgId, new status: $state, update date: $dateUpd";
// logStatus($logMessage);
}
// Function to log unknown events
function logUnknownEvent($data) {
// Record unknown event to a log for analysis
// logToFile('unknown_events.log', json_encode($data));
}
Implementation Tips
- Always verify the authenticity of requests, for example, by checking WAMM.chat server IP addresses.
- Process each request quickly (no more than 10 seconds) to avoid timeouts.
- If processing requires a long time, perform main tasks asynchronously (via queues).
- Set up monitoring for your webhook handler to track its health.
Notes
Messages are sent to the webhook URL only when there is an active connection to the WAMM.chat service. If your webhook is unavailable and messages were not delivered, you can always retrieve them using the API methods msg_get_last or msg_get.