Add Data From External Sources To A Reply

You can do that by using the mwai_ai_reply filter. 😊 This will trigger every time before the chatbot sends a response to the user. So, we will have time to retrieve data from any sources and modify the response.

Here is an example: if the user asks about their shipping status, we can provide information about the delivery and redirect them to the order page. 🚚📦

add_filter('mwai_ai_reply', 'my_mwai_reply', 10, 2);

function my_mwai_reply($reply, $query){
	// First we'll check if the user is talking about their shipping number.
	$last_message = $query->get_message();
	$keywords = ['shipping', 'number', 'track', 'order', 'delivery'];
	$is_shipping_query = preg_match_all('/' . implode('|', $keywords) . '/i', $last_message) > 0;

	// If it's the case, we will get the user's last order from our database.
	if ($is_shipping_query) {
		$user = wp_get_current_user();
		// We'll get the last shipping number from the user's meta. You could also get it from your database.
		$shipping_number = get_user_meta($user->ID, 'last_shipping_number', true);

		//if we have a valid shipping number, we'll check our Zapier webhook to get the latest status of the order.
		if ($shipping_number) {
			$zapier_url = 'https://hooks.zapier.com/hooks/catch/shipping_number/status/';
			$zapier_response = wp_remote_post($zapier_url, array(
				'method' => 'POST',
				'body' => array(
					'shipping_number' => $shipping_number
				)
			));

			$estimated_delivery_date = $zapier_response['estimated_delivery_date'];

			// We'll return a message to the user with the estimated delivery date and a link to the order page.
			$reply->set_reply("Your order will be delivered on $estimated_delivery_date. You can track your order <a href='https://myshop.com/orders/$shipping_number'>here</a>.");
		} 
	}

	return $reply;
}
 

Instead of modifying the entire query, you can also append some text at the end or beginning of your chatbot's response. Here's another example:  Your user asks how much stock you have for selling books. You want your chatbot to say something and then add the actual data afterward.

 
add_filter('mwai_ai_reply', 'my_mwai_reply', 10, 2);

function my_mwai_reply($reply, $query){
	// First we'll check if the user is talking about our stock.
	$last_message = $query->get_message();
	$keywords = ['stock', 'do you have', 'available'];
	$is_stock_query = preg_match_all('/' . implode('|', $keywords) . '/i', $last_message) > 0;

	// If it's the case, we will get the user's last order from our database.
	if ($is_stock_query) {
		//Extract the book name from the query as you can see in the example below
		$book_name = preg_match_all('/[A-Z][a-z]+/', $query->prompt, $matches);
		$book_name = implode(' ', $matches[0]);

		// We'll get the stock from our database
		global $wpdb;
		$wpdb_stock = $wpdb->get_results("SELECT stock FROM books WHERE name LIKE '%$book_name%'");

		// We'll append the stock to the chatbot's response
		$reply->set_reply($reply->result . "\n\n We have " . $wpdb_stock[0]->stock . " in stock.");
	} 

	return $reply;
}
 

Note: These examples are just examples. They won't work out of the box. This just showcases some use cases. You can do more complex or simpler ones. You can even call a new AI Engine request before sending a response from itself (check this out!). The possibilities are endless! Have fun using these.

 

Can I let the AI formulate the reply instead of me manually doing it ?

Using the previous filter will assist you in modifying the AI's response right before it's presented to your user. However, you might prefer to integrate this data into your chatbot's context and allow it to manage how the response is crafted. Please be aware that this approach may result in data modification that is not accurate. For this specific use case, instead of using the 'mwai_ai_reply' filter, you can utilize the 'mwai_context_search' filter.

 
add_filter( "mwai_context_search", 'my_api_context', 10, 3 );

function my_api_context( $context, $query, $options = [] ) {

  // If the context is already provided (maybe through embeddings or other means), we probably
  // don't need to waste time - we can return the context as is.
  if ( !empty( $context ) ) {
    return $context;
  }

  // Get the latest question from the visitor.
  $lastMessage = $query->get_message();

	// Fetch your data however you like, you can refer to the examples above.
	// [...] Using the same code to fetch the books from our database.

  // For test, let's say we got back this from the results:
  $content = "13 copies of Lord of the Meows remain in our library.";

  // If we have any results, let's build the content as to be used for the context of this query.
  if ( !empty( $content ) ) {
    $context["content"] = $content;
    $context["type"] = "websearch";
    return $context;
  }

  return null;
}
 
Did this answer your question?
😞
😐
🤩