How to get the bot to utter a different message depending on the results of a database query?

I have a bot that asks a user a series of question and then does a custom action where that action is to do some database query. If there is a result then the bot will display the result to the user and the conversation can continue further.

What I’d like to do is if the query does not have any results, I’d like the bot to utter something like “sorry, no results found” and end the conversation. I’m not sure how to implement this feature. I was thinking of creating a custom slot type that tracks whether the query returned a result and create stories where the value of that slot type is “True” (i.e. the query did return some result) and “False”, similar to this example in the documentation.

Is this the way to go or are there better alternatives?

Are you using a FormAction or a bunch of individual actions to ask the series of questions?

If you use a FormAction, you could just perform the database query in the submit() function and use a flag variable to check whether result was found or not and then dispatch the appropriate response.

I was going to go with multiple individual actions but I will try FormAction now.

I’m still not sure how to change the story even after reading Forms documentation.

Using the restaurant example in the documentation as an example, suppose that the submit() method of restaurant_form will dispatch a utter_no_restaurant template if no restaurant can be found with the details given by the user and I want the bot to utter goodbye and then end the conversation. How do I write a story to capture this scenario? Is this the correct way?

## no restaurant found
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
    - form{"name": null}
    - utter_goodbye

I think the best approach would be to have dispatcher.utter_template("utter_no_restaurant") followed by dispatcher.utter_template("utter_goodbye") in your submit() code if no restaurant was found.

If you use this approach, you don’t need to add the utter_goodbye in your story.

## no restaurant found
* request_restaurant
   - restaurant_form
   - form{"name": "restaurant_form"}
1 Like

Another way to do this is simply build your own custom action and execute your own logic in it.

* request_restaurant
   - action_get_restaurant

In action_get_restaurant you can get the restaurant from database. If there isn’t any then utter_message("No result found"). If there is a restaurant then just utter_template("your template", restaurant=restaurant) (assume your utter template has {restaurant} in it). You can read about custom action here: Actions

1 Like