Get Slot Value Alexa Python

Alexa is Amazon’s Voice Service, accessed through Amazon Echo. After the setup process, you simply say the word ‘Alexa’, Echo will light up, and Alexa will respond. Alexa Skills are like apps. Skills are voice-driven Alexa capabilities. In other words, Skills are the interface between users and Alexa. Slot value: Slots are input values provided in a user’s spoken request. These values help Alexa figure out the user’s intent. These values help Alexa figure out the user’s intent. Sample utterances: The sample utterances specify the words and phrases users can say to invoke your intents. Standard Request Types can be one of three values. We can access the information in the slots. Although this was not an complete guide to making Alexa Skill with Python, it should get you. This.event.request.intent.slots.name.value has the value of the name passed in via an utterance like 'Get fact about shaun'. Where the value above would be shaun taken from the utterance and available in the handler? Also, what other values are available on this object? This.attributes and this.events) 👍. Amazon Echo PHP file to control Honeywell Thermostat through a python script - echo.php.

In the previous articles we have built a Hello World Alexa skill.As any other hello world programs, the skill does only one job.We can only ask a single question and receive the single answer back.

Although you can build skill using that kind of interaction with a user, sooner or later you will feel the need of getting some custom data from a user.For example, you may want to tell a number or a name of a city and behave differently based on the answer.

Let’s see how we can do that.

Slots

To capture a user’s input we need to use so-called slots in intent utterances.To achieve that, we can use curly braces ({}) to provide a slot name.

For example:We would like to ask a user to provide any number.So we define an “AnswerIntent” with the following utterances:

  • Number {numberAnswer}
  • The number is {numberAnswer}
  • {numberAnswer}

Now, if the user says “The number is five hundred” we would like to get the number and use it somehow in our code.

At that moment we have defined the slot. But we need also to choose a “Slot Type” for that slot.The slot type will help Alexa to understand that is user saying.

That slot type can be either custom or built-in.

Built-in slot types

In our example, we need a number, so there is an “AMAZON.NUMBER” slot type.

That is how the “AnswerIntent” looks in the skill builder.

Besides that type, the skill builder provides a vast amount of other types.The list of types is growing.

For example, there are slot types to work with dates and times.As well as some other interesting ones. There are slot types with a list of airports or animals.

We can use a built-in type and extend it with additional values.

Here is just a small portion of them.

Custom slot types

Let’s say you want to get a more specific response from your users, but there is no built-in slot type for these needs.Then you can build your own custom slot type and provide a list of possible values.

Go ahead and in the skill builder click to “Add” slot type link and choose a name for your custom slot.

Then you can specify all the possible values for that type. Even add synonyms if you need.

After that, you are free to use the slot type in your slots. Create a new slot in utterance samples and attach the custom type.

Getting values from the slots

We have figured out how to define slots in the skill builder. The half of the job is done.

Now it’s time to access the response from our codebase.

We can achieve that in two steps.

At first, we need to tell Alexa to start listening to a user’s input.To do that we need some intent handler with reprompt call in it.

At this moment Alexa waits for the user’s input.If the user tells us something satisfying the sample utterances from “AnswerIntent” example above,Alexa would trigger “AnswerIntentHandler” where we can fetch a slot value.

The slots a live deep inside handlerInput.requestEnvelope.request.intent.slots object.We can fetch the slot by the name slots['<name-of-the-slot>'] and then access the value slots['<name-of-the-slot>'].value.

Get Slot Value Alexa Python Ide

Let’s see it in the example:

Here we fetch the slots object to a slots constant.Then we’ve got the value of numberAnswer slot.

That’s it. We’ve got it.

Now to demonstrate that Alexa understood a user correctly, we tell her to say the number back to the user.

That is how the dialog looks like.

Wrapping up

We have a new tool in the toolbelt. Now when we need to get some input from the user we can use slots for that.We have learned there are two kinds of slot types: custom and built-in.We can use both in order to build better Alexa skills.

You can find a complete example on GitHub.

Get Slot Value Alexa Python Tutorial

Prerequisites:

  1. Amazon developer’s account.
  2. Amazon web services account.
  3. Basics of python programming.

Topics Covered:

  1. Understanding architecture of alexa.
  2. Lambda function to create custom skill.
  3. Invocation name, Utterances and slots
  4. Zipping the files and its dependencies.
  5. Uploading and testing the skill.
  6. Submitting the skill for certification.

1. Let us understand the basic architecture of alexa with a simple diagram

User enables the skill from amazon alexa app in device, then the skill needs to be invoked using its ‘Invocation Name‘ here in this case ‘Rail Gaadi’ is the invocation name. when user prompts with utterance, alexa recognizes the utterance and converts that into the json object. Our custom skill reads the json object and process the request and sends the response back to alexa.

2. Creating lambda function and using the sample blueprint to develop custom skill

  • Login into lambda function console and click on create function
  • function name should be as simple as possible as it would be used in amazon resource name (ARN’s)
  • AWS Lambda function calls lambda_handler function to start your service, it is just like a main method or function which AWS Lambda understands.
  • In the context of creating custom skills, there are few functions which needs to be handled in lambda_handler which are as mentioned below.
  • on_session_started,on_launch, on_intent and on_session_ended as the name suggests all the method needs to be handled inside the lambda_handler.
  • on_session_started & on_session_ended is where your session starts & ends respectively.
  • on_launch this is where you prompt user with respective welcome note or with your app details
  • on_intent is where one implement the logic.

Note: try to understand the sample code in the blueprint and life would be easy.

3. Invocation Name, Utterances and Slots

let us understand invocation name, intents and utterance one by one as this is very important for any skill to understand and respond.

Get Slot Value Alexa Python Programming

Invocation Name:

  • Alexa recognizes the skills by the invocation name provided in this case – Rail Gaadi is the invocation name.
  • Alexa responds with welcome message when user does not provide any specific request. example: “Alexa, Open Rail Gaadi

Utterance:

  • A user spoken statement or sentence which alexa understands to perform some action
  • In our case, alexa responds with the output when user provides Invocation name with utterance. example: “Alexa, Open Rail Gaadi to get the status of 12345”

Slots:

  • Slots represents how data or values are handled for processing the requests.
  • Amazon provides built in slots which could be found here.
  • One can provide their own custom slots if required.

Hurry! theoretical journey has been complete. we are few steps ahead to submitting the alexa app.

Get Slot Value Alexa Python
  1. Login into amazon developer console.
  2. Click on the Alexa tab.

click on the Get Started button under alexa tab and user would be redirected to add skill page.

3. click on Add a New Skill button as shown below.

4. After clicking on add a new skill, we would be landing on Skill Information page here we have to provide app related information. Skill Type, Language, Name, Invocation Name and Global Fields.

  • Skill Type should be selected as Custom Interaction Model, as it is an custom skill.
  • Language is specific to region. Rail Gaadi is for indian market so i have selected English (India).
  • Name is which user would be seeing in the amazon alexa app for search. Name should be small and easy to remember.
  • Make sure invocation name and name should be same.
  • Global fields could be selected depending on the applications requirement.

5. Interaction Model is where we would be providing sample utterance, slots and intent. here i have taken another example where user would be asking for places history. It provides code editor and as well GUI it is up to the user which needs to be picked up.

6. After building and saving the model, we would be landing on Configuration tab. this is crucial as we are going to connect to skill to lambda function code by providing the ARN value in the Default field.

Here come the tricky part of zipping the code and uploading it to lambda function

7. Before zipping the folder make sure to complete all the error handling part.

  • Create and activate virtual environment, let us name it “zipfol“.
  • virtualenv zipfol
  • cd zipfol
  • source ./bin/activate
  • create a text file and include required libraries
  • pip install -r requirements.txt
  • move site-packages in the root folder and zip the folder
  • cd zipfol
  • source ./bin/activate
  • pip install -r requirements.txt
  • mv ./lib/python2.7/site-packages/* ../zipfol/
  • zip -r zipfol.zip *

8. Uploading the zip file to lambda function. if size of zip file is greater than 10 MB then use s3 bucket. it is very simple, just login into s3 bucket console,create s3 bucket and upload the zip file and use the link in lambda function.

9. Testing our code, provide the sample utterance in the service simulator and verify for the response as shown below.

10. Publishing Information sections is very important as it would be user facing, the information provided there would be shown to the user which helps to enable and access with ease.

Get Slot Value Alexa Python Codes

11. Privacy and Compliance, finally we are one step behind of submitting our code to amazon alexa store. necessary details and accept the compliance. Click on Submit for Certification.

Note: Amazon performs all the basic and necessary steps and testing of your app and if it passes all the certification process. you would be sending an email and it would be live soon.

Thank You for reading this blog. please provide your comments and please share it.