npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

alexa-ability-context

v0.4.0

Published

Simplify building multistep conversations with context-aware intent handling

Downloads

9

Readme

alexa-ability-context Build Status

Simplify building multistep conversations with context-aware intent handling.

Example

import { Ability, events } from 'alexa-ability';
import { handleAbility } from 'alexa-ability-lambda-handler';
import { trackContext, context } from 'alexa-ability-context';

// create ability
const app = new Ability({
    applicationId: 'your-app-id'
});


// add middleware to track context
app.use(trackContext());


app.on('OrderPizzaIntent', function(req) {
    req.say('Are you sure you want to order pizza?').send();
});


// check against previous event
app.on(events.yes, context.after('OrderPizzaIntent', function(req) {
    req.say('Are you really really sure?').send();
}));


// do more complex checks with a regex
app.on(events.yes, context.matches(/.*OrderPizzaIntent:AMAZON.YesIntent$/, function(req) {
    orderPizza(function() {
        req.say('Your pizza is on its way!').end();
    });
}));

app.on(events.no, function() {
    req.say('Ok, goodbye.').end();
});

export const handler = handleAbility(app);

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: defaults context, 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:

[
    { "event": "launch" },
    { "event": "ExampleIntent" }
]
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 name
  • handler: a standard alexa-ability handler that accepts req and next 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 accepts req and next 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 returns true or false.
  • handler: a standard alexa-ability handler that accepts req and next as arguments.