1 min read 2 days ago

Chatbot Takeover

If you need to completely bypass the AI query process and return your own message, you can use the mwai_chatbot_takeover filter. This will send the $takeover value as a reply in the name of the chatbot. This can be used in a lot of different use cases.

Here is an example showing how to respond if the user is asking for the current time. Assuming the AI doesn’t have any context or tool to get this info, we can intervene manually and reply with our own value, bypassing any part of the query process.

add_filter( 'mwai_chatbot_takeover', function( $takeover, $query, $params ) {
    // Check for specific triggers
    if ( strpos( $query->get_message(), 'current time' ) !== false ) {
        return 'The current time is ' . current_time( 'mysql' );
    }
    return $takeover;
}, 10, 3 );

You can also take over the current conversation, not to reply as the chatbot, but to stop the conversation with an error message. Here is an example using the mwai_ai_allowed filter.

add_filter( 'mwai_ai_allowed', function( $allowed, $query, $limits ) {
    // Return true to allow, false to block, or a string error message
    if ( /* some condition */ ) {
        return 'This Query was not allowed!';
    }
    return $allowed;
}, 10, 3 );