DEV Community

Hidetaka Okamoto
Hidetaka Okamoto

Posted on

Launch Utility package for Alexa Skills Kit SDK for Node.js

Alexa Skills Kit SDK for Node.js version 2 (ask-sdk) is very useful to create own Alexa Skill.
But some time we have to write long source code to handle Alexa's request.

const HelpIntentHandler = {
  canHandle (handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'
  },
...

I think the code handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent' can be more shorty.
So I've launched utility packages for ask-sdk.

What is ask-utils ?

ask-utils is utility packages for ask-sdk.

Install

$ npm i -S ask-utils

Easy to handle request

Before ask-utils

const HelpIntentHandler = {
  canHandle (handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'
  },
...

After ask-utils

const { canHandle } = require('ask-utils')
const HelpIntentHandler = {
  canHandle (handlerInput) {
    return canHandle(handlerInput, 'IntentRequest', 'AMAZON.HelpIntent')
  },
...

Easy to create random message

Alexa blog says Let's randomize your response!.
Alexa Skill Recipe: Randomize Your Responses to Add Variety to Your Skill
https://developer.amazon.com/ja/blogs/alexa/post/37e732b7-48fa-4940-9f12-9ffde7eeeaf8/alexa-skill-recipe-randomize-your-responses-to-add-variety-to-your-skill

So ask-utils provides helper function to make randomize response.

Before ask-utils

You have to make function to create random paharse.

function randomPhrase(myData) {

  // the argument is an array [] of words or phrases

  var i = 0;

  i = Math.floor(Math.random() * myData.length);

  return(myData[i]);

}
const welcomeGreetings = ['hello','howdy','hi', 'good day'];
const speachText = randomPhrase(welcomeGreetings);

After ask-utils

ask-utils provides helper function.

const { getRandomMessage } = require('ask-utils')
const welcomeGreetings = ['hello','howdy','hi', 'good day'];
const speachText = getRandomMessage(welcomeGreetings);

And More features !

Get slot value

const { canHandle, getSlotValueByName } = require('ask-utils')
const ExampleIntentHandler = {
  canHandle (handlerInput) {
    return canHandle(handlerInput, 'IntentRequest', 'ExampleIntent')
  },
  handle (handlerInput) {
    const yourName = getSlotValueByName(handlerInput, 'nameSlot')
    const speechText = `Hello ${yourName} ! Welcome to our skill !`

Progressive response

const { enqueueProgressiveResponseDirective } = require('ask-utils') 
const GetFirstEventHandler = {
  canHandle (handlerInput) {
    const request = handlerInput.requestEnvelope.request
    return request.type === 'IntentRequest' && request.intent.name === 'GetFirstEventIntent'
  },
  async handle (handlerInput) {
    try {
      await enqueueProgressiveResponseDirective(handlerInput, 'Please wait for a while')
    } catch (err) {
      // if it failed we can continue, just the user will wait longer for first response
      console.log(err)
    }
    // call some api
    const content = await get()
    return responseBuilder
        .speak(content)
        .getResponse()
  }
}

Need your feedback !

The package is developing in GitHub.
https://github.com/ask-utils/ask-utils

And I provides example project too.
https://github.com/ask-utils/example

If you have interest to the packages, please try it and give me feedback, feature request, pul request !

Thanks !

Top comments (0)