Intranoggin

Blither, Blather, Web Content Management.
Blog » Alexa on Azure: Part 3 - Adding Slots

Alexa on Azure: Part 3 - Adding Slots

My first Alexa Skill simply took no information from the user and just returned that Dad Rocks. While that is probably the greatest skill that can be built, I think there may be a niche here ready to be cracked open. I have an 11 year old and a 14 year old and I’ve discovered a real speech pattern with these two. Unfortunately, sometimes they aren’t around because they’re at school or something. In those times, I have nobody to tell me how much everything sucks. That got me thinking, “Some poor bastards don’t even have a teenager in the house ever. How do they know that everything sucks?” I think with the proper skill, Alexa can help them out.

 

For this, we’ll get to use slots in our skill. Right click your Functions project and Add a new Azure Function

image

Add your SpeechAssets folder just like we did in Alexa on Azure: Part 1- Creating the Function. (It may be easiest just to copy & paste this folder from our other function.) Then add another .txt file in here called CustomSlots.txt. Due to the tooling bug, here again it may be easiest just to copy, paste, & rename the Sample Utterances.txt.

NOTE: Just like our SampleUtterances.txt & IntentSchema.json, the CustomSlots.txt file has no actual bearing on our AzureFunction. We are simply using this as a mechanism for holding this type of information so it can be stored in source control. When we register our skill in the Amazon Developer Portal, we’ll be pasting the contents of these files into portal fields.

Based on my experience with my teenagers, I’m imagining 3 different scenarios.

  1. The greeting of the teenager should result in some sort of growl.
  2. Asking the teenager if they would like to participate in some activity should result in a response that said activity sucks.
  3. Asking the teenager their opinion of any subject should result in a response that that said subject sucks.

NOTE: IRL the teenager may typically ignore any of these greetings or questions anywhere from 3-5 times giving no response at all, but a this point, I’m not sure if I can implement that effectively in Alexa.

It seems easiest to start with the anticipated utterances, so I did. My SampleUtterances.txt content is.

AskTeenageDaughterStatus What's up
AskTeenageDaughterStatus How's it going
AskTeenageDaughterStatus What's the word
AskTeenageDaughterStatus what's going on
AskTeenageDaughterStatus what are you doing
AskTeenageDaughterStatus how was your day
AskTeenageDaughterStatus how did you sleep
AskTeenageDaughterStatus good morning
AskTeenageDaughterStatus good night
 
AskTeenageDaughterParticipation Do you want to {Activity}
 
AskTeenageDaughterOpinion What do you think about {Subject}

You can see I’m going to have an intent for each of the 3 scenarios I mentioned above.

  1. AskTeenageDaughterStatus
  2. AskTeenageDaughterParticipation
  3. AskTeenageDaughterOpinion

Notice the {Activity} & {Subject} token syntax. These will match across our CustomSlots & IntentSchema and will be handed back to our Participation & Opinion intents by Alexa.

Now update IntentSchema.json to include the slots we need.

{   "intents": [     {       "intent": "AskTeenageDaughterStatus"     },     {       "intent": "AskTeenageDaughterParticipation",       "slots": [         {           "name": "Activity",           "type": "ACTIVITIES"         }       ]     },     {       "intent": "AskTeenageDaughterOpinion",       "slots": [         {           "name": "Subject",           "type": "SUBJECTS"         }       ]     }   ]
}

And update CustomSlots.txt to include entries for each of the slots we are creating. Notice the casing here – the slots entries match the types from IntentSchema, while the utterances match the names from IntentSchema.

ACTIVITIES
Get Dressed
Go to Bed
Come eat dinner
Do your homework
 
 
SUBJECTS
Math
English
Gym
Lunch
Dinner
Shopping

Alright, now we get into the coding in run.csx. Something I find missing here is that there is no way to tell Alexa to call a different service for different intents. You can only register a single URL for the skill, so our run.csx file needs to handle all the intents. It’s not a big deal, we get all of the information we need in the body of the request, we just need to load up the intent name that was passed and use a case statement to handle each one.

TIP - To see the .json object that Alexa sends, use these two lines at the top of your function:
dynamic data = await req.Content.ReadAsAsyncReadAsAsync<object>();
log.Info($"Content={data}");

Once you’ve gotten the content read in, you can access the intent name with string intentName = data.requeest.intent.name;

Then set up a switch statement on intentName with each case being one of the intents from your IntentSchema

Within each intent seciton, you can read in the desired slot using data.request.intent.slots.<slotname>.value for me this was

string subject = data.request.intent.slots.Subject.value;

and

string activity = data.request.intent.slots.Activity.value;

Remember, the slot is only defined within the intent that uses it, so you can’t just read these all in generically without causing exceptions.

You can pull my working example code of GitHub, but for quick reference here’s the major highlights.

Loading Intent

Intent Switching

Loading Slot Value

Good? Good. Now we can go register our new skill in the Amazon Developer Portal. The steps for registering our Skill have already been outlined here: Alexa on Azure: Part 2–Registering Your Function

We’ll just need to make this adjustment: On the Interaction Model page, we’ll add our two new custom slot types. For type enter in the type matching the one in your IntentSchema.json file, and then enter the values you’ve chosen. Since we added the CustomSlots.txt into our SpeechAssets folder, we can just copy and paste from this file.

image

image

image

NOTE: The values you enter here only serve as a reference. Alexa doesn’t require that the given value be in your list. It will hand you whatever matches the slot value based on it’s position in the Utterance.

That’s it really. At this point, skill registration is exactly the same as the previous function we created and you should be able to test your skill in the browser and on your device.

Next Steps: Obviously this switch statement technique isn’t going to scale if we’ve got our responses being built inline like this. Currently this example is only 110 lines and it already feels like a train wreck. For reference, I’m going to push this version, as is, into a folder called AlexaAskTeenageDaughter.05. You can find it there now. The similarly named folder without the version suffix will be the current version as I make improvements over time.


Posted: 2/27/2017 11:00:00 AM by Ryan Miller | with 0 comments