Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

alexa-ability demo

node v5.12.0
version: 0.2.0
endpointsharetweet
Welcome to alexa-ability. Below is a brief interactive example of how to use alexa-ability to start building your own skills or middleware. For a more in-depth information visit the docs (https://github.com/nickclaw/alexa-ability/tree/master/docs). If you want to play around with the demo, scroll down to the bottom and click the clone button. You'll then be able to edit the code and see what happens. No account required!
// this is just setup code, feel free to ignore it const Ability = require('alexa-ability').Ability; const Request = require('alexa-ability').Request; // here's a good example of what a raw request will look like const event = { "version": "1.0", "session": { "new": true, "sessionId": "the-session-id", "application": { "applicationId": "my-application-id" }, "attributes": { "foo": "bar" }, "user": { "userId": "the-users-id" } }, "request": { "type": "IntentRequest", "requestId": " the-request-id", "timestamp": "2015-05-13T12:34:56Z", "intent": { "name": "TestIntent", "slots": { "SlotName": { "name": "SlotName", "value": "SlotValue" } } } } } // basic handler that can be "awaited" function handleAsync(app, event) { return new Promise(function(res, rej) { app.handle(event, function(err, req) { if (err) rej(err); else res(req.toJSON()); }); }); } null;
Handling Events: It's extremely easy to handle intents with alexa-ability. The code below shows the most basic use case. Possible events you can handle include "launch", "end", and any intents you declare for your skill. This module also has every default intent stored as constants. Check the documentation to see what's available! Modify the code below to see the different types of responses you can create!
app = new Ability({ applicationId: 'my-application-id' }); app.on('TestIntent', function(req) { /* try out different responses */ // req.say('ssml', '<speak>Hello World!</speak>').ask(); // req.say('Hello World!').show("Hello..", "World!").tell(); // req.say('Hello World!').linkAccount().ask(); req.say('Hello World!').tell(); }); await handleAsync(app, event);
Request Class: The Request class provides a simple way to access the data of (and respond to) requests sent by Amazon to your skill. It includes the following features: 1. Easier access to request data like session attributes, user, and slots 2. Chainable methods to build and send responses. 3. Emits events when the request finishes or fails. You can explore the properties and methods of a request instance below.
new Request(event);
Middleware: Middleware functions are a flexible and powerful way to enhance your skill. They can be used to do simple tasks like logging a request, or more complicated things like asynchronously getting user data from a database. Here are some facts about middleware functions: 1. Middleware are called in the order they were added. 2. the "on" function is actually just a simple wrapper around a middleware function. 3. Middleware must either call the "next" function OR send a response when finished. 4. The "next" function can be used to handle errors by calling it like "next(err)"
app = new Ability({ applicationId: 'my-application-id' }); app.use(function(req, next) { console.log('> executing middleware!'); next(); }); app.on('TestIntent', function(req, next) { console.log('> executing handler!'); req.say('Hello World!').end(); }); app.use(function(req, next) { // this wont be called because gets handled above // order matters with middleware and handlers }); await handleAsync(app, event);
Error handling: Handler errors is very easy with alexa-ability, any middleware functions that accept three arguments are treated as error handlers, and will only be called if an error was thrown by a handler before them. You can use error handlers to catch errors anywhere in your skill. But typically you'll usually just want to put a single error handler as your last middleware. The example below shows how middleware functions are executed or skipped.
app = new Ability({ applicationId: 'my-application-id' }); app.use(function(err, req, next) { // not going to be called because // there is no error to handle upstream }); app.use(function(req, next) { console.log('> throwing error!'); throw new Error(); }); app.use(function(req, next) { // not going to be called because // there is an error upstream }); app.use(function(err, req, next) { console.log('> handling error!'); next(); }); app.on('TestIntent', function(req, next) { console.log('> executing handler!'); req.say('Hello World!').end(); }); await handleAsync(app, event);
Loading…

no comments

    sign in to comment