HandleLambdaEvent: A continued in-depth analysis of the Alexa Skills Kit SDK for Node.js
In the last post looking at the Alexa Skills Kit SDK for Node.js, we left off after examining the setup. Next up is
HandleLambdaEvent, which pretty much does what it says.
HandleLambdaEvent is called when
handler.execute is called. It checks to see if there are any localization resources and initializes
i18next if so. Then it calls
As a reminder, this is called via
execute off of
alexaRequestHandler. As another reminder, the scope is set to our
alexaRequestHandler itself. As another, another reminder, this is exported from the SDK as
handler. So when you call
alexa.execute, you’re really calling
Here’s the code:
Remember that the handler (which is the value of
this) has a number of different properties already assigned to it.
The first one we encounter is
Knowing how AWS Lambda works isn’t strictly necessary to build Alexa Skills using the SDK, but it sure does help. I have a post that goes more in-depth, so go there for more and for examples of what our Alexa event may look like. (I’ll likely write a post in the future that looks at the Alexa event.)
Each event object has a request object, and on there is the locale. This should be familiar to you if you’ve worked with localization on the web. It will be not just a language, but often the region for that language. As of right now, there’s only
de-DE. I’m still holding out hope that
fr-FR is just around the corner.
From here we have a conditional: has the developer provided
resources. Or, to put another way: has the developer provided an object with responses in different languages/regions? (This is another topic that will warrant its own blog post.)
If not set, we’ll go straight to validating the request.
Otherwise, the resources are set, so we leverage the i18n library set to
this (remember, we technically could overwrite this, but for several reasons it’s incredibly rare that you would want to). I’ll break out this portion so we can take a closer look:
We just looked at
this.resources and we’ll soon look at
ValidateRequest, so let’s examine
this.i18n is set to i18next, an extensible internationalization framework.
sprintf is a postprocessor for i18next to allow for—wait for it—sprintf usage (e.g.
("I have %d apples", 6) becomes
"I have 6 apples").
One interesting thing here, remember, is that we have access to the
i18n object, so we could reasonably expect to leverage it ourselves to do things like pluralize words. (How often have you done
count !== 1 ? "apples" : "apple"?)
Here the code is instructing i18next to use the sprintf postprocessor and then initializing with some options:
overloadTranslationOptionHandlermakes for more readable translation options (i.e. not using an options object)
returnObjectsallows for returning an object (as the name implies), as the default option is to return just a translated string—here we can provide more information on the translation when it’s returned
resourcesis clear enough, I’d hope
And finally, the initialization method checks to see if there’s an error, throwing one if so, otherwise handing off control to
Which, incidentally, is what we’ll do next. Continue reading about
ValidateRequest on the Alexa Skills Kit Node.js SDK.