@boxoffice/alexa-chatbase-interceptors
v1.0.2
Published
Provides interceptors to track interactions on Alexa custom skill with Chatbase
Downloads
8
Readme
Chatbase integration for Alexa custom skill
Installation
npm install @boxoffice/alexa-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 chatbase = require('@boxoffice/alexa-chatbase-interceptors')
const {
ChatbaseRequestInterceptor,
ChatbaseResponseInterceptor,
ChatbaseInterceptingErrorHandler
} = chatbase(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
withERROR
as reasonIntentRequest
withAMAZON.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
},
}
Options
You can provide custom options to the interceptors builder by passing on option oject after the API key. Here are the default options :
const chatbase = require('@boxoffice/alexa-chatbase-interceptors')
const options = {
// The platform to use when pushing to events to chatbase
// Platform can be used as a filter in Chatbase dashboard and should be use to differenciate
// your live environment from your dev/staging one.
platform: `alexa-${process.env.NODE_ENV || 'default'}`,
}
const interceptors = chatbase(process.env.CHATBASE_API_KEY, options)