Hi,
In this post we are going to see how to connect a Dialogflow fulfillment with a database in Google Cloud to get information that we will use in the bot.
The idea is to simplify the programming of the answers in our agent, so we don’t need to code all the answers in Intents or in the Cloud Functions, and we can maintain the answers in a central repository, where we can make changes and add additional languages when needed.
For the database we have a number of options, but with the same philosophy of building a platform that is easy to manage and escale, for this example I will be using DataStore. It is a NoSQL database, where the data is arrange in Kinds, something similar to tables, where each row is what we call an “Entity”. The contents of each row are divided in Properties, as you can see in the following form taken from the Google Cloud console:
In this NoSQL database, we can add properties on the fly, getting additional flexibility in our bot development. In my Martinez Hotels example I only have the following 2 entries in my database:
You can see that for each hotel we have the following properties or fields:
- name
- city
- Description
- stars
- price
- hotelURL
- picURL
For the moment the price will always be the same, hopefully we will have time in the future to create a reservation system that will provide a price based on the demand for Martinez Hotels 😀
The searches are going to be done per city, as the users will request to have a room in a particular city, and the database query will only provide the first entity matching the city in the query.
The code to do this in a Cloud Functions with node.js in the Dialogflow console is the following:
The first 2 lines create an object to access DataStore, and then we create a query to search for Kind “Hotels” and filtering by “city”. The result is an array with all the entities in the DataStore that match the filter:
To call this function you need to properly manage “promises” in node.js, so the query execution is synchronous … In the main function, the way to call the function making the database query is something like this:
Promises and async execution is a complex topic … you can read more here.
As you can see I’m not managing the possibility to have several entities from the database, but it would easy to handle. For the first object we then collect the different properties in new constants that we use to create the answer to the user in a function called “answerWithCard” and to set the appropriate context for the following intents.
In the catch sub function we handle the errors in case we don’t find an hotel for that city or there is any other error accessing the database … again you can easily differentiate the 2 cases to improve the answers.
I will stop here, I hope database access is clear now.
Regards,
Jamarmu
============================================================
I work for Google Cloud, but this post are personal ideas and opinions
Hello! Is it possible to see the whole fulfillment code?
Hi, sorry, no, I don’t share the whole code because it is not good enough or error checked to be something that you would deploy. If there is any part of it that your are looking for just let me know.
obviously like your web-site but you need to test the spelling on several of your posts.
Many of them are rife with spelling problems and I to
find it very bothersome to tell the reality on the other hand I will definitely
come back again.
Thanks for your feedback … the truth is that I write the posts in Spanish, and then translate them into English, and always in a rush. I will try to make it better. If you find any part that is not properly understandable please let me know !