Action
uncanny-automator
automator_whatsapp_webhook_message_received
Fires when a new message is received from WhatsApp through the webhook integration, passing message arguments.
add_action( 'automator_whatsapp_webhook_message_received', $callback, 10, 1 );
Description
Fires after a new WhatsApp message is received via webhook. Developers can use this hook to access message details, sender information, and the full webhook response. It allows for custom logic, data processing, or triggering further actions based on incoming WhatsApp messages before they are parsed by the system.
Usage
add_action( 'automator_whatsapp_webhook_message_received', 'your_function_name', 10, 1 );
Parameters
-
$args(mixed) - This parameter contains an array of arguments passed to the hook, typically representing the raw webhook response data from WhatsApp.
Examples
<?php
/**
* Example function to handle incoming WhatsApp messages via the automator_whatsapp_webhook_message_received hook.
* This function will log the message details and potentially trigger other actions.
*
* @param array $args An array containing message details:
* 'from' (string): The sender's WhatsApp ID.
* 'wamid' (string): The unique message ID.
* 'body' (string): The text content of the message.
* 'timestamp' (int): The Unix timestamp when the message was received.
* '_response' (array): The raw response from the WhatsApp API.
*/
function my_custom_whatsapp_message_handler( $args ) {
// Log the received message for debugging purposes
error_log( 'WhatsApp Message Received: ' . print_r( $args, true ) );
// Example: If the message body contains a specific keyword, perform an action.
if ( isset( $args['body'] ) && strpos( strtolower( $args['body'] ), 'support' ) !== false ) {
// In a real scenario, you might:
// 1. Create a support ticket in your system.
// 2. Send an automated reply acknowledging the request.
// 3. Notify a specific user or team.
// For this example, let's just log a specific action.
error_log( 'User requested support: ' . $args['from'] );
// You could also use other WordPress hooks or functions here.
// For instance, to send an email notification:
// wp_mail( '[email protected]', 'New WhatsApp Support Request', 'A new support request was received from ' . $args['from'] . ' with message: ' . $args['body'] );
}
// Example: If the message is from a specific user, update their profile.
// This is a simplified example and would require user management logic.
// if ( $args['from'] === '+1234567890' ) {
// $user_id = get_user_by_email( '[email protected]' ); // Hypothetical function
// if ( $user_id ) {
// update_user_meta( $user_id, 'last_whatsapp_contact', time() );
// }
// }
}
// Hook into the action, specifying the function to be called and the number of arguments it accepts.
// The 'automator_whatsapp_webhook_message_received' hook passes one argument ($args).
add_action( 'automator_whatsapp_webhook_message_received', 'my_custom_whatsapp_message_handler', 10, 1 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/whatsapp/helpers/whatsapp-helpers.php:278
public function extract_receiving_message_response( $response = array() ) {
$timestamp = $response['entry'][0]['changes'][0]['value']['messages'][0]['timestamp'];
/**
* Prevent spammy WhatsApp webhook. This is an old issue where WhatsApp repeatedly sends the webhook payload to the URL.
* We have to do this to safe guard the users and not let WhatsApp spam their logs with incoming webhooks.
* By default, its enabled.
*
* When the Trigger is erratically firing,
* try increasing the acceptable interval first via 'automator_whatsapp_acceptable_interval'
*
* @default int 5 - Five seconds.
* @filter automator_whatsapp_acceptable_interval
**/
$acceptable_interval = apply_filters( 'automator_whatsapp_acceptable_interval', 5, $response, $this );
/**
* Disable timestamp validation.
*
* Otherwise, you may completely disable the timestamp validation.
*
* @default true
* @filter automator_whatsapp_timestamp_validation_enabled
*/
$timestamp_validation = apply_filters( 'automator_whatsapp_timestamp_validation_enabled', true, $response, $this );
if ( true === $timestamp_validation && ! $this->is_timestamp_acceptable( $timestamp, $acceptable_interval ) ) {
throw new Exception( 'Stale data: WhatsApp Bug. Do not process.' );
}
$default = array(
'from' => '',
'wamid' => 0,
'body' => '',
'timestamp' => '',
);
$message = $response['entry'][0]['changes'][0]['value']['messages'][0];
$text_body = $this->extract_message( $message );
$args = array(
'from' => $message['from'],
'wamid' => $message['id'],
'body' => $text_body,
'timestamp' => $timestamp,
'_response' => $response, // Send the whole response to the Trigger.
);
do_action( 'automator_whatsapp_webhook_message_received', $args );
return wp_parse_args( $args, $default );
}
Internal Usage
Found in src/integrations/whatsapp/triggers/wa-message-received.php:62:
$this->add_action( 'automator_whatsapp_webhook_message_received' );