I would suggest you to use intents for each parameter that you require rather than using entities. That would solve two problems:
- If user does not give correct value for required param, dialogflow
will not keep on asking same question again and again in loop
- You can control which parameter to fill next
Suppose you require 3 parameters to buy a watch : color, date/date-time, cost
Your agent will have these intents:
- buy_watch -> input_context = None, output_context = buy_watch
I want to buy a watch
I want to buy watch in black color
...
- get_color -> input_context = color, output_context = buy_watch
Black
i want it in black
...
- get_date -> input_context = date, output_context = buy_watch
i want it tomorrow
i want it next week
(that will capture both date and date-range and not on anything else, you can have logic to assign the value to param if you get any of them)
- get_cost -> input_context = cost, output_context = buy_watch
around 10000
budget is 15k
- got_all_details --> event = e_got_all_details
For each of the intent you will have all 3 parameters optional.
Also, you will nee to set default value of the param to the $entity for self intent, and #context.entity for other params.
For example, in the intent get_color, you will have these params and default values:
color = $color
date = #buy_watch.date
cost = #buy_watch.cost
Lastly, you will have to call webhook for all the above intents.
In the webhook, you will have to check the params of incoming intent and set output_context + response_message according to the missing param.
params = intent_request["query_result"]["parameters"]
case 1 --> if not params["color"]: set output_context = color and response = what color do you want
case 2 --> if not params["date"]: set output_context = date and response = when do you want to buy
case 3 --> if not params["cost"]: set output_context = cost and response = what is your budget
case 4 --> if you get all params: call event e_got_all_details
Intent got_all_details will be called through event when you get all the params, regardless of in what order you get them.
Depending on the output_context of the response and input_context which you have configured in your intent, correct intent will be triggered and you will be able to get the correct values.
I had similar problem, and this was the most efficient way to handle it in my opinion for validation of params and handling when you get unexpected values (using intent specific follow-up fallback intents).
Edit 1: One extra logic will be there to assign value to date depending on you got it from date or date range entity.
if params["date"] or params["date-range"]:
date = params["date"] if params["date"] else params["date-range"]
All the intents will have training phrases which will try to capture params without any order, but do not mark them required.
So, if the user says i want to buy a watch tomorrow, buy_watch intent will be triggered with param date. The code will check color and cost are missing and will prompt for that after setting appropriate output_context.
Hope it helps.