GithubHelp home page GithubHelp logo

isabella232 / alexa-app-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexa-js/alexa-app-router

0.0 0.0 0.0 26 KB

Router for alexa-app library.

License: MIT License

JavaScript 100.00%

alexa-app-router's Introduction

alexa-app-router

A simple router for the Alexa alexa-app library.

NPM Build Status

Table of Contents

Quick Start

Read the alexa-app documentation before using this add-on utility.

Sample app with code usage available in the alexa-adopt-a-pet project.

Installation

npm install alexa-app alexa-app-router --save

Registering the Router

var alexa = require('alexa-app');
var router = require('alexa-app-router');

var app = new alexa.app('app-name');

var config = {...};
var intents = {...};
var routes = {...};

router.addRouter(app, config, intents, routes);

Routes

Ul-style routing is strongly recommended. Special processing is done with route variables ({id}) and query parameters (?limit=10&offset=5).

var routes = {
    '/': launchHandler,
    '/exit': exitHandler,
    '/help': helpHandler,
    '/menu': menuHandler,
    '/pets': findPetHandler,
    '/pets/{petId}': petDetailsHandler,
    '/shelters': findShelterHandler,
};
function launchHandler(request, response) {...}
...(more handler functions)

Intents

alexa-app-router uses a special shorthand for registering intents, normal intent registration with alexa-app is not necessary.

var intents = {
  FindPetIntent: {
    slots: {ANIMAL_TYPE: 'ANIMAL_TYPE'},
    utterances: ['{adopt a |find a }{-|ANIMAL_TYPE}']
  },
  MenuIntent: {utterances: ['{menu|help}']},
  ...(more intents)
};

Config

Configuration for the router.

var config = {
  defaultRoutes: {
    'AMAZON.CancelIntent': '/exit',
    'AMAZON.HelpIntent': '/help',
    'AMAZON.NextIntent': '/',
    'AMAZON.PreviousIntent': '/',
    'AMAZON.RepeatIntent': '/',
    'AMAZON.ResumeIntent': '/',
    'AMAZON.StartOverIntent': '/',
    'AMAZON.StopIntent': '/exit',
    'AMAZON.YesIntent': '/',
    PetDetailsIntent: '/pets/{petId}',
    FindPetIntent: '/pets?offset=0',
    FindShelterIntent: '/shelters?limit=5',
    MenuIntent: '/menu'
  },
  pre: preHandler,
  post: postHandler,
  launch: launchHandler
};
function preHandler(request, response) {...},
function postHandler(request, response) {...},
function launchHandler(request, response) {...}

defaultRoutes

Contains the default routes to use when no route is specified. These are used on first launch, and when no route has otherwise been specified.

pre

Shorthand for app.pre = function preHandler(request, response) {...};

post

Shorthand for app.post = function postHandler(request, response) {...};

launch

Shorthand for app.launch = function launchHandler(request, response) {...};

Breaking Changes

From 0.0.X to 0.1.X breaking changes were introduced:

  • The whole routing paradigm has been made more intuitive, flexible, and powerful. Read the docs below for updates.

Gotchas and Important Notes

  1. Every intent your app uses (including Amazon default intents) must be specified in config.defaultRoutes.

Going to a Route

When prompting the user for a new response, provide the .route() command a map of routes to go to depending on the user's response. The route is chosen in the following way:

  1. Exact intent match from .route().
  2. Route specified on the default entry.
  3. Intent match on config.defaultRoutes.
function launchHandler(request, response) {
  var text = 'Welcome to my app. Would you like a list of commands?';
  response
    .say(text)
    .route({
      'default': '/'
      'AMAZON.CancelIntent': '/exit',
      'AMAZON.NoIntent': '/exit',
      'AMAZON.YesIntent': '/help',
      FindPetIntent: '/pets?offset=0',
      FindShelterIntent: '/shelters?limit=5'
    })
    .send();
}

This allows two useful features:

  1. Since Alexa can receive any available intent at any time, you can stop unexpected intents by using the default route here.
  2. You don't have to specify, for example, 'AMAZON.HelpIntent': '/help' every time if it is set up on config.defaultRoutes and you don't use default;

Internally the route is saved on the route session variable. .route(routes) is equivalent to:

response
  .shouldEndSession(false)
  .session('route', routes)

Route Parameters

Routes support both url and query string parameters.

E.g. .route('/pets/123/photos/45678?format=png&size=large') will match the route /pets/{petId}/photos/{photoId}.

The current route info can be retrieved with request.route:

request.route = {
  params: {
    petId: '123',
    photoId: '45678'
  },
  query: {
    format: 'png',
    size: 'large'
  },
  route: '/pets/{petId}/photos/{photoId}',
  url: '/pets/123/photos/45678?format=png&size=large'
}

This allows easy management of complex state flows. Consider the following interaction with the sample routes/intents above:

  1. User calls the Pet skill and asks to view pets. The config.defaultRoutes handler for PetIntent is set to '/pets?limit=1&offset=0' which calls the findPetHandler().
  2. User asks for the next pet and AMAZON.NextIntent is triggered. In the findPetHandler, dynamically route AMAZON.NextIntent to '/pets?limit=1&offset=' + (request.route.query.offset + 1).
  3. Suppose this returns a pet with an internal Id of 123 in findPetHandler(). When the user asks for more details, a MoreDetailsIntent is triggered.
  4. The MoreDetailsIntent can be dynamically routed to '/pets/' + pet.Id which calls petDetailsHandler() with the pet Id.

Backwards Compatibility

All functionality is backwards compatible with alexa-app. Intents can be registered normally, the router intents are simply a shorthand for convenience.

Special thanks to Matt Kruse for his excellent alexa-app, and Daniel Doubrovkine for his work maintaining it!

alexa-app-router's People

Contributors

dblock avatar nickcoury avatar tejashah88 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.