Action uncanny-automator-pro

automator_api_received

Fires after the Automator API successfully receives and processes data for a recipe execution.

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

Description

Fires after Uncanny Automator processes incoming API data, before saving trigger metadata. Developers can use this hook to intercept and further process API received parameters and recipe data, enabling custom actions or modifications to how external data interacts with Automator recipes.


Usage

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

Parameters

$param (mixed)
This parameter likely contains data passed to the webhook, possibly in an array format, which can be used to populate or trigger automations.
$recipe (mixed)
This parameter contains an array of data received from the webhook request.

Examples

add_action( 'automator_api_received', 'my_automator_api_data_handler', 10, 2 );

/**
 * Handles data received via the automator_api_received hook.
 *
 * This function is an example of how to hook into the automator_api_received
 * action to process data that has been received through the Uncanny Automator API.
 * It demonstrates logging the received parameter and recipe data for debugging
 * or further processing.
 *
 * @param mixed $param The primary data payload received.
 * @param mixed $recipe The recipe associated with the received data.
 */
function my_automator_api_data_handler( $param, $recipe ) {

	// Log the received data for debugging purposes.
	// In a real-world scenario, you might use this data to trigger other actions,
	// update post meta, send notifications, or interact with external services.
	error_log( 'Uncanny Automator API Data Received:' );
	error_log( 'Parameter Data: ' . print_r( $param, true ) );
	error_log( 'Recipe Data: ' . print_r( $recipe, true ) );

	// Example: If the recipe is a specific type, perform a special action.
	if ( is_array( $recipe ) && isset( $recipe['ID'] ) && 123 === $recipe['ID'] ) {
		error_log( 'Processing special action for Recipe ID 123.' );
		// Perform special actions here, e.g., update user meta, send custom email.
		// For instance: update_user_meta( get_current_user_id(), 'special_automator_flag', true );
	}

	// Example: Accessing specific data within the $param array.
	if ( is_array( $param ) && isset( $param['user_id'] ) ) {
		$user_id = absint( $param['user_id'] );
		error_log( "Processing data for User ID: {$user_id}" );
		// Further actions based on user ID.
	}

	// No return statement is needed for an action hook.
}

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

uncanny-automator-pro/src/core/webhook/webhook-common-options.php:406

public static function run_webhook( $trigger_code = null, $trigger_meta = null, $param = array(), $recipe = array(), $request = null ) {
		$user_id = get_current_user_id();

		$args = array(
			'code'           => $trigger_code,
			'meta'           => $trigger_meta,
			'ignore_post_id' => true,
			'webhook_recipe' => $recipe['ID'],
			'is_webhook'     => true,
			'user_id'        => $user_id,
		);

		$args = Automator()->process->user->maybe_add_trigger_entry( $args, false );
		if ( empty( $args ) ) {
			return;
		}

		// Adding an action for other usage of API Data.
		do_action( 'automator_api_received', $param, $recipe );

		// Save trigger meta
		foreach ( $args as $result ) {
			if ( true !== $result['result'] ) {
				continue;
			}
			if ( empty( $param ) ) {
				continue;
			}

			$trigger_id     = absint( $result['args']['trigger_id'] );
			$trigger_log_id = absint( $result['args']['trigger_log_id'] );
			$run_number     = absint( $result['args']['run_number'] );
			$trigger_meta   = array(
				'user_id'        => $user_id,
				'trigger_id'     => $trigger_id,
				'trigger_log_id' => $trigger_log_id,
				'run_number'     => $run_number,
			);
			Automator()->db->token->save( 'WEBHOOK_BODY', maybe_serialize( $param['WEBHOOK_BODY'] ), $trigger_meta );
			$tokens = array();
			foreach ( $param['params'] as $data ) {
				$tokens[ $data['meta_key'] ] = maybe_serialize( $data['meta_value'] );
			}

			// Extract headers if configured.
			$extract_headers = self::get_header_token_configuration( $trigger_id );
			if ( ! empty( $extract_headers ) ) {
				$extracted_headers = self::extract_configured_header_values( $request, $extract_headers );
				if ( ! empty( $extracted_headers ) ) {
					foreach ( $extracted_headers as $token_name => $header_value ) {
						Automator()->db->token->save( $token_name, maybe_serialize( $header_value ), $trigger_meta );
					}
				}
			}

			// Directly use the function since it will Fatal with Traits. We can use Traits after sometime or after we built an "Adapter" for it like we did with Action tokens.
			if ( method_exists( Automator()->helpers->recipe, 'set_trigger_log_properties' ) ) {
				self::insert_properties( $request, $tokens, $param );
			}

			Automator()->db->token->save( "{$trigger_code}_parsed", maybe_serialize( $tokens ), $trigger_meta );

			Automator()->process->user->maybe_trigger_complete( $result['args'] );

		}
	}

Scroll to Top