automator_whatsapp_acceptable_interval
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' Filters the interval to prevent spammy WhatsApp webhook requests by controlling how often triggers can fire.
add_filter( 'automator_whatsapp_acceptable_interval', $callback, 10, 1 );
Description
This filter controls the minimum time in seconds between processing WhatsApp webhook requests to prevent spam. Developers can increase this value to reduce erratically firing triggers, safeguarding users from excessive log entries. The default is 5 seconds.
Usage
add_filter( 'automator_whatsapp_acceptable_interval', 'your_function_name', 10, 1 );
Parameters
-
$response(mixed) - - **$this** `mixed`
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the 'automator_whatsapp_acceptable_interval' hook.
* This example increases the acceptable interval to 10 seconds if a specific
* WhatsApp response object indicates an error.
*
* @param int $acceptable_interval The current acceptable interval in seconds.
* @param mixed $response The response object from the WhatsApp API.
* @param mixed $whatsapp_helper The instance of the WhatsApp helper class.
* @return int The modified acceptable interval.
*/
add_filter( 'automator_whatsapp_acceptable_interval', function( $acceptable_interval, $response, $whatsapp_helper ) {
// Check if the response object is not empty and contains an error code
if ( ! empty( $response ) && isset( $response->error_code ) && $response->error_code === 400 ) {
// If an error is detected, increase the acceptable interval to 10 seconds
// This might help with temporary network issues or rate limiting.
return 10;
}
// Otherwise, return the default or previously filtered interval
return $acceptable_interval;
}, 10, 3 ); // Priority 10, accepts 3 arguments
?>
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:243
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 );
}