How to Handle Context with Dialogflow (Part 1: Knock Knock Jokes)
Step 1: Build a trigger for the bot to say “Knock Knock”
To get the bot to start the “knock knock” joke, you have to trigger it. In this example, we’ll use an intent to trigger it.
1.1 Create Intent#1: knockknock, where user says “wanna knock knock joke” and bot responds “knock knock”.
1.2 Add output context knockknock
Step 2: Build an Intent for Who’s There
2.1 Create Intent #2 : knockknock.whosthere. In this Intent, add training phases similar to “who’s there”.
2.2 Add input context = knockknock.
2.3 Fill out the responses to “who’s there”. In this example, we use “Annie”.
How this works: This second intent will only be triggered if the preceding utterance contains the output context-knockknock.
- The user said “who’s there”;
- The output context-knockknock is present.
Step 3: Build an Intent for the Punchline
3.1 Still in Intent#2, addoutput context “whosthere” with lifespan of 1
3.2 Create Intent #3: knockknock-whosthere-annie
Add input context — whosthere
And viola! You’ve just created your first knock knock joke using Context in Dialogflow!
Here’s a summary of the contexts for these three intents
What Happens when Said Out of Context
Note, if I say “who’s there” or “annie who” without the necessary context — bot will return a fallback response. This is because the context did not exist for the bot to respond.
BONUS: Using Follow Up Intents
You can also use Follow Up intents to build knock knock jokes.
With Follow Up Intents, Dialogflow automatically prefills the necessary input and output context (context=knockknock-followup) to the preceding and current intents.
The principles are the same:
- The preceding intent has to contain an output context;
- The current intent has to contain the matching input context for the intent to be triggered.
Before we go, here’s one last joke to end the tutorial!
In the next tutorial
In Part 2, we will cover the use of contextual fallback to handle errors and unexpected responses from the user! You can find Part 2 here.
Here’s a sneak peak!
Download the Dialogflow Agent
Over to you now! To kick things off, download my Dialogflow agent with three knock knock jokes to practice with. Use this handy reference of 40 Ridiculous Knock Knock jokes to modify and create your own!
Other tutorials In this series
The examples in this tutorial are from Sammy the Sailing Bot, the world’s first AI chatbot by the Singapore Sailing Federation
Other chatbot articles that I’ve written
Slideshare: 10 Do’s and Don’t for Successful Adoption of Chatbots
Let me know how you get along with context and knock knock jokes!