 Welcome back to another my microsoft sound bites and today we're going to be talking about get response And so this will allow my craft to have a little more of a dialogue with the with your users and kind of make some decisions So it can pose a question and then make a response based on the answer that it receives And so we're gonna be focusing on that and validation of the user responses today So with that, I think we'll jump right in we went ahead and pre-created some of these files And so just so that everybody has an idea of what's in them I'm just going to cap them out to the screen just to kind of save us some time So if you're really interested in what's happening here you'll be able to pause the video and Take a look for yourself. The other files haven't changed the request and the intent, but I'll cap them out there just for thoroughness and With that, I think we're going to hop on over into the init file So this this probably looks pretty familiar as the you know It's the thing that we've been looking at for a few videos now some of those minor changes that stratus mentioned But today we're gonna add that get a response in because we want to start asking the user questions So you can see here when the user, you know, the user's asked a question or you know Triggered an intent to ask for an ice cream. We welcome them to the store and now we're curious about whether they want any toppings so what goes in here is a Dialogue file just like up in the welcome up here So we're going to go ahead and put in our dialogue file and the dialogue file I believe we're going to be doing the toppings request And you don't need to have the dot dialogue What else does get response do or is this all we want it to do right now? Well, just like speak dialogue you can add in a data object if you have some dynamic data that you want to Inject into that dialogue, but for this one We're just going to leave it as the toppings request one of the big differences between speak dialogue and get response Is that get response will return the utterance? So this is going to trigger the microphone that it'll read Topic, you know, whatever's in toppings request dialogue We'll trigger the microphone and prompt the user to respond to that and it's going to return Whatever whatever the user says as the return value from that method so we can assign that to a variable Yeah, perfect Maybe let's log that out and log out the topping response just so we can we can see that something has come back perfect So remind me again. How do we only see the output from our skill? Yeah, so we're going to use the colon find command And then so that we can see not just the logs from our from our skill class, but also Anything related to our skill we might do c e cream. So it's because it's case sensitive You know some things ice cream with capital i something's ice cream with the lower case i this should capture everything If we reload the skill we should see that reload great perfect There's our info log our get response is working. It's it's logged the result But currently we're we're still telling the person that they're just getting a chocolate ice cream. So we can probably extend that final speak dialogue And um, and we'll add in the the toppings into our final speak dialogue And so in this case We have already um pre-prepared the file. So as I showed right at the beginning of the video So we're not going to worry about that If we remember from our previous lessons if you use the control key in the left and right it will circle through your history So to save you some typing There you go. Um as you saw sometimes uh because the microsoft when you're doing the get response It's triggering The microphone listener. So if you if you type a response before the microphone listener is open Then it's not going to hit properly. Um, so you kind of have to wait and pretend like you're a speaking user For the purpose of this video. We've actually turned off microsoft's uh Audible feedback in order to just not clutter up the audio feed here So we're taking a guess you have to hit it just right so that you've waited enough time for the trigger But not too long that it times out great. We've got to get response We're getting the response from the you know the response utterance from the user And then we're able to provide that as part of our final speak dialogue But at the moment we could say anything and You know the the ice cream shop is just going to give us a a flavored ice cream with whatever type of topping we want Whether we have it or not, whether it's the legitimate topping or not So, you know, we could ask for a chocolate ice cream with puppies on top and uh our skill would at the moment Provide that to us, which is probably not what we want. But the Get response method also has the option to validate the response, you know using using a function Let's add a validator in So I imagine the first thing we need to do is actually decide on what it is that we will have in stock for something to Validate against so why don't why don't we uh take that offline and we'll come back and have a nice list for people Of the toppings that we're going to provide to the store So there we go, uh, we've decided on only a few Toppings because we're a very poor store and kovat doesn't allow us to restock very quickly So this is all we have to offer today And so now we we need to be able to check, you know, what comes back from the response Against this list. So what we're going to do is we're going to add in a function that's going to do that for us Is our toppings validator that we wrote earlier. We've done a few Example versions here. You can see the first line's commented out. It's kind of the simplest possible Version of a validator that you could do you could have in this case So the validator needs to return a boolean a true or false of whether the response is valid or not And that first commented outline Says does that response is it is it in self dot toppings at all? So if the user replies Nuts or gummy bears or chocolate chips or anything in that list, then it's going to say yes true And return true and it'll be a valid response if it's not in that list or if the if the person Wants to have two toppings so they say I want to have sprinkles and nuts then It's going to return false because the entire response is not in self dot toppings So it's a it's a very limited version So what we ended up with was moving to a slightly more detailed version Where we first assign an empty list so that we we've got something getting ready We then look through all of the toppings and check if that topping Exists in the response if the user responds with yeah, sure I would like whipped cream and gummy bears Then it's going to go through the list and say does sprinkles exist in the response? No, does whipped cream exist in the response? Yes I'm going to append that to the requested toppings list and then continue on sort of go through all the toppings Add them all to the list and then we return that requested toppings list What you'll also notice is that this is not strictly a boolean But an empty list will evaluate to false Whereas a populated list will evaluate true So it still kind of meets that need to to return true or false And then yeah, the final the final commented learn Is is an alternative way of doing that that same thing You know, they're both completely valid Whether you whether you wanted to use a for loop if you find that more readable Or if you want to use a list comprehension And have that much tighter either way is is really fine The other thing that we would add to this if we were making a real skill is a Doc string for our validator to describe exactly what it's doing But great, we now have a we now have a validator That's going to check a response and tell us Whether it's valid or not and we can use that in our get response method By adding another argument of validator So we're going to add that to our get response method as an argument of validator equals And it's equal to the name of the function that is going to validate our response. All right We now have our validator on in our get response method. So if the user responds With whatever whatever they respond with it's going to get past to that validator function And tell us whether it's a valid response or not But if it's not a valid response We probably want to provide the user with a bit more information Rather than just bugging them with the exact same question Chances are that, you know, we can let them know that we don't have that particular topping in stock Or you know, we might even provide them a list of available toppings or any number of things But we can do that with the on fail argument And so that we can pass again another dialogue file and that will be read If the response provided Was not valid. So if the if the validator returned false it would read The topping missing dialogue and give the user another chance to respond to the question And then finally we can we can specify the number of times The user gets to respond to this. So if they if they continue getting it wrong We'll try in this case two times before we just fail and say No, that that didn't work. The user's not getting a valid response back You can set that to any number if you set it to negative one It will retry indefinitely. Obviously You want to be pretty Pretty cautious with that, but uh, but it is a it's it's possible Cool. So let's let's give this a try So we're going to try and trigger our Validator function that we just wrote. So we're going to have to trigger this in The same way that we used to by first triggering the intent It's going to ask for toppings and we're going to say French fries And we can see that it did actually have french fries. So I'm going to add it in and say Nuts which we know is in our list and so you can see that we actually Triggered our failure events and then we passed after multiple retries and there's the output So there's a few other things that we can do to you know, make this You know just that a little bit better like at the moment We will just take whatever the topping response is and feed that into our final speak dialogue And we probably actually want to Get the list of requested toppings that we've validated and pass that in instead So we might quickly cut and paste that Here we have, you know an extended version of of our Intent handler So we first check if the topping response is not none So that there is no response to our question then we just provide the ice cream without toppings But if they do respond then we're going to reuse that validator function to extract out You know the various toppings that they requested so this way they can request more than one thing at a time To turn that list into more of a human speakable String, you know rather than a list of a list of strings. We want a single string Spoken to the user for that we're using join list So we import that from microsoft.util.format And then we pass it a list of strings and a joining word So in this case and and and that will be Translated into the user's own language But what this does is if the user asks for whipped cream nuts and gummy bears it's going to take that list of strings and Add commas in between them and then put the joining word before the final one So that will turn from a list of of three strings one two three two one comma two And three which is you know how how it's normally spoken and then we we can pass that Formatted string into our dialogue file The other thing that we want to note here is that we now are using both the width or without toppings So that when the shopkeeper returns with your ice cream, he doesn't say With none. So for example In the case that that the list is none and we ran this dialogue It'll say here's your chocolate ice cream with none toppings So we have two different dialogues here to handle Whether or not toppings are selected Yep, and this is this is a nicer way to do it than to try and add in words like toppings Into the one dialogue file, you know, sometimes you see skills where They'll join the list and then add the word toppings on the end Which works if you're only ever going to use this skill in English But if you open to people all around the world using your skill Then it's it's much nicer to use the the different dialogue files so that they can be translated effectively All right, let's let's do one last run. We'll show The our new extended version of the skill working. So we'll ask for some ice cream And we'll ask for some toppings of So we've eroded out once second time We asked for whipped cream and sprinkles, please And then it's responded with just the sprinkles and whipped cream So it's extracted those two toppings correctly Provided it back to us as the answer. Perfect Shall we test the fail? Yeah, let's let's test the complete fail the customer has sat on their phone while they're ordering and they didn't hear your question and they're not responding Great as expected I didn't respond to the question and we got a chocolate ice cream with no toppings back So that's all working as expected Perfect. So is there anything else that we wanted to cover here in our skill or are we pretty happy with the results? You know, I think, you know, there's so many ways that this could be improved, you know, if this really was an ice cream shop You know, there's there's edge cases that we that we wouldn't be catching This gives a pretty good overview of the basic use of of get response Well, more than the basic use I think, you know, you can use it just with the with the dialogue file But um, the validator function, you know, it can be a really powerful thing if you if you use it correctly Yeah, sounds good. I look forward to adding this into my next skill All right, that was get response. So, uh, you know, using this we can we can not just Take an intense and respond to the user using dialogue We can also ask the ask the user questions and start to get into a bit of a conversation Then if we start to get a bit more advanced, we can use a validator function to check that response and Make sure that the user's giving something that we expect and if it's not then we can provide a bit more context a bit more Direction to the user about how they might be able to to get to the next stage of our skill You know, you can obviously just take the response and then Process it after that happens and and then, you know, do things based on that But this allows a very quick way of staying in that conversational mode And getting to to a valid response that we can use I think, you know, there's there's a few other methods that we have available in On the micro skill class that utilize get response. So they're they're under the hood they get response But they just have some some magic around them to Make things a little bit easier. We'll have a look at those in in the next video But for now, uh, that's everything you need to Ask the user a question and and have microff respond Excellent, then I look forward until the next time Until next time. Ciao