Saturday, July 9, 2016

AWS Alexa Skill - Lou Person


AWS Alexa Skill - Lou Person

I recently published a post about how Echo, Dot, Tap and Alexa make the world a better place and improve the life of people with disabilities.  You can read it here:  http://blog.louperson.com/2016/07/amazon-echo-and-alexa-make-life-better-lou-person.html

The skill, called Dylan Day, has Alexa lookup events in history that occurred on a given date.  For example: "Alexa, ask Dylan Day what happened on July Fourth" will read out loud events which occurred in history on July Fourth.

The ingredients for the Skill are as follows:
  • An Amazon Web Services Console account.
  • JavaScript JSON for the application itself.
  • The Lambda service inside of AWS.
  • An Amazon Developer account.
To make coding easier (not required) I used the following tool set:
  • Visual Studio with the AWS Explorer installed.
  • Git for moving the code around.
Under a separate post, I describe setting up and securing (through IAM) an AWS console account.  You can read more about it here: http://blog.louperson.com/2016/01/amazon-web-services-over-cup-of-coffee.html

Amazon, for whom I work at Amazon Web Services (AWS), provides a great library of sample skills for developers.  I started with example code found here on GitHub: https://github.com/amzn/alexa-skills-kit-js/tree/master/samples/historyBuff
I cloned the files locally and opened them up in Visual Studio.


The speechAssets directory contains files related to speech utterances and Intent Schema which are entered in the Developer portal. More on this later.  The src directory contains the main application files which are uploaded to Lambda.  There are two files.  AlexaSkill.js and index.js.  AlexaSkill.js communicates with the Alexa service and is called by index.js.  This file pretty much acts as the gateway between the custom skill and the Alexa service.  I thought of it as the railroad tracks.  index.js is the engine which runs on the tracks.  This is where I made the customizations.  It contains the actual code for the skill. 

Most importantly, AlexaSkill.js contains a reference to the appID provided in the developer console.  This value is entered in index.js and passed to AlexaSkill.js This is critical from a security perspective because it will ensure ONLY the intended application can invoke the Lambda function.  In AlexaSkill.js:

function AlexaSkill(appId) {
this._appId = appId;
}

From index.js, I changed this:
var APP_ID = undefined; //replace with 'amzn1.echo-sdk-ams.app.[your-unique-value-here]';
to this (actual code obfuscated):
var APP_ID = 'amzn1.echo-sdk-ams.app.xx9bfexx-1xxf-4287-b0xx-6xx14xxf7cxx';

You can view the original code (I customized for my skill) from the GitHub link above.  I changed the invocation from History Buff to Dylan Day and changed some of the spoken text inside of the code.

I then used Visual Studio to upload the code to my Lambda function as part of AWS. 

Once uploaded, I received an ARN to reference my application in the developer portal.  This is the other side of the security feature above where I entered the appID in the Lambda code, the other side of the control is entering the Lambda ARN in the Developer portal.  Note the arrow in yellow.







Make sure that the Lambda trigger is set to Alexa Skills Kit before proceeding.

Next it was time to publish the Skill!  I logged into the developer console and selected Alexa Skills Kit. 


I then created a New Alexa Skill.  I chose Custom Interaction Model and will use Smart Home Skill API for another post when I try to integrate my Echo with my home automation system, such as my sprinkler system and HVAC.

Now comes entering the intent schema and sample utterances.  The intent schema and sample utterances are contained in the SpeechAssets folder of the Git repository discussed above. 

Up above, I mentioned that the ARN for the Lambda function in Visual Studio was needed in the developer console.  Make sure you enter the ARN name and not the ARN role. I entered the ARN information in the configuration section.   I did not use Account Linking (I selected no) because I was not linking any end user accounts with the skill.

I was able to test my Skill on my Alexa logged in with my developer account, as well as send test utterances as text through the developer console.  The results of a test are here:

Finally I entered the publishing information for the Alexa installation through the end user Alexa console and privacy information.


I submitted the Skill for certification!  It did take a few rounds back and forth with the certification team, mostly around the Publishing Information page.  Then about 9PM last night, my Skill went live on the Alexa End User console! 


I work at Amazon and the postings on this site are my own and don't necessarily represent Amazon's position. 


Post by Lou Person, I work at Amazon Web Services.




No comments:

Post a Comment