DialogFlow Connectivity with a Database

Home Artificial Intelligence DialogFlow Connectivity with a Database

DialogFlow Connectivity with a Database
You are here, means either you are also new to Google Dialogflow or may be have not tried connecting DialogFlow to a database until recently.
What will you need for this, I’ll say, not much. You will simply need a google account, as long as you are not planning to use a lot of data. And if you are planning to use a lot of data, then you’ll need a billing account too. Also, you will need some basic experience with NodeJS, just to deal with the errors though. Your past coding with any JS or even Java will help.

The Problem
Now, connecting an end to end conversational API to a database, not such a big deal. Right?

So, what is the problem here? The problem is in the documentation. When a newbie, like me, we go to the documentation and search for an answer. The question being, which database can we use with Google’s DialogFlow? Can we use a SQL database? If yes, then how? Which packages do we have to use?

We go to sites. Tons and tons of sites. Surf through tens if not hundreds of websites. We go to Stackoverflow, we go to Quora, we try and find other DialogFlow communities. All of them have the answers, nearly all of those websites show us how to insert data in the database, how to fetch data from the database and how to manipulate data in the database.

But, most of the times it skips the most important question like, which database should be used?
If DialogFlow provides the ability to connect to a database in its architecture itself.

The solution
Now the problem is pretty simple. Which database can be connected with DialogFlow?
If you are looking for the answer in DialogFlow’s documentation, then sorry, but you are at the wrong place. I read, if not all, at least 80-90% of the documentation and found no answer to this question. Not even once they tried to explain, which database can be connected with DialogFlow.

The answer, according to me, is not in the Docs, but in the Fulfillment Tab. When we open the fulfillment, we go to the inline editor, we click on the toggle button to enable the inline editor.
In the already present code, the third line is :
const functions = require(‘firebase-functions’);
The point here to note is Firebase. Firebase Realtime Database and Firestore are two Databases that can be connected with the Dialogflow, without any extra effort or any upgradations. Just by using the inline editor. Which in most cases is sufficient for our projects.

Now again, a new problem rises.
When we open the firebase’s documentation, again we have no clue how to connect it with Dialogflow. There are some rules, some ways, which might be understandable to the experienced ones, but to people like us, or me, the newbies, it is simply put, OUT OF OUR SCOPE.

Though I did it, yet it wasn’t without failures, and not lying, I still face many problems and errors while doing so. I can’t provide you with a perfect solution to all the errors, but as a fellow coder, I am trying to help a bit.
How I did it

So, when dealing with the database, we have three main operations to perform. These are insert, update and delete. So, I am going to give you the basic syntax and the needed understanding of the syntax for these three tasks. Obviously, there can be more than one way to do what I am doing here, but I am here sharing what I found the easiest and most efficient for the newbies like me.

If you surf the right websites, you’ll find many more ways. But it’ll take a lot of effort to do so.
So, let’s start with the linking of the database to the code first.

Here, I am using Firebase Realtime Database, which is a NoSQL database, much similar to MongoDB and other NoSQLs, so if you have any previous experience in dealing with those, it’ll help, and if you don’t worry not, I’ll try and help as much as possible.

//This whole block helps you to import the Admin SDK to your project. This is necessary for all the modifications in the database
var admin = require(‘firebase-admin’);
var refreshToken; // Get refresh token from OAuth2 flow admin.initializeApp({ credential: admin.credential.refreshToken(refreshToken), databaseURL: ‘https://.firebaseio.com’
The Admin SDK lets you interact with Firebase from privileged environments to perform actions like:
· Read and write Realtime Database data with full admin privileges.
· Programmatically send Firebase Cloud Messaging messages using a simple, alternative approach to the FCM server protocols.
· Generate and verify Firebase auth tokens.
· Access Google Cloud Platform resources like Cloud Storage buckets and Firestore databases associated with your Firebase projects.
· Create your own simplified admin console to do things like look up user data or change a user’s email address for authentication.
Now, once you have imported the Admin SDK, now let’s start with our main goal and start making modifications in the database.
Now the first operation that you need to perform in the database is to make all the documents and to structure the database. To tell the system how the files will be managed.
So, at this point I am expecting you to already know how the file systems are managed in a NoSQL database. If you have the idea then let’s continue, else you may have to take a quick sneak peek to the basic structuring of a NoSQL database.
Now, let’s start with insert operation,
So, to insert some data in the Firebase Realtime Database there are mainly two functions that you can perform. These are push and set respectively.
For us to use any of them we first have to make a reference to the database. So that in case we are using multiple databases with a single agent, or if we need various instances of the database, we can do that.
The syntax to create the reference is,
// Get a database reference to our blog var db = admin.database(); var ref = db.ref(“path”);
If you do not want to use this you van simply use the complete path every time to go to the reference,
// Creating a complete reference
var ref = admin.database.ref(“path”);
When we use the set operation, what it does is, it does not create any new block or row, it simply replaces the previously present block there. It can be done like,
// Putting the data in the database
Fieldname1: “field value”,
Fieldname2: “field value”
Now, if we want to add a new field/block every time that we pass the data, we can use push operation. Whenever we use push operation a new unique ID is generated and the data is updated in the block. This new block can be understood as the child, which can be named by its unique ID.
The syntax for doing so is,
// Putting the data in the database
Fieldname1: “field value”,
Fieldname2: “field value”
Now after insert operation, the second most important operation that is needed to be performed in the database is update operation.
We have to note that there is a major difference between update and put operation. Though both of them changes the same block, makes updates in the same field, the difference is the put operation changes the block as a whole, it may have different number of child fields, but when working with the update block, the number of the child fields have to same.
Now to update any block we simply use update keyword,
// Putting the data in the database
Fieldname1: “field value”,
Fieldname2: “field value”
This piece of block will change the previous values of Fieldname1 and Fieldname2 with the new values that are provided by the user, though here we are not sure where the data will be updated, as we are not putting any checks to decide which block is to be chosen.
There are a lot many plot holes which you will learn while using the dialogflow and firebase together, like when you’ll push the data, the data will be updated many times because of the language being an asynchronous one. You’ll have to use once or on keywords to settle that. You’ll also have to use the concept of promises to manage the code flow.
Now, for the final task, I am going to discuss about deleting something in the database, this has to be done properly and only when necessary, because deleting something important in the database can often lead to major discrepancies in the database, which is often not promoted.
I myself haven’t tried using this operation but have studied about this in the documentation and also in many stockoverflow pages and other sources.
So, to delete any data in the database, we have to use the keyword remove,
// Deleting data at the source
Now hit your business logic and let me know if you get stuck somewhere. I’ll try my best to help you. Trust me, I will, this is my First Story.
Also, this fulfillment tab, especially when you are working with the inline editor, the engine works with an old version of NodeJS, so even though newer versions of NodeJS doesn’t need any semi-colons
This version needs the semi-colons.



Leave A Comment

Your email address will not be published. Required fields are marked *