One of the nice things about tools like Chatfuel and ManyChat is their visual interface for creating decision tree chatbots.
What are decision tree chatbots?
These go by many names - conditional logic chatbots, click-bots, scripted bots (which I like the best).
The important thing is that the entire conversation is pre-scripted, there isn't any Natural Language Understanding or AI of any sort, and people chat with your bot by simply choosing (clicking on) one of the options presented as buttons.
You can create similar chatbots using Dialogflow, and I will explain how in this article.
What we will be building
We will build the dichotomous key chatbot that I have talked about before.
The user will answer some Yes/No questions and figure out the type of vertebrate class, as the flowchart below shows:
Moving the conversation to the next intent
You should first read my article on moving the conversation to the next intent, because I use the basic idea from that article for building the entire chatbot.
The basic process is as follows:
- set up an output context for intent 1
- use the same as input context for intent 2
- based on user's response, set the input/output contexts
The Telegram channel
We will be building a bot you can deploy to Telegram. Why Telegram?
- there is no approval process for a Telegram bot, which makes the overall process much easier
- the actual process of bot creation in Telegram is completely straight-forward, and it is less likely you will get stuck in one of the main steps
- there is less jargon to learn and understand
On the flip side, there aren't a whole lot of people using Telegram! 🙂
But you should use the exact same process and be able to deploy your bot to Facebook Messenger, or Slack, or Skype or any other channel.
You can talk to the bot here (you need to use the Telegram app)
Connecting your Telegram bot to Dialogflow
Unlike pretty much every other channel, connecting your Telegram bot to Dialogflow is quite straight-forward.
Talk to the BotFather
First, you need to talk to BotFather. 🙂
Choose a name for your bot
Choose a username for your bot
Copy the access token provided by BotFather
Choose the Telegram integration inside Dialogflow
Paste the access token and start the bot
Defining the intents
Now we will define the list of intents for this chatbot.
The welcome intent gives you an option to choose amongst different kinds of bots to test (note: for now, only the decision tree is implemented).
When user selects Decision tree, we show the first question:
If the user says "Yes" to "Does it have fur?", then we respond that it is a mammal. Note that we don't prompt the user for any more questions, so there is no need to set an output context.
On the other hand, if the user responds "No" to the question "Does it have fur?", then we will respond with the next question - "Does it have feathers?" Note that in this case we do set the output context, as we expect another yes/no answer from the user.
Now you can repeat the same process and define all the intents for the entire flowchart. Here is the list of intents for your reference: once you define all of them, your entire flowchart should work.
Additional things to note
This is a pretty simple chatbot.
In fact, it is so deceptively simple, that you have likely missed many factors which are at play to make it work properly. (Unless you are a regular reader of this blog, in which case you probably already knew most of this stuff).
Why not use followup intents?
Notice that I haven't used follow up intents. This is an example of a chatbot which is very hard to build if you used the default lifespan of 2 (which the followup intent does) in your intents. By avoiding followup intents, I have worked around the problem. You can also change the lifespan of all output contexts to 1 in your followup intents, but it is easy to mess it up.
The problem we face is that the user's responses, which are added as Training phrases into the various intents, are identical across multiple intents (just the words "Yes" and "No").
So imagine this: as a response to the first question, the user clicks the "Yes" button. Now "Yes" is a training phrase which is used in multiple intents. How does Dialogflow choose the right one? It does it by using the input context.
In Dialogflow, as you have seen, we can set input and output contexts.
Dialogflow's contexts are usually explicitly defined (that is, they are typed out inside the visual editor). Here is what this means: an intent with an input context can never be triggered unless that context is currently active in the conversation.
You can infer/predict a lot of things based on the above rule.
Context lifespan = 1
I have always maintained that the best value for the context lifespan is 1, in nearly all cases.
This bot is a good example. If you were to use the default lifespan of 5, what will happen when user pressed "Yes" on a button? Since a context from a previous step could still be active there is a pretty good chance that the user's response could be mapped to the wrong intent.
By carefully calibrating all lifespans to be 1, we avoid this scenario. (And the reason the default lifespan used in followup intents fails is the same - you will regularly trigger the parent intent in a Yes/No branching scenario because the parent's input context is still active).
Want a copy of the agent's ZIP file to import into Dialogflow?
You can download the agent ZIP file from my MBD Resources course. (Chapter 17)
- Free Tool: Convert your WordPress website into a Dialogflow FAQ chatbot
- Dialogflow Mega Agent Tutorial
- Reader Question: Will followup events work when an intent has an input context?
- How to learn Dialogflow in a week
- Reader Question: Do I need a contextual fallback intent for every single context?
- Reader Question: Can I use a single agent for Facebook, WhatsApp and website bot?
- Five ways to integrate a Dialogflow chatbot into your website
- BotFlo vs Dialogflow CSV Importer
- Build Better Dialogflow Bots
- Deploy NodeJS Webhook to Google Cloud