How to manage context from your Python webhook in Dialogflow ES

First published: 2017 | Last Updated: May 2022

This tutorial used to refer to inline webhooks before. But I have updated it to use a simple Python based webhook for two reasons: I don’t recommend using the fulfillment library and Python is better suited than NodeJS for Dialogflow bots.

The problem

A little while back I wrote a post about getting the user’s complete birthdate in DialogFlow. Turns out, the issue is somewhat non-trivial. Since DialogFlow will accept a date with only a month and a day (e.g. March 15th) as a valid input for the entity, the user may not enter a year. The real issue, of course, is that DialogFlow will automatically append the current year as the year in the entered date. This is not just missing information, but it is actually wrong information.

The date.partial field

One way to handle this is to get the user’s input as a, and make the parameter value you extract to be $date.partial. Check out the link above to see a little animated gif which shows you how to do that. For now, I will proceed on the assumption that you have already done that.

Validation on the webhook

At this point, we will be validating the input to check if the $date.partial captured the full date or only a partial date. If it captures a partial date, then DialogFlow will send a message to the user asking for the date. On the other hand, if it captured the complete birthday, DialogFlow will simply send a message prompting for the next response.

The flowchart

As I mentioned in previous posts, I suggest creating flowcharts for your chatbots using Mindomo.

So here is the Mindomo flowchart which describes this flow. You can take a look at this video course to understand the conventions I use in the flowchart.

Notice that the next input we wish to get is the user’s last name. Usually the way we build such a conversation in DialogFlow is by setting a context with an appropriate name.

Defining the intents

Let us define our intents one by one.

First we create an intent called book.appointment

Next we create the provides.birthdate intent

Download this intent

Next we define the provides.birthyear intent

Read the full article

About this website

I created this website to provide training and tools for non-programmers who are building Dialogflow chatbots.

I have now changed my focus to Vertex AI Search, which I think is a natural evolution from chatbots.


BotFlo was previously called MiningBusinessData. That is why you see that watermark in many of my previous videos.


    1. This question is too general to answer in a comment. You can search my website for specific help, or if it is urgent you can get in touch for paid 1-on-1 video consultation via my Services page.

  1. A useful post – thanks Aravind. One question (for now).

    As you say the last webhook function doesn’t do very much (sendBirthYearResponse). I can’t see what the point of it is to be honest as it doesn’t check anything and just sets up the output context and response to be the same as the Dialogflow intent ‘GetYearOfBirth’ could do – why use a webhook in this instance? Thanks.

    1. Good point!

      The main reason is that if you got a partial date in the first input, you will get the birth year as a separate value, which usually means the webhook will need to handle it a little differently (versus getting the entire value in a single parameter).

      Said another way, if you choose to omit the webhook function call at this step, you will still need to add some other logic downstream to see whether user entered full birthdate vs partial date + birth year. Quite often, you will see that it is better to handle it right away. Besides, what if the year entered is a negative integer? You will need some validation for that too, since there isn’t an entity to represent just the year alone in Dialogflow.

      You can also skip the webhook call, as long as you are clear about this distinction and can add the appropriate logic downstream.

  2. I have taken user input email from user and i am checking my user input to database consists of email or not .If database consists of email given by user then i will proceed to next intent using contexts but email doesn’t exists in database then i have to say thank you .How to solve this problem can you tell ?

    1. Perhaps I should have been more clearer, but the resource files are available as part of a paid course (now I have updated the post to indicate that).
      Having said that, everything you need to create the agent plus the intended behavior is mentioned in full detail in the tutorial itself. If you want to speed up your workflow a bit, the resource files can help.

  3. I’m setting context like this, but when a user is fast with his/her input, the input arrives before the context is set and therefore the next input is not mapped to the correct intent. Any idea, how i can wait for the context to be set?

    1. I don’t think there is an easy solution to this problem. You need to train your users to only type after they receive the response. If you control the user interface (e.g. website chatbot), you can also disable the input area until the response comes back.

      1. Thanks for your answer. I wait for the response from dialogflow, but still the same problem. It seems the processing takes to long.
        What i do is: Sending the request, wait for it with .then() and afterwards sending the message to the user, like “please provide your birth-year”. But still, sometimes the answer of the user arrives before the context was set?

        1. I am not sure I completely understand your question. If you are interested, you can get in touch for coaching via Skype (Services -> Get in touch).

  4. Hi Aravind,
    i am using javaclient to set a context and its parameters.
    I see that I can read it in the console, when I choose Text response.
    But how do i read the context and params, when the intent calls a webhook.
    I am using v2 for the req/resp format in the webhook.
    Can you please help?

    1. I think you might want to check out this article. The Java client is meant for integrations, and generally doesn’t have any need to know about context and params, which is actually handled by the webhook (sometimes there are exceptions though).

      Also, you can get in touch for 1-on-1 Skype coaching if you need urgent help (go to Services on the menu -> fill out the contact form).

Leave a Reply