I would like to add realtime data (like from Google) to my chatbot context, how should I do that ?

If you need help with this, you can check out our documentation on the matter here. Just use the mwai_context_search filter.

I would like the chatbot to generate arguments to call a function I have in my code, is it possible ?

There is an OpenAI feature just for this, it’s called Function Calling. Read our documentation to learn how to use it with AI Engine. This example will cover how to send emails from the chatbot.

I would like to add to the chatbot’s message some data from my database, an API or a webhook, is it possible ?

Yes, 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.