AWS GoT Hack: Target Intent(s)

Read More

AWS GoT Hack: Lambda Code

To recap, I'm working on a 48-hour hack to help learn about AWS.  My goal is to:

Create a "Game of Thrones" Alexa Skill that allows the user to ask if a character is alive or dead, and responds with an accurate answer.

In the previous post, I created a Git repo in AWS CodeCommit. I'll use this repo to hold the code for the Lambda function, and other assets, e.g. the Alexa Skill's sample utterances and intent schema.

Lambda Code

Lucky for me, there is lots of sample Lambda code for Alexa Skills. "Hello World" seems to be a good place to start.

CodeCommit Detour...

So I copied the hello world code, committed it (commit early, commit often), and tried to push it. The push failed. Back to the CodeCommit docs, and I find that there is a 15 minute timeout on the credentials by default. So, I ended up having to configure SSH anyway. Sometimes just do the right thing, even if it takes a few extra minutes, eh?  

BTW, if you aren't already doing it, check out git-flow. You'll be glad you did. 

Back to the Lambda code...

The lambda code is broken into two files:

  • AlexaSkill.js, which provides generic helper functions
  • index.js, which contains the custom code for your Alexa Skill

Steps to adapt index.js for my needs:

  • Get the Application Id from the Amazon Skill console and paste it into "APP_ID"
  • Rename "HelloWorld" to "GoTWikia"
  • Update the onLaunch "speechOutput" and "repromptText" variables as appropriate.
  • Make a similar change in intentHandlers to response.ask() under AMAZON.HelpIntent.
  • In intentHandlers, add my planned response under "AttributionIntent":
    • "My data comes from the Game of Thrones Wikia at gameofthrones.wikia.com"

That should be about it for the code.  Let's give it a try.

Uploading Lambda code

To upload the code, I first zip the two files together, and then head back to the Lambda dashboard, where I upload and save it. Simple.

Test the Alexa Skill

Once the code is there, we can return to the Alexa console and test it. Type in one of our sample utterances: "where do you get your data" and see what happens.  I got this:

The remote endpoint could not be called, or the response it returned was invalid.

So, which was it? On to debugging! For that, we get to use AWS CloudWatch.

CloudWatch

CloudWatch does a number of things. What I want to do now is look at logs. And what I see is:

Syntax error in module 'index': SyntaxError
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)

Well, duh. Maybe I should have used an IDE or run a syntax checker before uploading the code.

Looks like I forgot to put quotes around my application ID. Fix it, compress the files, upload and test again.

Voila! The Lambda response is:

{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "My data comes from the Game of Thrones Wikia at gameofthrones.wikia.com"
    },
    "shouldEndSession": true
  },
  "sessionAttributes": {}
}

Which looks right, and when I push the "Listen" button, Alexa plays it just as expected. Yay!

In the next post, I'll add our other question ("Is {{name}} [[alive|dead]]?") to the skill and Lambda code. 

AWS GoT Hack: Lambda Function

To recap, I'm working on a 48-hour hack to help learn about AWS.  My goal is to:

Create a "Game of Thrones" Alexa Skill that allows the user to ask if a character is alive or dead, and responds with an accurate answer.

In the previous post, I created most of the very simplest implementation of our Alexa Skill.  Now we need to go write the code that backs up that skill. We'll be using AWS Lambda.

One creates a Lambda function from the Lambda Management Console. I'll skip the blueprint and configure the trigger to be the "Alexa Skills Kit".

Configuration

The configuration page is a bit more complex:

  1. I'll name the function "GoT-Skill"
  2. I'll use the Node.js runtime. 
  3. When I actually start coding, I'm going to upload a .ZIP file, rather than enter the code into a form field. However, just to get the function set up, I'm going to leave the template code here.
  4. I'll create a basic execution role.
    1. "Create a new IAM Role"
    2. Name it "LambdaBasicExecution"
    3. Hit "Allow"
    4. Launch IAM Management Console
    5. Select "LambdaBasicExecution"
    6. Hit "Attach Policy"
    7. Select "AWSLambdaBasicExecutionRole"
    8. Hit "Attach Policy" 
    9. Select "Choose an existing Role" and "LambdaBasicExecution". I had to wait a minute or so for the new role to appear.
  5. I'll leave the Advanced settings the way they are.
  6. Review the function and accept it.

OK, so now I have a very basic Lambda function that really does nothing.  But, at the top of this page I do have something I need: the ARN. I'll copy that, go back and paste it into my Alexa Skill.

Testing Alexa Skill

Now I can test my Alexa Skill. I'll enter an utterance ("where do you get your data") and see what happens.

The request JSON shows that the utterance is mapping correctly to the "Attribution" intent, but the response is:

The remote endpoint could not be called, or the response it returned was invalid.

Well, that actually makes sense as I haven't actually added any code to the template. I should go do that.

Lambda Code

Before I start writing code, I want to create a repo in which to store that code. I could do that using Github, but I'm trying to use a lot of AWS services, so I'm going to try AWS CodeCommit in the next blog post.

AWS GoT Hack: Alexa Skill

Read More