Dialogflow Chatbot - Saving Data in the Realtime Database (Using the Inline Editor)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so building on top of my last video I'm now gonna show you how to save data in the real time database so we're using the real time database because it is the most straightforward one to use you can just drop in any Jason that you want if you have more complex data or more complex use case and you need indexing searching you might want to consider using firestore the new database which is soon currently in better so - in order to activate the database we just go to consult firebase google.com we select the project that we created in dialogue flow here which is in my case get name and then we literally just click start now and we can just use this default setting just activate that's how you do it and now I start going back to dialogue flow so what we're gonna do is in the last video we received the name of the user so now we're gonna save it in the database obviously be mindful what kind of data you save in your database don't save the name if you don't have to with gdpr and everything these days so this is just an example okay so the first thing we need to do is go to the get name intent where we recognize the name via the system given name entity and we're gonna remove the default text response here and we're gonna activate a neighbor what what we'll call for this intent I'm gonna save this before I forget so I moved the response here because we're going to use the web hook in order to return us a response the next step will be going to fulfillment but if all this will be disabled so you gotta enable it and we're gonna use the inline editor to edit our web book because it's gonna be only a couple couple of lines of code if you get more complex you can always do it on your local machine and push it using the command line tool that is provided by firebase but for this one this is good enough okay so these imports already here by default the only thing that I added was requiring firebase admin which I need in order for the database interaction and then I'm called a initialized app here which uses default credentials to initialize the connection which I'm not sure if it's super happy about I got some warnings here estimating the firebase config so you might want to be specific for a production app but it's just for demo purposes so it works without any config which is great also because I credit my my dialog flow agent a while ago I needed to update the admin and firebase function to the latest versions in order for the initialization to work I need to update to the newest version otherwise you need to very explicit with the config cool so again this is this comes out of the box little bit of boilerplate code and then what I did is I created a function save name which handles my saving of the name with the name recognition and then saving it to the database so the first thing that I'm doing is from the barometer that the system given name and T recognizes some I'm taking this here from the agent thought parameters don't name setting it in a constant then I'm doing this you're gonna see later why I'm doing this I'm just handing it over to another constant worker yeah we're gonna output the response here by calling agent at and then saying thank you whatever your name is so that is why I remove their response from the intent because this is the new response and now the interesting thing comes here where we save in the database okay so you see I just activated my real time database there's nothing in there and what we're saying here is push the name data to a walk to a collection called names which if it's not there yet it'll just create it so it literally just dump all the chase and all the data that you tell it into the database because this doesn't exist it will just credit you'll see in a moment and yeah that's that's almost it then this last bit is important as well we're gonna have to map the intent where the name gets right and gets recognized where we fill the name slot and then we tell it okay when this intent is triggered then use this function and that is pretty much it okay so I already Lloyd this last time so let's see how I'm saying hi hello what's her name PETA thank you Peter okay now if we go back to the real-time database we can see that the names collection almost added and a piece of data was added with the name Peter okay right that works so far I could leave you here but I want to finish a full working example off the back of the last thing that I showed which was not only using the system given name entity to recognize the name but recognizing all kind of names so we have this we have this loop here where if the name isn't recognized by get name with the system gives no manatee we have a catch-all and that is a system a need to recognize any name and then were because well the user could enter anything so we have to confirm then if that's the name and if they confirm positively whatever name that it will go to this confirm that yes intent so that is the second hook point where I need to save the name that has been successfully recognized so I'm gonna get rid of the text response here as well and activate the web worker saving this and then we just gotta remember a name of this intent because we have to map map this in fulfillment code [Music] you just gonna add another line here we're map this intent to the same function safely this will almost work not entirely that's where these other lines come in so because we're just confirming the name and not recognizing not slop filling the name and within that intent the name is actually saved in the context of the previous intent where we fill the slot with the name and that's why we read the context you so I'm I'm using the same function to both handled both intense and in the one case the name comes as a parameter parameter and in the second case it comes around to context and then I just added this logic here now you can see why I was saving the apologize to another conference name because rather simple logic here if the Rabb parameter exists then that means we came from the first intent get name if not then you will probably get the name by by the context which is this one and would come from the second attempt confirm then yes I'm gonna deploy this and the rest is the same so once we have the name we're just return the answer and push everything through the database cool might be worth adding us a further safety net at some point but for demonstration purposes yes this shall be enough cool so successfully deployed let's give it another try hello what's your name now I have to be creative then you they they earn the name detail which is not in the given name entity can you confirm that you trace your name yep I'm confirming that okay maybe what's still define let's give it another try all right there we go thank you it's interesting how it told me that it was done deploying but it still didn't work okay just try it again if that helps and now we can see in the real-time database cool that name was added as well yeah and that's it I hope that was useful as a short intro on how to save data I'll see you in the next video
Info
Channel: Peter Fessel
Views: 37,042
Rating: 4.8974357 out of 5
Keywords: dialogflow, chatbot, google realtime database
Id: imaA_s4jx9M
Channel Id: undefined
Length: 11min 15sec (675 seconds)
Published: Tue May 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.