Manage state with contexts

This page describes how to track conversational states with follow-up intents and contexts.

Add contexts to conversational state

  1. Click the plus add next to Intents in the left menu to create a new intent.

  2. Name the intent "Languages".

  3. Enter the following Training Phrases:

    • I know English
    • I speak French
    • I know how to write in German
  4. In the Response section, enter the following text response:

    • Wow! I didn't know you knew $language. How long have you known $language?
  5. Click the Save button.

  6. Click on Intents in the left menu.

  7. Hover over the "Languages" intent and click on Add follow-up intent:

  8. Click on Custom in the revealed list:

Dialogflow automatically names the follow-up intent "Languages - custom," and the arrow indicates the relationship between the intents.

Intent matching with follow-up intents

Follow-up intents are only matched after the parent intent has been matched. Since this intent is only matched after the "Languages" intent, we can assume that the user has just been asked the question "How long have you known $language?". You'll now add Training Phrases indicating users' likely answers to that question.

  1. Click on Intents in the left menu and then click on the "Languages - custom" intent.

  2. Add the following Training Phrases:

    • 3 years
    • about 4 days
    • for 5 years

Try it out

Try this out in the Dialogflow simulator on the right. First, match the "Languages" intent by entering the query I know French. Then, answer the question How long have your known $language? with about 2 weeks.

Despite there being no response for the second query ("about 2 weeks"), we can see our query is matched to the correct intent ("Languages - custom") and the duration parameter is correctly parsed ("2 weeks").

Intents and contexts

Now that your follow-up intent is being matched correctly, you need to add a response. In "Languages - custom" you've only asked for the duration the user has known the language, and not the referenced language itself.

To respond with a parameter gathered from the "Languages" intent, you need to know how follow-up intents work. Follow-up intents use contexts to keep track of if a parent intent has been triggered. If you inspect the "Languages" intent, you'll see "Languages-followup" listed as an Output context, prefaced by the number 2:

After the "Languages" intent is matched, the context "Languages-followup" is attached to the conversation for two turns. Therefore, when the user responds to the question, "How long have you known $language?", the context "Languages-followup" is active. Any intents that have the same Input context are heavily favored when Dialogflow matches intents.

Click on Intents in the left navigation and then click on the "Languages - custom" intent.

You can see that the intent has the same input context ("Languages-followup") as the output context of "Languages". Because of this, "Languages - custom" is much more likely to be matched after the "Languages" intent is matched.

Contexts and parameters

Contexts store parameter values, which means you can access the values of parameters defined in the "Languages" intent in other intents like "Languages - custom".

Add the following response to the "Languages - custom" intent and click the Save button:

  • I can't believe you've known #languages-followup.language for $duration!

Now you can query your agent again and get the proper response. First enter "I know French", and then respond to the question with "1 month".

You should see that the language parameter value is retrieved from the context.

Next steps

Now that you've completed your first agent, you can extend your response logic with fulfillment and consider which additional platforms you want to support via Dialogflow's one-click integrations.

Fulfillment allows you to provide programmatic logic behind your agent for gathering third-party data or accessing user-based information.

Dialogflow's integrations make your agent available on popular platforms like Facebook Messenger, Slack and Twitter.

You might also want to check out: