Dustin Coates in Alexa

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.

Summary

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 ValidateRequest.

In Depth

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 HandleLambdaEvent. Indirection!

Here’s the code:

function HandleLambdaEvent() {
  this.locale = this._event.request.locale;
  if(this.resources) {
    this.i18n.use(sprintf).init({
      overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
      returnObjects: true,
      lng: this.locale,
      resources: this.resources
    }, (err, t) => {
      if(err) {
        throw new Error('Error initializing i18next: ' + err);
      }
      ValidateRequest.call(this);
    });
  } else {
    ValidateRequest.call(this);
  }
}

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 _event:

this.locale = this._event.request.locale;

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 en-US, en-GB, and 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:

this.i18n.use(sprintf).init({
  overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
  returnObjects: true,
  lng: this.locale,
  resources: this.resources
}, (err, t) => {
  if(err) {
    throw new Error('Error initializing i18next: ' + err);
  }
  ValidateRequest.call(this);
});

We just looked at this.locale and this.resources and we’ll soon look at ValidateRequest, so let’s examine this.i18n and sprintf.

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:

  • overloadTranslationOptionHandler makes for more readable translation options (i.e. not using an options object)
  • returnObjects allows 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
  • lng and resources is 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 ValidateRequest.

Which, incidentally, is what we’ll do next. Continue reading about ValidateRequest on the Alexa Skills Kit Node.js SDK.