Deliver the right response to your Manychat users when you know the context of their messages

How to make your ManyChat bots smarter with Dialogflow AI (Part 5)

This is a multi-part series on how to add AI to ManyChat. For the complete series including videos and tutorials, go to https://www.janis.ai/manychat

You might have linear flows, buttons, and carousels, but free-form text input is the primary user input on Messenger, so you’re going to need your bot to be responsive to messages you receive. Dialogflow’s strength lies in it’s natural language processing (NLP), the artificial intelligence used to understand human intent and provide meaningful responses to your users.

No time to read? Watch this story on video

Context

The same message, or a vague message sent by your users can mean different things, depending on the user’s preferences, geographic location, the current flow the user is in, or just the topic of conversation.

Let’s say for example a user sends your bot the thumbs up sticker. You could send back a thumbs up emoji as a response, or you might even randomize your responses. What if you knew the thumbs up was in reaction to an event in your conversational experience and you could tailor your response to that event?

Using Contexts in Dialogflow enable you to deliver a more contextual response.

Getting Started

I’ve taken the restaurant bot template on ManyChat and I’m going to start to add some AI to it.

If I load up the template in Messenger and click the Restaurant Menu option in the welcome message you’ll see how a user is given a carousel of menus the restaurant offers.

If a user wanted to browse a restaurant’s menu like this, they could just Google the restaurant and browse a menu on a website. In a conversational experience, it’s more natural and likely that a user will just ask questions about what’s on the menu. Adding a keyword rule in ManyChat for every item on the restaurant’s menu isn’t the best strategy here. Just because the message contains the word “tacos”, the context of the message is unknown. Also, what if they ask about an item you don’t offer on your menu?

Let’s add Dialogflow!

The first thing I’ll do is create an Entity in Dialogflow called Food ( Part 3 of this series explains how to create entities in greater depth). I’ll include everything on the restaurant menu in my Food entity.

I just added 4 quick things for this article

It’s going to be much easier to create a variety of training phrases, and just include any item on the restaurant menu within a phrase users might say to my bot. Dialogflow will recognize the word as a value from my Food entity.

I just added the word tacos, and it could be any item on the restaurant menu.

We’ll want to create two different intents

A user might ask questions about items that are on the restaurant menu, and items which are not on the menu, so we’ll want to create two Dialogflow intents. You can see how I’ve named my intents:

For each intent, I added the exact same training phrases, but in the foodrequest-On-Menu intent I referenced the Food entity, and in my foodrequest-Not-on-menu I used Dialogflow’s built-in sys.any entity for keywords. This will tell Dialogflow that any value in the phrase (which hasn’t been explicitly defined as a value in our Food entity) will trigger a different intent.

Highlight your keyword and you’ll prompted to select an entity. Select the @sys.any entity. I could have used any random keyword, because as long as it’s not part of our Food entity, this intent will get triggered for the same training phrases.

Create responses for these two intents

When the user inputs any of these training phrases, you might respond by redirecting the user to a ManyChat flow (See Part 2 on how to respond with a ManyChat flow), but I just created two different responses directly in Dialogflow. When you add keywords in your phrases that Dialogflow recognizes at entities, it automatically creates the Parameter Value for you and adds it to the Actions and Parameters section of the intent. You’ll see here that I’ve included the Parameter Value $Food in my response.

Example: 
User: Do you have tacos?
Bot: Yes, we have tacos

If the user asks about a food item that we don’t have on the menu, I included the$any Parameter Value.

Example: 
User: Do you have lasanga?
Bot: Sorry, we don’t have lasanga on the menu

Note: I could have put a Flow redirect in my response and funnel the user into a reservation flow. I could include my text response “Yes, we have $Food” and below that a Custom Payload response which redirects to a ManyChat Flow for capturing a reservation, but I’ve kept it open here because the purpose of this tutorial is to demonstrate context.

Let’s test our AI powered Q&A for the menu

Now that we have our two intents set up to respond to questions about our menu, we can test them out and see what that looks like in Messenger:

After telling the user if we have something, or don’t have something on our menu, I am probably going to get a response from the user.

When I set context I’ll know the context of their response.

Now let’s add context!

In my foodrequest-On-Menu we don’t need an input context, we just need to set the context here and so I’m going to set an output context and name my context evaluating_menu. You just need to type in a desired name for the context and hit enter to commit it. I’ve named my context evaluating_menu because this is exactly what the user is doing, asking questions and evaluating my restaurant menu.

Note: When naming contexts, you can only use alphanumeric names and no spaces. (dash or underscore is acceptable).

I’ve added an output context to my foodrequest-Not-on-Menuintent:

You’ll see a number next to your context name. This is called the context lifespan. By default, contexts expire after either 5 requests, or 20 minutes from the time they were activated. To keep the context active for a while, you may want to keep the default of 5, or you if you only want the lifespan to be short because the topic of the conversation is going to change, just click the number to edit it. I’ve set the lifespan of the context in these intents to 2, since the user will ask if we have a type of food. We’ll then respond if we do, or don’t have the item, and then wait for another message from the user.

Note: You can also extend or reduce the lifespan of a context by adding the same output context to subsequent intents. The lifespan will be set to whatever is set in the most recently matched intent containing the output context in question.

I’ve also created two additional intents because I’m going to want to capture the user’s response to our restaurant menu and you can see how I’ve named my intents here:

If the user is happy that we have tacos on the menu, they might express their happiness with that with the blue thumbs (See Part 4 on how to capture the blue thumbs up sticker), or some positive phrases:

You’ll likely have several intents for “Thanks” or the blue thumbs up sticker, and each can have a different input context. Since the output context is set to evaluating menu, if the intent for HappyUser-Evaluating-Menu has a matching input context of evaluating menu, this intent will be executed, and all other intents with the same training phrases will not.

I’m going to add an input context of evaluating_menu.

When you add an input context to an intent, it will automatically add the output context and you’ll see I’ve set the context lifespan to 0 here in order to reset the context as soon as the intent is matched. When the context lifespan is set to 0, you’ll see the output context is striked out)

If the user is disappointed that we don’t have something on our restaurant menu, and they respond with something like “Damn” or “Too bad”, then we’ll also set the input context and output context in our Saduser-Evaluating-Menu intent with evaluating_menu and we can tailor a response.

You can see now that we’ve set the context in one intent, and then shared the context with other intents in order to create a relationship between intents.

Visualizing context

We can see how the output context set in the first intent and then we shared it with our other intent and set it as the input context:

Wait…there is a shortcut!

Now that I’ve explained how context works and I’ve shown you how you can set an input and output context for your intents, Dialogflow provides a shortcut for the scenario we just created:

User: asked a question
Bot: provided a response
User: sent another message related to their original question
Bot: followed up with a contextual response

Dialogflow refers to this as a follow-up intent and makes it really easy to create follow-ups by automatically adding the context for you.

I created a new intent and named it Delivery which will capture when a user asks if the restaurant delivers food. I didn’t set context, I just added some training phrases:

I then provided a response that asks the user what area they are in so the bot can tell them if the restaurant delivers to their area:

I saved the intent. Now, if you go back to your list of intents and roll your mouse over the intent you’ll see an option to Add follow-up intent:

Since I’m asking the user what area they are located in to determine if the restaurant will deliver to their area, I’ll want to create a custom follow-up intent:

Click Add follow-up intent and this menu will pop up!

Dialogflow will automatically create an intent nested under the Delivery intent we created and give a default name for the intent:

Click into the follow-up intent and you can rename it. I renamed it to Delivery — yes because I’m going to respond to the user if their area is somewhere that we do deliver food to.

You’ll see that Dialogflow automatically set the input context for this follow-up intent to Delivery-followup. If you also look at the parent intent we created and called Delivery, that Dialogflow also added an output context as Delivery-followup with a context lifespan of 2.

So, our parent intent links to our follow-up intent, because the input context of the follow-up intent matches the output context of the parent intent.

In my follow-up intent Delivery — yes, I added a response for those areas we deliver to by including an entity value of $geo-areas in my response. I had created an entity which includes all of the values for the local areas that the restaurant delivers to:

I also created another follow-up intent (custom) and I renamed it as Delivery — No. You can see both follow-up intents are nested under our parent Delivery intent:

This will capture any area other than ones in our geo-areas entity so we can respond to the user that we don’t deliver to their area. For this intent, I simply added XXX to represent ANY value that is not in our geo-areas entity, highlight the XXX and selected the @sys.any entity in Dialogflow. I also created a response that we don’t delivery to their area.

Save your intents and test things out in Messenger:

You’ll notice above that the user entered one area, and then after we responded that we don’t deliver to their area, they entered a different area and they still got an appropriate response. This is because the lifespan of our context was set to 2 and therefore Dialogflow knew how to response because the context remained active!

Summarizing

I wanted to use a template from ManyChat because it could definitely be enhanced with AI. I want to show you how you can capture free-form messages from your users and:

✅Know how to respond when you receive the same message. A response like the thumbs up can be in response to so many different things. Adding context to an intent can determine which intent will fire and how you’ll respond.

✅Determine how long the context will last by controlling the lifespan of the context.

✅Create relationships between intents by matching input context to output context. You may do that manually if you have a scenario that requires you to set context for multiple intents. If you simply wanted to create a sequential follow-up, that works more like a flow (but maintaining context), then you can use Dialogflow’s built-in “Follow-up” intent feature.

This is a multi-part series on how to add AI to ManyChat. For the complete series including videos and tutorials, go to https://www.janis.ai/manychat

A few things before you go…

  1. Share this with other ManyChat users if you found it useful!
  2. Join the Janis Facebook group and meet other Dialogflow experts
  3. Subscribe to our blog for more tips and tricks to add AI to ManyChat
  4. Oh yeah… Try Janis. It’s free to use and connect Dialogflow with your ManyChat bot.