alexa-ability-context
Simplify building multistep conversations with context-aware intent handling.
Example
;;; // create abilityconst app = applicationId: 'your-app-id'; // add middleware to track contextapp; app; // check against previous eventapp; // do more complex checks with a regexapp; app; const handler = ;
API
trackContext(options) -> middleware
A middleware factory that takes an optional options
object. The currently supported
options are:
key
: defaults to__context__
, the session key to store the context between requests.property
: defaultscontext
, the property to expose the context object as on the request.
req.context
The trackContext
middleware will add an additional property to the request object called
context
. Which will look something like this:
req.context.now
The object that will be added to the context for future requests. By default it is just
{ event: 'name' }
, but you're free to modify it or add properties however you like.
req.context.destroy()
Clears the context.
req.context.skip()
Causes the req.context.now
object to not be persisted.
context
context.after(event, handler) -> handler
Creates a new handler function that only executes when the previous intent matches.
The two arguments are:
event
: an event namehandler
: a standard alexa-ability handler that acceptsreq
andnext
as arguments.
The string the regex will be tested against will look like this:
FirstIntent:SecondIntent:ThirdIntent
context.matches(regex, handler) -> handler
Creates a new handler function that only executes when the conversation context matches the given regular expression.
The two arguments are:
regex
: a regular expression.handler
: a standard alexa-ability handler that acceptsreq
andnext
as arguments.
context.custom(fn, handler) -> handler
Creates a new handler function that only executes when the custom fn
returns a true
.
The two arguments are:
fn
: a function that takes in the request object and returnstrue
orfalse
.handler
: a standard alexa-ability handler that acceptsreq
andnext
as arguments.