Action Dynamic uncanny-automator

automator_whatsapp_message_delivery_{dynamic}

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Fires when a WhatsApp message is being delivered, allowing custom actions with incoming message data.

add_action( 'automator_whatsapp_message_delivery_{dynamic}', $callback, 10, 1 );

Description

Fires after a WhatsApp message is delivered, with the dynamic part of the hook name indicating the delivery status (e.g., `success`, `failed`). Developers can use this to perform custom actions based on message delivery outcomes, such as logging, retrying, or triggering subsequent steps in a workflow.


Usage

add_action( 'automator_whatsapp_message_delivery_{dynamic}', 'your_function_name', 10, 1 );

Parameters

$incoming_data (mixed)
This parameter contains mixed data that is passed into the function, potentially including information about the status of the message delivery.

Examples

add_action( 'automator_whatsapp_message_delivery_sent', 'my_custom_whatsapp_sent_handler', 10, 1 );

/**
 * Handles successful WhatsApp message delivery notifications.
 *
 * This function is triggered when a WhatsApp message is successfully delivered.
 * It can be used to perform follow-up actions based on delivery status,
 * such as logging, sending internal notifications, or triggering other recipes.
 *
 * @param array $incoming_data The data received from the WhatsApp delivery webhook.
 *                             Expected keys might include 'message_id', 'status', 'timestamp', etc.
 */
function my_custom_whatsapp_sent_handler( $incoming_data ) {
	// Example: Log the successful delivery to a custom post type or a transient.
	// In a real scenario, you'd likely have more robust error handling and data validation.

	if ( ! isset( $incoming_data['message_id'] ) || ! isset( $incoming_data['status'] ) || 'sent' !== $incoming_data['status'] ) {
		// Basic validation, though the hook itself implies 'sent' status.
		// Still good practice to be defensive.
		return;
	}

	$message_id = sanitize_text_field( $incoming_data['message_id'] );
	$timestamp  = isset( $incoming_data['timestamp'] ) ? absint( $incoming_data['timestamp'] ) : current_time( 'timestamp' );

	// You might want to associate this delivery with a specific user or recipe.
	// This data would typically be available in $incoming_data if passed from the source.
	$user_id = isset( $incoming_data['user_id'] ) ? absint( $incoming_data['user_id'] ) : 0;

	// Example: Store delivery log.
	// This could be a custom database table, a meta field on a user, or a transient.
	$delivery_log = array(
		'message_id' => $message_id,
		'status'     => 'sent',
		'timestamp'  => $timestamp,
		'user_id'    => $user_id,
		// Add any other relevant data from $incoming_data
	);

	// For demonstration, we'll just log it to the PHP error log.
	// In a real application, you'd implement proper data storage.
	error_log( 'WhatsApp Message Sent: ' . print_r( $delivery_log, true ) );

	// --- Further actions based on successful delivery ---

	// Example: If this message was part of a specific recipe,
	// you might want to mark that part of the recipe as complete or trigger the next step.
	// This often requires access to recipe/action IDs which might be passed in $incoming_data
	// if the source context provides them.
	if ( isset( $incoming_data['recipe_log_id'] ) && isset( $incoming_data['action_id'] ) ) {
		$recipe_log_id = absint( $incoming_data['recipe_log_id'] );
		$action_id     = absint( $incoming_data['action_id'] );

		// Assuming Automator plugin is active and has necessary functions.
		// This part would depend heavily on how the Automator plugin tracks recipe progress.
		if ( function_exists( 'Automator' ) && method_exists( Automator()->recipe_runner, 'complete_action' ) ) {
			Automator()->recipe_runner->complete_action( $action_id, $recipe_log_id, true, '' );
		}
	}
}

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:422

public function complete_action( $action_data = array(), $incoming_data = array() ) {

		$completed = 1;

		$error_message = '';

		// Get the meta.
		if ( 'failed' === $incoming_data['status'] ) {

			$recipe_error = Automator()->db->action->get_error_message( $action_data['recipe_log_id'] );

			if ( ! empty( $recipe_error ) ) {

				$completed = 2;

				$error_message = implode( ' - ', array_values( $incoming_data['errors'] ) ) . '<br>';

				// Skip awaiting error message.
				if ( 10 !== intval( $recipe_error->completed ) && $error_message !== $recipe_error->error_message ) {
					// Append the error message so previous error message wont get overwritten.
					$error_message .= $recipe_error->error_message;
				}
			}
		}

		Automator()->db->action->mark_complete( $action_data['action_id'], $action_data['recipe_log_id'], $completed, $error_message );

		Automator()->db->recipe->mark_complete( $action_data['recipe_log_id'], $completed );

		// e.g. `automator_whatsapp_message_delivery_failed`.
		do_action( 'automator_whatsapp_message_delivery_' . $incoming_data['status'], $incoming_data );
		do_action( 'automator_whatsapp_message_status', $incoming_data, $incoming_data['status'] );
	}


Scroll to Top