Action uncanny-automator

automator_whatsapp_message_status

Fires when a WhatsApp message status is updated, passing incoming data about the status change.

add_action( 'automator_whatsapp_message_status', $callback, 10, 2 );

Description

Fires after a WhatsApp message status is updated, allowing developers to react to specific delivery outcomes. Use this hook to log failed deliveries or trigger subsequent actions based on the message's status. It passes the incoming status data for conditional logic.


Usage

add_action( 'automator_whatsapp_message_status', 'your_function_name', 10, 2 );

Parameters

$incoming_data (mixed)
This parameter contains data related to the status of a WhatsApp message.
$incoming_data (mixed)
This parameter likely contains data related to the incoming message status from WhatsApp, including the status itself and potentially error messages if the status is 'failed'.

Examples

<?php
/**
 * Example callback function for the 'automator_whatsapp_message_status' action hook.
 * This function processes incoming WhatsApp message status updates to potentially
 * trigger further actions or logic within the WordPress Automator plugin.
 *
 * @param array $message_data The complete data array received for the WhatsApp message status.
 * @param string $status The specific status of the WhatsApp message (e.g., 'delivered', 'failed', 'read').
 */
function my_automator_whatsapp_status_handler( $message_data, $status ) {

	// Log the incoming status for debugging purposes.
	error_log( 'WhatsApp Message Status Update: Status - ' . sanitize_text_field( $status ) . ', Message ID: ' . ( isset( $message_data['id'] ) ? esc_html( $message_data['id'] ) : 'N/A' ) );

	// Example: If the message failed, trigger a user notification.
	if ( 'failed' === $status ) {
		$recipient_user_id = get_user_by_email( $message_data['to'] )->ID ?? null; // Assuming 'to' field contains recipient email.

		if ( $recipient_user_id ) {
			// This is a placeholder for sending an actual notification.
			// In a real scenario, you'd use wp_mail() or a notification plugin.
			error_log( 'WhatsApp message failed for user ID: ' . $recipient_user_id . '. Attempting to notify.' );

			// Example of how you might queue a notification:
			// wp_enqueue_script( 'my-notification-script', 'path/to/your/script.js', array(), '1.0', true );
			// wp_localize_script( 'my-notification-script', 'myNotificationData', array(
			// 	'message' => 'Your WhatsApp message failed to deliver.',
			// 	'userId' => $recipient_user_id,
			// ) );
		}
	}

	// Example: If the message was read, update a custom post meta.
	if ( 'read' === $status ) {
		// Assuming $message_data contains a reference to a related post or object.
		// Let's pretend there's a 'related_post_id' in the data.
		$related_post_id = isset( $message_data['related_post_id'] ) ? absint( $message_data['related_post_id'] ) : 0;

		if ( $related_post_id && get_post_status( $related_post_id ) ) {
			update_post_meta( $related_post_id, '_whatsapp_message_read_timestamp', time() );
			error_log( 'WhatsApp message read status updated for post ID: ' . $related_post_id );
		}
	}

	// You could add more complex logic here based on different statuses.
}

// Add the callback function to the 'automator_whatsapp_message_status' hook.
// The '2' indicates this function accepts 2 arguments.
add_action( 'automator_whatsapp_message_status', 'my_automator_whatsapp_status_handler', 10, 2 );
?>

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

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'] );
	}


Internal Usage

Found in src/integrations/whatsapp/triggers/wa-message-status-updated.php:61:

$this->add_action( 'automator_whatsapp_message_status' );
Scroll to Top