Filter uncanny-automator

uap_slack_maybe_customize_bot

Filters Slack bot messages before they are sent, allowing customization of bot output.

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

Description

Filters the Slack bot message payload before sending. Developers can modify message content, icons, or other Slack API parameters to customize bot notifications. This hook fires after default bot icon logic and before the message is sent.


Usage

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

Parameters

$message (mixed)
This parameter contains the Slack message payload that can be customized.

Return Value

The filtered value.


Examples

add_filter( 'uap_slack_maybe_customize_bot', 'my_uap_customize_slack_bot_message', 10, 1 );

/**
 * Example customizer for the UAP Slack bot message.
 *
 * This function demonstrates how to modify the Slack message payload
 * before it's sent to Slack. In this example, we'll conditionally
 * add a username based on the message content.
 *
 * @param array $message The Slack message payload array.
 * @return array The modified Slack message payload.
 */
function my_uap_customize_slack_bot_message( $message ) {
    // Let's assume the $message array contains keys like 'text' and potentially 'username'.

    // Check if the message text contains a specific keyword and if a username isn't already set.
    if ( isset( $message['text'] ) && strpos( $message['text'], 'urgent' ) !== false && empty( $message['username'] ) ) {
        // If 'urgent' is in the text and no username is set, prepend "URGENT BOT:" to the username.
        $message['username'] = 'URGENT BOT: ' . ( ! empty( $message['username'] ) ? $message['username'] : 'DefaultBot' );
    } elseif ( isset( $message['text'] ) && strpos( $message['text'], 'info' ) !== false ) {
        // If 'info' is in the text, change the bot icon to a generic info icon.
        // This assumes 'icon_url' is a valid key in the $message array.
        $message['icon_url'] = 'https://example.com/icons/info-icon.png';
    }

    // You could also modify other aspects of the message, like adding attachments,
    // changing the channel, or even preventing the message from sending by returning false.

    return $message;
}

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:52
src/integrations/slack/helpers/slack-app-helpers.php:123

private function maybe_customize_bot( $message ) {
		$bot_name = $this->helpers->get_bot_name();
		if ( empty( $message['username'] ) && ! empty( $bot_name ) ) {
			$message['username'] = $bot_name;
		}

		$bot_icon = $this->helpers->get_bot_icon();
		if ( empty( $message['icon_url'] ) && ! empty( $bot_icon ) ) {
			$message['icon_url'] = $bot_icon;
		}

		return apply_filters( 'uap_slack_maybe_customize_bot', $message );
	}


Scroll to Top