Posted in Alexa, AWS, Programming

Writing Custom Alexa Skills using AWS Lamdba and Python

In this tutorial, we will learn to write an Alexa skill and publish it for certification. We will use AWS Lambda to host the code which will be used to implement the skill.

When you write an Alexa skill it generally involves two set of activities.

  1. Writing a service which will perform the tasks/actions of your new skill. This is the part we will write in AWS Lambda. You can host this service yourself too if you want to by hosting it on a web server and providing an HTTPS endpoint. Lambda just takes cares of many aspects of this and makes it easier to integrate with Alexa
  2. Configuring your skill in amazon developer portal (name, words associated with your skill, how to invoke it etc) and linking it to your service.

Alexa and your service communicate over the internet using HTTPS and JSON.

For this simple tutorial, we will write an Alexa skill that will tell us a Chuck Norris joke. This is fairly simple to build. We will build a skill so you can ask Alexa through your echo device to tell you a Chuck Norris joke – Alexa will call a Lambda function we will write. This function will randomly pick a joke from a pre-existing list of jokes and send it back to your echo device. Alexa will then read out the joke to you.

In order to follow this example, you will need to have an Amazon AWS account and Amazon developer portal account.

You can sign-up for AWS at aws.amazon.com and the developer portal at developer.amazon.com. Both accounts are free and you don’t have to pay for using Lambda functions for the first million calls each month or 400,000 GB-seconds of compute time per month. This is more than sufficient for this example. You can read about Lambda pricing here

Lambda Function

Let’s start with writing a Lamba function.

  1. Start with a blank lambda function
  2. Choose Alexa Skills Kit as a trigger.
  3. Give it a name and select Python from the Runtime drop-down
  4. Choose an existing role that has Lambda execution privileges and leave rest of the settings as it is.

You can find the Lambda function used for this example here

This Lambda function has lambda_handler as the entry point. When this function is called by the Alexa voice service – it passes the type of the event in the event parameter.

One thing we need to make sure that your Lambda function can only be called by the application we will create in the amazon developer portal and no one else. That is where the application_id checks come in. We will get the application id when we create the skill in the amazon developer portal.

The next step is to handle different types of requests that will come from Alexa. The types of requests are

  • LaunchRequest
  • IntentRequest
  • SessionEndedRequest

In this example, we will only handle LaunchRequest and the IntentRequest. LaunchRequest is triggered when the application launches or a user says something like “Alexa Open Chuck Norris Fan Facts”. IntentRequest is triggered when the user asks your application to perform a certain action.

In this example, we only have one action and that is to tell a Chuck Norris Joke. Our application will tell a joke even if a user just opens the app by saying ‘Alex Open ‘ so we will essentially be executing the same action for both types of requests. For the sake of simplicity, we will not discuss SessionEndedRequest.

The only difference between the two requests in this example will be that on LaunchRequest we will populate an internal list of jokes and then randomly pick a joke and send it back to Alexa.

In the on_intent function, as a good practice, we should always check if the name of the intent is correct – even if there is only one intent implemented.

The code is quite self-explanatory. During the application launch we initiate a list of jokes and on an intent launch, we simply check if the intent name is something we support and then call the function that implements an intent. In our case telling a Chuck Norris fact.

Here is a brief explanation of rest of the code

populate: This is a simple function. It adds a list of jokes to a global python array. This function is called on LaunchRequest. Everytime our skill is invoked we will choose a joke randomly from the list.

build_speechlet_response: This function builds a JSON response to send back to Alexa service. The response has three basic parts.

  • outputSpeech is something Alexa is supposed to read out as a response.
  • card is something that shows up in the Alexa companion app. You can decide in this part of the response about what should be shown in the ‘cards’ inside the app. That is primarily the title and the content.
  • reprompt is something Alexa will say in case it doesn’t get a response from the user or doesn’t understand what user has said.  One use of this section can be to ask the user to say something specific again in case the response is not what you expected. We dont have a use for this section.

build_response: Just a wrapper function that builds the response payload by adding version and any session attributes (we don’t use any session attributes in this example) and then calls build_speechlet_response  to build the JSON body of the response.

Creating Alexa Custom Skill

Now that we have a simple Lambda function that will do all the work, we need to create a new Alexa skill in the amazon developer portal.

Note: Your developer login account should be the same that is used with the alexa device in order for you to be able to test your new skill.

Once you are logged into the portal go to the Alexa section and select Alexa Skills Kit. On this page, you can select the Add New Skill button.

The process of adding a new skill is made of adding information in various sections. They are all very self-explanatory but I will briefly mention the important bits you need to remember.

  • You should choose the languages your skill will support. You can only test the language which is configured for your Alexa account. You can check or change that in the Alexa companion app. You can find this setting under Settings->[device-name]->General -> Language
  • You can add multiple languages but you must configure the skill for each language separately.
  • Fill in the skill name and the invocation name. That is the name a user will say to launch your skill. For this example put “Chuck Norris Fun Facts” in both.
  • In the Interaction Model fill up the intentions and utterances as below screen-shot-2017-02-11-at-9-40-43-pm

 

The intent schema is JSON which lists out the intents your skill will support – each intent is mapped to a phrase a user will say.

In the example above we are defining an intent “TellFact” and then mapping it to some utterances, a user will use like “Alexa ask Chuck Norris Fun Facts for a Chuck Norris fun fact” or “Alexa ask Chuck Norris Fun Facts for a joke”. You can also simply invoke this by saying “Alexa Open Chuck Norris Fun Facts”.

  • In the Configuration section select Lambda and fill in the ARN of the Lambda function you created earlier.
  • Fill in rest of the sections

That’s it. The Skill should now be available under “Your Skills” section of the Alexa app and you can start interacting with it. Go ahead and say “Alexa Open Chuck Norris Fun Facts”

Once you fill in all the sections you can submit the app for certification provided you want to do that.

Read through the certification guidelines and make sure you are not violating any copyrights in the name of the app etc and once approved your app will appear in the Alexa Skill Store.

Author:

Not your average Geek!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s