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

algoliasearch-alexa

v1.1.3

Published

AlgoliaSearch Alexa Skills Kit Adapter

Downloads

52

Readme

Table of Contents generated with DocToc

Algolia Alexa Skills Kit Adapter

This is an adapter that allows you to use the Algolia search API easily within your Alexa Skills Kit Skill. It provides tools for integrating Algolia search and a framework for structuring your Alexa skill.

Developed to be used on Amazon Lambda, you set up your intent handlers normally except for any that you want to leverage Algolia. For these handlers, a configuration object must be provided that defines the handler you want to call upon completion of the Algolia search. Algolia will be queried automatically, then provide an object with the results, intent, session, and response to your defined handler.

Here you can see an example usage:

const algoliaAlexaAdapter = require('algoliasearch-alexa').default;

const handlers = {
  LaunchRequest () {
    this.emit(':tell', 'Welcome to the skill!');
  },
  SearchProductIntent: {
    answerWith (data) {
      if(data.results.length) {
        this.emit(':tell', `There were ${data.results.hits.length} products found.`);
      } else {
        this.emit(':tell', 'We could find no products. Please try again.');
      }
    },
    params: {
      hitsPerPage: 1,
      filters (requestBody) {
        return `brand:${requestBody.request.intent.slots.brand.value}`;
      }
    },
  },
  CustomHelpIntent () {
    const speechOutput = 'Find one of 10,000 products from the Product Store, powered by Algolia.';
    this.emit(':ask', speechOutput);
  },
  Unhandled () {
    this.emit(':ask', 'Look for products in the Product Store.');
  },
};

const voiceSearch = algoliaAlexaAdapter({
  algoliaAppId: 'applicationId',
  algoliaApiKey: 'publicSearchKey',
  defaultIndexName: 'products',
  alexaAppId: 'amzn1.echo-sdk-ams.app.[unique-value-here]',
  handlers,
});

module.exports = voiceSearch;

Quick start guide

Follow this guide to quickly start with Algolia and Alexa.

API Description

algoliaAlexaAdapter

This function accepts a single argument, which is a configuration object.

This configuration object accepts:

  • algoliaAppId: The app ID from your Algolia application (required)
  • algoliaApiKey: The public search key associated with your Algolia application (required)
  • alexaAppId: Used to verify that the request is coming from your Alexa Skill, responding with an error if defined and requesting application does not match this ID; optional but recommended
  • defaultIndexName: The index you want to query on Algolia (required)
  • handlers: An object with your standard request (LaunchRequest, IntentRequest, or SessionEndedRequest) or built-in and intent handlers (required)

Handlers Configuration

Each handler can be configured in one of two ways. How it's configured depends on whether one wants to query Algolia first or not.

Without Querying Algolia

Specify a key-value pair where the key is the intent handler name and the value is a function. The function will accept no arguments, but has the current request information bound to this, provided by the Alexa service via Lambda.

Querying Algolia

Specify a key-value pair where the key is the intent handler name and the value is an object. That object contains a function answerWith which will be invoked following the Algolia search. This accepts one argument: an object with values for the keys of results from Algolia and event from the Alexa service.

State Management

States in the Alexa Skills Kit represent, roughly, different steps in the skill flow process. For example, there can be a state for starting a game, a state for being in the middle of a turn, and an empty state that represents the skill launch. You can read more here at the Alexa Skills Kit SDK README.

To define your states for each handler, provide an array of objects, with each that you want tied to a specific state to have a key of state:

const states = {
  SEARCHINGMODE: '_SEARCHINGMODE'
};

const handlers = [
  {
    NewSession () {
      this.handler.state = states.SEARCHINGMODE;
      this.emit(':ask', 'Welcome to the skill! What product would you like to find?');
    },
  }, {
    state: states.SEARCHINGMODE,
    'AMAZON.YesIntent': {
      answerWith (data) {
        // Do something...
      }
    }
  }
];

Localization

You can set your localization strings via the languageStrings option on the top level object. Within the intents, you will invoke them with this.t as normal. See here for more information on localizing a skill.

Dev

$ npm run dev

Linting

Lints using eslint:

$ npm run lint

Autofixer:

$ npm run lint:fix