Alexa - Get what the user is saying in your lambda function

The title may be a bit misleading as this is not possible. You cannot just record everything that the user is saying. However there is a great alternative if you have an idea of what your users will be saying. It is my belief that listening to everything that the users are saying has been removed due to privacy issues. So you cannot just "listen in". However knowing what your users are going to say is also going to have a positive impact on the performance of your skill.

Almost wildcard speech

So how can this "almost" be achieved? Well say your user is going to make a choice and you wish to do something depending on this in your backend (lambda). The best way to accomplish this is using slots!. Slots is a way to add wildcards to the users utterances (the utterances trigger intents). These slot values are available in your lambda function. So let say the user may say something like "I choose left" or "I choose right". The decision in that sentence is "left" or "right", so these are the slots of our sentence. Below a decision using the utterance I choose {Decision} can be seen:

Example of using slots with SearchQuery

You will notice that the slot is {Decision} and the type of it is searchquery. Searchquery is a wildcard type of slot, anything spoken in that part of the sentence will be recorded. It is then available using the following code in your lambda function:

var decision = this.event.request.intent.slots.Decision.value;

The above will give you the {Decision} part of your utterance. Meaning if the user said "yes" or "no" then that is what you get, however the user can say all sorts of things so remember to handle that as well!

You may be thinking: why not just make an utterance that says {Decision}? Well because you cannot do that, if you do that, you will get the following error:

Sample utterance "{Decision}" in intent "DecisionIntent" must include a carrier phrase. Sample intent utterances with phrase types cannot consist of only slots. Error code: MissingCarrierPhraseWithPhraseSlot

Which basically means that SearchQuery slots cannot stand on their own. That brings us to the next part - matching exact words using a custom slot.

So what if they only utter 1 word? say yes or no?

So if we cannot use SearchQuery with just 1 word, what do we do? Well, we can use custom slots. When viewing your Alexa skill on the left side you will find the heading "Slot types". Here you can add new slot types and you will also find the previous slot type {Decision} here. So in order to have a custom slot we create a new one called DefinedDecision:

Defining a slot type

Then we can add single-word utterances that the user will say. Perfect for answers such as: "yes", "no", "Red", "Blue" and so on. I know what you are thinking that there could be thousands of words and you are right, but this is the only way to do it. Again - just like with the decision slot - this requires you to know what your user will be saying. Below it can be see both of these slots are added to the "Decision" intent:

2 slot types to create almost wildcard search in alexa

Just the same way as before this slot can be accessed in your lambda code:

var definedDecision = this.event.request.intent.slots.DefinedDecision.value;

That is it

So by creating two slot types in your intent you can almost do wildcard choices in your Alexa Skill. This took me a while to figure out, I hope you save that time - let me know in the comments! :)

Do you not yet have an Amazon echo? You can easily order one from amazon: