0.1.0 • Published 6 years ago

alexa-chatbase-interceptors v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

chatbase-interceptors

Installation

npm install @boxoffice/chatbase-interceptors

Usage

Simply initialize the interceptors with your Chatbase API key and add them to your skill's lambda.

const Alexa = require('ask-sdk-core');
const chatbaseInterceptors = require('@boxoffice/chatbase-interceptors');

const {
  ChatbaseRequestInterceptor,
  ChatbaseResponseInterceptor,
  ChatbaseInterceptingErrorHandler,
} = chatbaseInterceptors(process.env.CHATBASE_API_KEY);

// ... declare your handlers and interceptors

module.exports.handler = Alexa.SkillBuilders.custom()

  // ChatbaseRequestInterceptor aggregates data from the request to track the user's request
  // The order of the interceptors does not matter
  .addRequestInterceptors(...requestInterceptors, ChatbaseRequestInterceptor)

  // ChatbaseInterceptingErrorHandler does not actually handle errors.
  // It uses the canHandle method to set the not_handled flag on the user request.
  // For this reason, you should set it as the first error handler to intercept all errors
  .addErrorHandlers(ChatbaseInterceptingErrorHandler, ...errorHandlers)

  // ChatbaseResponseInterceptor aggregates data from the response
  // and sends all tracking information to your Chatbase app.
  // To be as accurate as possible in terms of response time, it should be put last
  .addResponseInterceptors(...responseInterceptors, ChatbaseResponseInterceptor)

  // After that, nothing changes
  .addRequestHandlers(...requestHandlers)
  .lambda();

Optionally, if you want to set a response intent or mark a request as not handled (see the Chatbase not_handled concept), a request attributes is available. By default, the only request marked as not handled are:

  • SessionEndedRequest with ERROR as reason
  • IntentRequest with AMAZON.FallbackIntent as intent name
const SendTomatoHandler = {
  canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
      && Alexa.getIntentName(handlerInput.requestEnvelope) === 'SingASongIntent',
  },
  handle(handlerInput) {
    // Set your skill's intent
    handlerInput
      .attributesManager.getRequestAttributes()
      .chatbase.setResponseIntent('SendTomato');

    // Proceed with sending a tomato
  }
}

const UnhandledRequestHandler = {
  canHandle() {
    return true;
  },
  handle(handlerInput) {
    // Mark this user request as not handled
    handlerInput
      .attributesManager.getRequestAttributes()
      .chatbase.setRequestAsNotHandled();

    // Proceed with fixing the situation
  },
}
0.1.0

6 years ago