Skip to content

DBClient & Logger & cloneHandler

Compare
Choose a tag to compare
@hideokamoto hideokamoto released this 02 May 00:24
· 198 commits to master since this release

DBClient

We can access to DynamoDB easier.
If you are using S3 adapter, we have to write a aws-sdk script.
But to use this, we don't think about it.

import { HandlerInput } from 'ask-sdk-core'
import { DBClient, DBItem, getUserId } from 'ask-utils'

const handler = {
  canHandle() {
    return true
  },
  async handle(handlerInput: HandlerInput) {
      // configure client
      const client = new DBClient( 'MyTable' ) 

      // Get user id
      const userId = getUserId(handlerInput)

      // Get DB data
      const data = await client.get(userId)

    // Update DB data
     const score = data.count || 1
     await client.put( userId, { score } )

      // return response
      return handlerInput.responseBuilder
          .speak(`You launch the skill at ${score} times !`)
          .getResponse()
  }
}

Logger

We can log the skill request/response.

import * as Alexa from 'ask-sdk'
import {
    RequestLogger,
    ResponseLogger
} from 'ask-utils'



export const handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers({
       ...
    )
    .addRequestInterceptors(
        RequestLogger
    )
    .addResponseInterceptors(
        ResponseLogger
    )
    .lambda()

cloneHandler

We can clone your handler object.

import * as Alexa from 'ask-sdk'
import {
    mergeHandler
} from 'ask-utils'

// The handler will handle the `MyIntent` request
const baseHandler: Alexa.RequestHandler = {
    canHandle(handlerInput) {
        if (Alexa.getRequestType(handlerInput.requestEnvelope) !== 'IntentRequest') return false
        return Alexa.getIntentName(handlerInput.requestEnvelope) === 'MyIntent'
    },
    handle(handlerInput) {
        return handlerInput.responseBuilder.speak('Hello world!').getResponse()
    }
}

// And the handler supports the `AMAZON.YesIntent` and the response is same as `baseHandler`
const clonedHandler: Alexa.RequestHandler = mergeHandler(
    baseHandler,
    {
        canHandle(handlerInput) {
            if (Alexa.getRequestType(handlerInput.requestEnvelope) !== 'IntentRequest') return false
            return Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent' 
        }
    }
)

Happy coding!