alexa-router
The alexa-router
project allows you to easily develop custom skills for
Amazon's Alexa.
Getting started
All you need to do is npm install -s alexa-router
and you're already done
with the setup.
Understanding actions
The router is configured through actions, next options and globals.
let Alexa = require('alexa-router')
let alexa = new Alexa.Router()
alexa.action('hello-world', {
handler: request => {
let response = request.response()
response.speech('Hello world!')
return response
}
})
alexa.action('global-hello', {
handler: request => {...},
global: {
type: 'intent',
intent: 'AMAZON.YesIntent'
}
})
alexa.action('here-be-dragons', {
handler: request => {
let response = request.response()
response.speech('Sorry, you can\'t activate that command right now')
}
})
alexa.action('say-the-weather', {
handler: (request, params) => {
let response = request.response()
if (params.sayWeather) {
response.speech('If you can\'t see the sky then it\'s probably cloudy')
response.endSession(true)
} else {
response.speech('Would you like me to read the weather?')
response.next({
type: 'intent',
intent: 'AMAZON.YesIntent',
action: 'say-the-weather',
params: { sayWeather: yes }
})
response.next({
type: 'unexpected',
action: 'here-be-dragons'
})
}
return response
}
})
Understanding the routing mechanism
How the internal router works?
- Check if the incoming request has next options
- If next options are present try to resolve the next action
- If no action was resolved see if there's an
unexpected
next configured - If there's no
unexpected
next then try to find a global unexpected
- If no global
unexpected
then throw RoutingFailed
error - If no next options are present in the request's session then try to match a global action
- If no global action was found try to find an
unexpected
global action - If no
unexpected
global action then throw RoutingFailed
HTTP handling
alexa-router
is HTTP server agnostic. This means that you can use it with
any Node.js library that can parse and reply JSON. An example using Express:
let express = require('express')
let bodyParser = require('body-parser')
let Alexa = require('alexa-router')
let app = express()
let alexa = new Alexa.Router()
alexa.action('my-action', ...)
app.post('/alexa/incoming', bodyParser.json(), (req, res) => {
alexa.dispatch(req.body)
.then(result => res.json(result))
.catch(err => {
res.send('Somthing bad happened...').status(500)
})
})
To-do
Testing
git clone https://github.com/estate/alexa-router && cd alexa-router
npm install && npm test