Filter uncanny-automator

automator_outgoing_webhook_live_response_tokens

Filters the tokens available for live webhook responses to modify their content before sending.

add_filter( 'automator_outgoing_webhook_live_response_tokens', $callback, 10, 1 );

Description

This filter allows developers to modify outgoing webhook live response tokens before they are saved. It's applied after header and body tokens are combined, providing access to the `$response` data. Use this hook to dynamically alter or add tokens, potentially overriding saved ones, for richer webhook integration. The first parameter defaults to `false`, indicating whether to force an override.


Usage

add_filter( 'automator_outgoing_webhook_live_response_tokens', 'your_function_name', 10, 1 );

Parameters

$response (mixed)
This parameter is a placeholder and does not contain any meaningful data.

Return Value

The filtered value.


Examples

/**
 * Example of a WordPress filter for 'automator_outgoing_webhook_live_response_tokens'.
 *
 * This filter allows you to modify the webhook response tokens before they are saved.
 * In this example, we'll add a custom token based on the response status code,
 * but only if the response is successful (status code 200).
 *
 * @param mixed $value    The default value passed to the filter. In this case, it's initially `false`.
 * @param mixed $response The full webhook response object.
 *
 * @return mixed The modified value to be used by the filter.
 */
add_filter(
	'automator_outgoing_webhook_live_response_tokens',
	function ( $value, $response ) {
		// Check if the filter is being called with the default value of 'false'
		// or if it's being explicitly enabled by another filter.
		// We also check if the $response is available and has a 'code' property.
		if ( ( false === $value || true === $value ) && isset( $response->code ) ) {

			// Check if the response status code is 200 (OK).
			if ( 200 === $response->code ) {
				// Add a custom token representing the success of the webhook.
				// The key 'webhook_status_success' will be available as a token
				// in subsequent automation steps if this filter returns `true`.
				// The actual value passed to the filter (which is '$value' here)
				// will be used to determine if the tokens should be re-saved.
				// Returning 'true' here ensures that if the initial call was 'false',
				// it's now evaluated as 'true', prompting re-saving of tokens.
				// We are not directly modifying the tokens here, but influencing
				// the logic that saves them.
				return true; // Signal to re-save tokens with the potentially modified `$all_tokens`
			}
		}

		// Return the original value if no modifications are needed or conditions are met.
		return $value;
	},
	10,
	2 // This filter accepts 2 arguments: $value and $response
);

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/core/lib/recipe-parts/trait-webhooks.php:181

// Combine header and body tokens.
			$all_tokens = array_merge( $header_leafs, $response_body );

			// If tokens are not previously saved OR set to true, override previously saved tokens.
			if (
				empty( get_post_meta( $action_data['ID'], 'webhook_response_tokens', true ) ) ||
				true === apply_filters( 'automator_outgoing_webhook_live_response_tokens', false, $response )
			) {
				$save_tokens = Automator_Send_Webhook::clean_tokens_before_save( $all_tokens );
				update_post_meta( $action_data['ID'], 'webhook_response_tokens', wp_json_encode( $save_tokens ) );
				unset( $save_tokens );
			}

			// Parse response into leafs.

Scroll to Top