When you define intent in Dialogflow, you can set one or more input or output contexts. These contexts are used to limit the scope of specific intents so that they will be triggered only when certain conditions are met.
We’ve already discussed a simple example of a restaurant chatbot, with two different skills triggered by the same utterance – “How much will it be?”. One skill, to calculate the delivery cost, should be triggered only in the context of arranging the delivery order, and another, calculating the reservation cost, should be triggered only when the user is asking about the reservation.
Let’s build this in Dialogflow and Activechat!
We’ll be creating two different intents with the same set of training phrases. The only difference will be the contexts: we’ll set “reservation” as input context for the first one, and “delivery” as input context for another.
If you test one of your training phrases in the Dialogflow console, you may be surprised. Even when you type the phrase exactly as it’s entered in the intent definition, Dialogflow does not recognize it and throws “Default Fallback” intent instead. How can it be?
The reason is that we have input contexts set in both of these intents. To make your chatbot able to recognize the “How much will it be?” utterance outside of any of these two contexts, you will need another intent – with the same training phrases but no contexts set.
Again – when you specify input context for the intent, this intent will be triggered only when this context is present!
Let’s store the current conversation context in the $context attribute. When you are in the “reservation” skill, it should be set to “reservation” (use DATA block to set the value), and when your chatbot user switches to the “delivery” skill, set it to “delivery”.
Here is how this can be done when choosing the skill from the chatbot menu:
If your “reservation” and “delivery” skills are also triggered by the NLP intent, you can set the context immediately after the opening CATCH block in each of these skills.
Don’t forget to pass the context to Dialogflow, adding it to the NLP block settings:
Now, when you type “What’s the cost?” in the chatbot, one of your intents will be triggered – depending on what is the current conversation context.
Output contexts from Dialogflow intents are available in the $_nlp_contexts system attribute.
For more info on Dialogflow contexts, check the official contexts overview from Google.