How to Handle Context with Dialogflow (Part 1: Knock Knock Jokes)
(FREE: Download the Dialogflow agent to play with — link at the bottom of article. This is Part 1 of a four-part tutorial series on how to use context with Dialogflow.)
If you’ve been wondering how to use Context, here’s a fun and easy way to learn how to use them in your chatbot — by building Knock Knock jokes!
We all know what knock knock jokes are — they’re silly, cheesy but they never get old.
Yet context is not an easy topic to master. One — there are so few examples or tutorials; and two — it’s bloody hard to explain.
I’m going to try with Knock Knock jokes.
Top 3 Most Popular Bot Design Articles:
1. Why Conversational Context is the Most Powerful Tool you can give your Chatbot
2. Emotionally Intelligent ChatBots — Part 1: Getting emotions from user’s face
3. Emotionally Intelligent ChatBots — Part 2: Getting emotions from user’s voice
Knock knock jokes are a great way to learn about context because the joke can’t be delivered in one punch line. The joker has to say “knock knock” and the user has to respond “who’s there” before the joke even starts. In total, there must be 5 messages exchanged.
This is where context comes into play.
What is Context
In language, context gives clarity and meaning to words written or spoken, and comes from words that immediately precede or follow what was said.
In Dialogflow, “Contexts represent the current state of a user’s request and allow your agent to carry information from one intent to another.”
What this means is that context is the glue that chains intents together. It represents a necessary condition that has to exist before an intent can be triggered. It has to be present in the preceding as well as the current intent.
So how do we do that? Let’s jump straight in.
In this example, we will use 3 intents:
- Knockknock intent
- Knockknock.whosthere intent
- Knocnknock.whosthere.punchline intent
FREE: Download the Dialogflow agent to play with — link at the bottom of article
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 with lifespan of 1.
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. In other words, two conditions are required for this intent to be triggered:
- 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, add output 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
Part 1: How to use Context to build simple Knock Knock Jokes
Part 2: How to use Contextual Fallback to handle errors
Part 3: How to use Context when users try to spoil your punchline
Part 4: How to use Context and Entities to respond to users’ Knock Knock Jokes
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
Let me know how you get along with context and knock knock jokes!
About the Author
Deborah Kay is Chatbot Evangelist and Founder of Digital Discovery, a digital advisory firm based in Singapore. You can connect with her on Facebook, Instagram, Twitter, or LinkedIn.