Making chatbots reply smarter with context using Dialogflow Fulfillment

In the previous article, I wrote about why good chatbots need context instead of tree-based flows. The benefits of introducing context are that users can engage in a more natural dialogue with your chatbot, get direct replies and change information without restarting the conversation.

I’ll be using Dialogflow and Cloud Functions for Firebase to describe and explain the implementation. The ticket price inquiry example is based on the scenario described in my previous article, so take a look if you have not.


1. Instead of one intent with the required slot filling parameters, create that intent followed by one intent for each parameter. (See purple boxes above)

2. In those intents with a single slot filling parameter, set it optional.

3. Put all entities extracted from any intent into the conversation context programmatically. (See the blue box above)

4. Make a functional response for a group of related intents (see the orange box above), so that you’re making a chatbot to reply based on the user’s intent and information (either mentioned or referred from context), instead of intent without information.

Let’s take a closer look at the code.

Intent mapping

Start by creating a map of intents. Let the agent (a webhook client) use the intent map to handle incoming messages.

Remember to create those intents in Dialogflow and turn on the webhook fulfilment.

Intent fulfilment

Next, for each intent that acts on the customer’s query (such as the ticket price inquiry in this case), the chatbot will reply based on the number of participants (children, adults and seniors), the site they wish to visit, and the citizenship.

  1. Extract both the slot filling values (parameters) and the context parameters found in the request body.
  2. If the customer has explicitly mentioned the site, the number of people and/or their citizenship, use that. If not, recall from the context in the current conversation session.
  3. Make a functional reply using those parameters, such as replyTicketPrice(agent, citizenship, participants, site) .
  4. Finally, keep all (and new) parameters mentioned in the context.
    agent.context.set({name, lifespan, parameters: { …currentParams, …newParams}}) This is helpful when the bot goes back to step 1 and 2, preventing the user from repeating the details.
  5. In many cases, a chatbot is designed to fulfil a wide range of customer requests, so a good idea is to assign a topic name in the context to keep the current conversation relevant (be it about ticket price inquiry, membership registration or something else) in case the user informs the chatbot about a different site or a different number of visitors.

Trending Bot Articles:

1. How Conversational AI can Automate Customer Service

2. Automated vs Live Chats: What will the Future of Customer Service Look Like?

3. Chatbots As Medical Assistants In COVID-19 Pandemic

4. Chatbot Vs. Intelligent Virtual Assistant — What’s the difference & Why Care?

So how should the bot exactly reply? There are 8 possible ways to reply, based on what parameters were given, and it’s up to your conversation design.

If the customer comes with a short and sweet message like “ticket prices”, the chatbot will ask for one of the required parameters. Or if the customer says “ticket prices to the cloud forest”, there’s an equal chance that the bot will ask for the number of the participants or the citizenship. All bot replies are in the form of questions, which will elicit a relevant answer from the user, otherwise, the bot will tell the customer the ticket prices right away if all are known.

What happens when the user says “2 adults, 1 child”, “cloud forest”, or “tourist”? The respective parameter-based intent is triggered, not the ticket price intent. In this situation, the chatbot will invoke the replyTicketPrice response based on whatever information is passed to it each time. There is also the possibility that the customer starts talking to the chatbot that they are “interested to visit” without a specific purpose like inquiring about the ticket prices, so the bot may ask “Which site?”.

The fulfilment of this parameter-based intent (site) shares the same design pattern for the other two (participants and citizenship).


Context is a great way to carry important details from intent to intent, especially if the customer changes information, interrupts the flow by going off-topic, or wants the bot to complete multiple requests.

You’ve probably looked up local weather info using digital assistants, and then asked “what about (this city) instead?” and still get the weather forecast. That’s context at work. Can you think of other use cases too? Are you thinking of re-designing your bot dialogues? Or do you have different ways to accomplish context using other bot frameworks?

Opinions expressed are solely my own and do not express the views or opinions of my employer. If you enjoyed this, subscribe to my updates or connect with me over LinkedIn.

Don’t forget to give us your 👏 !

Making chatbots reply smarter with context using Dialogflow Fulfillment was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.





Deprecated: Function WP_Scripts::print_inline_script is deprecated since version 6.3.0! Use WP_Scripts::get_inline_script_data() or WP_Scripts::get_inline_script_tag() instead. in /www/eksidoio_182/public/wp-includes/functions.php on line 5453