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.