Dialogflow Chatbot - Read & Write Realtime Database - Calculate Average User Age

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so because people have been asking for it I'm gonna show you now how to read from the real-time database what I'm gonna do is create a chat bot that will ask the user for their age and then we're going to calculate an average age over over all of the users and so first I'm just going to show you how to read from the database and then we're going to use a transaction to read and write to the database in the same operation okay so what I've done here is I've created a new chat bot save average age I just wanted to point out that I'm using the API version 2 in this case so in some of my previous example have been using the version 1 so I'm using the new version now and let's go straight into the intense so as usual we're gonna start with the welcome intent this is actually the default work I'm intend after screen ended and I've given it an output context or asked for H so just so that we can trigger the interaction you've seen this before so now I'm going to create a new intent a stage that will the ask for H context as an input only that output so what do we need you we are going to ask the user for their age so we'll just need some training phrases that's prefilled that's great another object could be I'm 90 years oh yeah sure I should have created the first I'm gonna read that okay so let's create a new parameter H that will be using the system number integer entity and I'll use image so now I'm gonna assign that here in the training phrases [Music] we don't really need this I'm gonna throw it out anyway actually cool we're not gonna put a response here because we're gonna use this for fulfillment a webhook megawatt of coal yes that should be it let's say this one thing that I forgot previously to point out is that the work um intent has a response which is the prompts can you tell me your H Oh so next let's go into the fulfillments to implement the bulk of the codes I've already activated the inline editor here and prefilled it with some code so that I don't have to write everything so let's first start so as in my last example where I was just writing to the database initializing the database connection here what happened this timer is I got an error when I just called initialized app with the with no arguments so I think something has changed so it doesn't work with just defaults anymore so we have to be more explicit so for credentials I'm still using a application default method here and then for the database URL I'm going to show you how to get that so what you need to do is go to consoles of firebase google.com and create a database I have previously credited I've just deactivated it so now I'm going to enable it again now it's enabled and you can see that the URL is shown here so what we would do is just copy that and then the only difference would be that we're going to replace the HDP with WS for WebSockets which is the protocol that's been used to you cool so that's your work this is pretty much the boilerplate code when you create the project then I've created a function handle H and here at the bottom mapping the this function to to that function sorry I'm mapping the ask agent end to that function then so remember we have credit the parameter age so I'm reading that from the agent or parameters here and I'm putting in what first of all if the user has input their age in one sense thank you and then the first thing we're gonna do is just read something from the database not write anything so we're going to read the average so remember I said at the beginning we wanted to save a running average of all the ages so we don't want to say individual ages just an average so what I'm doing here is I'm reading the rest H in full and then once the value is there we there snapshot here the the callback function and then we're reading the running average I will put a link in the description with all the coach so that you can look at it in yeah in more detail and just copy it so you don't need to pause the video right now okay so what do we need to do we need to create this ref and also this value so let's do that what's happening now we're just going to reload okay there we go so the ref we want to call H info and then we're creating a cheer child's average running average which it's going to be zero in the beginning and then I'm just going to do one more thing which we're going to use in the next part which is we're also going to go back what they're going to need eight total which is going to have the number of submitted ages we'll need a we'll need to keep a count of that in order to continue to calculate the average so we've got that so now let's deploy it is yeah yeah I'm just giving an output I recorded average age just average age which we're reading from the snapshot so deploying deploying deploying hi can you tell me your age let's say I'm 18 years old doesn't even matter thank you there's something wrong let's forget the log no errors here let's try it again oh yeah I just needed a second thank you all recorded average ages viewer which is the average age that I input here cool so that is reading from the database and now let's go one step further and actually what is really boring to just not change the average age so the line to get one step further and read and write so modify the average age and then output average age so I wish this browser editor and it would support the shortcut for commenting on commenting if anyone has a tip on how to do that please let me know for now I'm just gonna have to do this line by line [Music] okay so we're doing a similar thing here we're reading the Rach H the ref H in full but now we're using a transaction which is yeah it just makes sure that the database stays in a consistent state and all the all the other operations get executed before another chatbot client could potentially write to the database I'm also going to put the URLs to the relevant parts of the documentation in the prescription so it can read more about transaction and also snapshots when you click on those links I had a console log view which I'm just going to get a little off so the transaction once the data is available it will give us the H in full it's similar as to the snapshot here we're just making sure here that it did exist which it does cuz I credit it but in case they got deleted we don't want the whole thing to crash so that's here that's security then I'm saving some data in variables so the old average I'm reading that from from the objects the running average and the old total count which is the total count that I input into the database here it's all 0 at the moment yeah sorry about that it's great when things are just popping up without asking for it and then we're using those old values to calculate the new average that's a bit of an interesting parts not really part of this tutorial but it's interesting anyways so what I'm doing here is taking the old average multiplying it by the old total count because it was an average first and we're basically waiting the new age that has been rate set with the old average and we need to multiply it by the total count to get the weight right and then we're dividing it by the old total count plus one which has been leaving new total counts to get the new average we're saving that here and then increasing the total count in the database in the transaction and in the end we return age info so we just returned the whole object and then the database will update with whatever values I changed here and in between we are outputting the average recorded average age is the average well so I'm gonna click deploy everything should be fine so now we're still saying thank you but then we're flying with the new average let's try that [Music] okay it's done hi can you tell me Rach yeah well so let's say I'm 19 thank you all right our recorded average issues ooh I have this last time it usually takes a while we can check here there's an arrow there's no error so I'm just gonna try it again nope still no okay I'm gonna reload not sure what's wrong as we can see here is still no error so I'm assuming it's just as like delay uh-huh there we go okay so now I told it that my age was 90 and then it said I would call that average is 90 if we go to the database we can see that average was set to 90 and the total count is 1 because there's 1h submitted now I'm gonna do this a couple more times and I'm gonna say ok my H is actually 2 e so now that much H has dropped to 60 and let's do one more it's just 30 and now the average I just dropped again so we can see here that the counter goes up and the average is kept up-to-date as well cool that is it's like that I'm gonna put a link to the code in the description and also link the parts of the real-time database documentation so you can find find it easily let me know in the comment if you have any questions thanks for watching
Info
Channel: Peter Fessel
Views: 50,598
Rating: 4.8488665 out of 5
Keywords: Google Dialogflow, Dialogflow APIv2, Realtime Database, Firebase Functions
Id: xWpHVbMnBXI
Channel Id: undefined
Length: 16min 6sec (966 seconds)
Published: Sun Jul 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.