Filter uncanny-automator

uap_slack_chat_post_message

Filters the message body before it is sent to Slack, allowing modification of the notification content.

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

Description

Filters the message body before sending a chat message to Slack. Developers can modify the payload, including the message content, attachments, and other Slack API parameters, to customize outgoing Slack notifications. This hook fires just before the Slack API request is made.


Usage

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

Parameters

$body (mixed)
This parameter is a mixed array containing data to be sent to the Slack API for posting a message.

Return Value

The filtered value.


Examples

add_filter( 'uap_slack_chat_post_message', 'my_custom_slack_message_body', 10, 1 );

/**
 * Example function to modify the Slack message body before sending.
 *
 * This function demonstrates how to add extra information to the Slack message body,
 * such as the current user's ID or a timestamp, for more context in the Slack notification.
 *
 * @param array $body The default message body array.
 * @return array The modified message body array.
 */
function my_custom_slack_message_body( $body ) {
	// Assume $body is an array like: [ 'action' => 'post_message', 'message' => 'Your message here' ]

	// Check if a WordPress user is currently logged in.
	if ( is_user_logged_in() ) {
		$current_user = wp_get_current_user();
		// Add the user's ID to the message body for context.
		$body['user_id'] = $current_user->ID;
	}

	// Add a timestamp to the message body.
	$body['timestamp'] = current_time( 'mysql' );

	// You could also conditionally modify the 'message' content if needed.
	// For example, to prepend a specific string to all messages:
	// $body['message'] = '[Urgent] ' . $body['message'];

	return $body;
}

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/slack/helpers/slack-api-caller.php:29

public function chat_post_message( $message, $action_data = null ) {
		$body = array(
			'action'  => 'post_message',
			'message' => $this->maybe_customize_bot( $message ),
		);

		$body = apply_filters( 'uap_slack_chat_post_message', $body );

		return $this->slack_request( $body, $action_data );
	}


Scroll to Top