From the course: Developing Skills for AWS Alexa

Responding to requests with intent handlers - Amazon Web Services (AWS) Tutorial

From the course: Developing Skills for AWS Alexa

Start my 1-month free trial

Responding to requests with intent handlers

- [Narrator] Now that we've created the interaction model for the order pizza intent, and given it at least a good start on the number of utterances it needs to map to the intent, let's take a look at what happens when we try to actually invoke it. So, we're not going to use the utterance profile anymore. Instead, we're going to head to the test tab. Let's say open Pizza Dog. - [Alexa] Welcome. You can say hello or help. Which would you like to try? - [Narrator] As long as we're here, why don't we modify the launch intent response. We don't want it to say that anymore. Head back to the code tab. Instead, let's say welcome to Pizza Dog. What can I get for you? Remember, we're under the launch request handler. Save, and deploy. Meanwhile, we can go on testing. Let's use one of our utterances. Go back to the build tab and you can see I'd like a size crust pizza with topping. Let's try that. I'd like a large pepperoni pizza, oops. I'd like a large, thick crust pizza with pepperoni. Now, the fact that I almost did that backwards means there's probably another sample utterance we should put in to make sure that case is accommodated. But for now, we're going to try the happy path. All valid slot values. - [Alexa] You just triggered order pizza intent. - [Narrator] Now, what happened? This is different from what just happened in the utterance profiler. It says you just triggered order pizza intent. Well if you recall from a previous lesson, that is the default handler that gets triggered when we don't have handler code for the intent. See? You just triggered, and then it gives the name. What it means is, we need a new intent handler. The easiest way to do this is to scroll to the top, copy the hello world intent handler from conts down to the semicolon. Create a new line above it, and paste. Now, we have to get to renaming some things. As the red x says, we can't have more than one hello world intent handler. Instead, we want to call this the order pizza intent handler. And here under can handle, we don't want to look for hello world intent, we want to look for order pizza intent. Right now, the response will still be the same as hello world. Let's just give it something slightly different. This is the order pizza handler. Now we need to copy the name of the handler. Don't forget this step. Head all the way down to the bottom, and add it under add request handlers. It can kind of go anywhere. We'll add it and put a comma. Now if we deploy and go back to test, we'll see things work a little bit differently. Say quit to explicitly end the session. I'm going to copy this utterance so we don't have to type it again. I'm going to say open Pizza Dog. For one, we should see our new launch request response. - [Alexa] Welcome to Pizza Dog. What can I get for you? - [Narrator] All right, welcome to Pizza Dog. What can I get for you? Let's tell her. I'd like a thick crust pizza with pepperoni. - [Alexa] This is the order pizza handler. - [Narrator] Very good. Now we can see we're actually getting to our code. So, now that we have some code, we need to get those slot values, because of course, if we were a real pizza company with the real capability of ordering pizza, we need to get those slot values and pass them along to some other service. So, let's head back to the code tab. Scroll to the order pizza intent handler, and let's get to work. All right, we're going to start by getting some variables. We'll use the keyword var. Crust equals something. How do we get the slot? Well we have this value handler input, we'll start with that. In fact, let me go back to the test tab, and I'll show you exactly. See the JSON input section here? We're going to scroll down to request. Underneath request, there's a section called intent where you see the name of the intent, order pizza intent. And then there's a section called slots. Underneath slots we have the size, the topping, I'm collapsing it with this triangle here, and the crust. Each slot, size, topping, and crust. If we expand them, we can see there's a value. In this case, large. So we're going to drill into this structure in our code tab. So say handler input dot request envelope. That's just something you need to know. Dot request, now we're getting into the recognizable structure that we just saw. Dot intent. Dot slots dot crust dot value. So, handler input dot request envelope. That's going to get us to the JSON we just saw here. Then we're going to drill in to request dot intent dot slots dot crust dot value. See? Request dot intent dot slots dot crust, right here, dot value. And that should be the word thick. Let's do the same with the other slot values. So this would be topping. And this will be size. Now, we want to modify our output. If we got here, we know that the automatic dialogue delegation has already filled all these slots. So we want to say all done. Your order of a, and I'm going to go outside the quotes, use the plus to concatenate, size plus space plus crust plus the word crust. So we're going to say your order of a large space thick crust pizza space plus, and let's say with space plus topping plus, then finish out the quote, should arrive in 30 minutes. Exclamation point, we'll save that. So if I concatenated the string correctly, it should say something like all done, your order of a small, thin crust pizza with sausage should arrive in 30 minutes. Before we test, let's create ourselves a slight shortcut on the build tab. Under order pizza intent, let's have an utterance that says for a, and then we'll choose this size crust, crust with topping. That's going to allow us to say ask Pizza Dog for a medium, thin crust pizza with sausage. Click build. When the full build is complete, we can go back to the test tab. Now let's say ask Pizza Dog for a medium, thin crust pizza with mushrooms. - [Alexa] We serve pepperoni, sausage, cheese, or mushroom. - [Narrator] Again, here's the utter delegation coming back. Mushroom has to be singular. Don't like it anyway, so let's say cheese. - [Alexa] All done. Your order of a medium, thin crust pizza with cheese should arrive in 30 minutes. - [Narrator] Fantastic. You can see that all our slot values are coming out as they should. Our string concatenation is right. Alexa is giving a great response, confirming the order that we just made. So now you've seen how we can map our utterances to an intent, and that intent to an intent handler to get all the values we need to order a pizza.

Contents