Dialogflow 3 – Calling a fulfillment to process information

Dialogflow 3 – Calling a fulfillment to process information


Let’s continue with our Dialogflow tutorial. As we have seen, with intents we can define answers to our users, but if the conversation flow gets more complicated, or we need to find information in any external source, we would need to do some programming to make it happen.

Update: To clarify where we are I’m adding this diagram that shows the basic architecture of a Dialogflow bot. What we are doing in this tutorial is programming the Cloud Function on the right to generate answers and integrate external services with the agent:

To enable the use of fulfillments you need to select  “Enable webhook call for this intent” in the “Fulfillment” section of our intent:

As you can see I have added a default “Text response” which will only be provided to the user in case the fulfillment is not working, the text is in Spanish but it is just saying that the systems are not working at this moment.

If we go to the “fulfillment” section of the console, and cleaning all the default comments, we would see something like this:

We can use two different types of fulfillment, an external one that it is called Webhook, where we will have a web service that needs to work with the Dialogflow Fulfillment API, which is documented here, or if we go for an easier option, we can use the inline Dialogflow editor, which will deploy a Cloud Function using node.js.

As we can see in the sample source code, where I have only added the shadowed part, we define the objects needed to interface with Dialogflow and an “agent” object is instantiated to pass information to our chatbot agent.

At the Botton you can see the intents mappings from our Dialogflow intents to the functions defined in this code. As an example we are triggering the function “testFunc” each time the intent “test” is triggered.

Our “testFunc” is only putting some diagnose info in the console, and then providing the phrase “Test, 1, 2, 3, …” to the user with the function agent.add().

The next example is more complex, and is what we are currently using for the “reservation” intent in Hoteles Martínez:

In the top section you can see that the text answers are defined as contacts, that we will latter add to agent.add calls. This allows me to have a cleaner code and to add other languages easily in the future … sorry, for the moment answers are only in Spanish, but English will come later in this tutorial.

Another relevant thing, in the highlighted area, is the use of the parameters captured in the intent; for those of you who remember the previous post they were reqCity and reqDate. In this intent the parameters were not mandatory, so we need to check for them in the fulfillment, and if they are not there ask the user for them using the phrases ask_city and ask_date. With the use of contexts, that we will see in the next post, we will prepare the intent managing the user answer.

If we have reqCity and reqDate (or their equivalents in the context resCity and resDate) we will ask for the length of our stay.

As a recap, our fulfillment will check for the parameters and request the missing ones.

We can do much more complex things … for example in post #5 (it was posted in the second place) you can see how to connect to an external data base and get information from it in our fulfillment.

Finally, for those who want to understand what is going on behind scenes, when you click “Deploy” in the fulfillment, what is happening is that a Cloud Function is being created/updated and you can access it in your Google Cloud console for your project:

You can find more information about Cloud Functions from Google Cloud here … in 7 words, they are a Serverless application running framework..

In the Cloud Functions logs you will see the output of what is going on in your functions, and will also see the same content in the Firebase logs (link at the botton of the inline editor) … those are 2 interfaces that will provide the same information. To properly log what is going on you will see the following lines in my code:

if (DEBUGLOG) { console.log(‘Starting Reservation function’); }




I work for Google Cloud, but this post are personal ideas and opinions

This Post Has 6 Comments

  1. Alma

    WOW! I love your Dialogflow tutorials! You make it easy to understand Dialogflow. I didn’t find much information about how to work context in the fulfillment until I found your tutorial. I hope you keep working on them, I saw you stopped since January…

    1. jamarmu

      You are right, I’m afraid this is only a part time job. If you have any topic that you would like to see here, please let me know.

  2. Marcelo

    HIi there,
    Just arrived, and congrats nice work you are doing here!
    I have no experience in dialogflow either google cloud functions.
    But I know a little javascript.
    I am in trouble trying to debug my code!
    I noticed you used “DEBUGLOG” flag in your code! Where is declared?
    Where Can I see my debug code?
    Its possible to print debug code on the dialogflow right pane (paramters)? thats an easy way to show vars!
    thanks for your help,

    1. jamarmu


      The DEBUGLOG is defined as follows at the begining of the function:

      const DEBUGLOG =”1″;

      Nope, as far as I know you can’t log on the simulator window. I monitor the Cloud Functions logs … you can access them both by entering the Firebase console or the regular Google Cloud console and then looking for Stackdriver.



  3. Louis

    Hi there, how are you?
    my name’s Louis. I’m studying at Victoria University. Nowadays, I’m implementing a chatbot and integrate with Facebook messenger. Now, I try to retrieve first_name or username from Facebook messenger without asking users. I knew that first_name or username is default data that Facebook provides for me but I doubt how can I show or get this data by coding. Moreover, the problem that I need to hold time until users give their answers, maybe around 2-3 mins. This problem, I found out that sometimes when I waited for users responding, I got this message “Webhook call failed. Error: DEADLINE_EXCEEDED.” but I can keep going to wait for users responding to my question. it means for a while when users back to answer my question, the Dialogflow can continue in the process. Unfortunately, sometimes the session’s been timeout and cannot continue. Therefore, I don’t understand and cannot get a conclusion from my observation.
    Thanks a lot for your nice to provide a lot of knowledge and thanks to advance for your answer.

Leave a Reply