Permalink
Please sign in to comment.
Showing
with
457 additions
and 0 deletions.
- +18 −0 .eslintrc.json
- +1 −0 .gitignore
- +9 −0 LICENSE
- +1 −0 README.md
- +1 −0 index.js
- +14 −0 lib/index.js
- +25 −0 lib/parse.js
- +19 −0 lib/reply.js
- +35 −0 package.json
- +10 −0 spec/alexa-skill-kit-spec.js
- +246 −0 spec/parse-spec.js
- +51 −0 spec/reply-spec.js
- +18 −0 spec/support/jasmine-runner.js
- +9 −0 spec/support/jasmine.json
| @@ -0,0 +1,18 @@ | ||
| +{ | ||
| + "extends": "defaults", | ||
| + "env": { | ||
| + "node": true, | ||
| + "es6": true | ||
| + }, | ||
| + "parserOptions": { | ||
| + "ecmaVersion": 6, | ||
| + "sourceType": "module" | ||
| + }, | ||
| + "rules": { | ||
| + "no-console": "off", | ||
| + "semi": ["error", "never"], | ||
| + "indent": ["error", 2], | ||
| + "quotes": ["error", "single", {"avoidEscape": true, "allowTemplateLiterals": true}], | ||
| + "prefer-arrow-callback": "error" | ||
| + } | ||
| +} |
| @@ -0,0 +1 @@ | ||
| +node_modules |
9
LICENSE
| @@ -0,0 +1,9 @@ | ||
| +The MIT License (MIT) | ||
| + | ||
| +Copyright (c) 2017 Slobodan Stojanović | ||
| + | ||
| +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
| + | ||
| +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
| + | ||
| +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| @@ -0,0 +1 @@ | ||
| +# Alexa Skill Kit |
1
index.js
| @@ -0,0 +1 @@ | ||
| +module.exports = require('./lib') |
14
lib/index.js
| @@ -0,0 +1,14 @@ | ||
| +'use strict' | ||
| + | ||
| +const parse = require('./parse') | ||
| +const reply = require('./reply') | ||
| + | ||
| +function alexaSkillKit(message, context, handler) { | ||
| + parse(message) | ||
| + .then(handler) | ||
| + .then(reply) | ||
| + .then(context.succeed) | ||
| + .catch(context.fail) | ||
| +} | ||
| + | ||
| +module.exports = alexaSkillKit |
25
lib/parse.js
| @@ -0,0 +1,25 @@ | ||
| +'use strict' | ||
| + | ||
| +module.exports = function parse(message) { | ||
| + return new Promise((resolve, reject) => { | ||
| + if (typeof message !== 'object' || !message || !message.request || (['LaunchRequest', 'IntentRequest', 'SessionEndedRequest'].indexOf(message.request.type) < 0) && !/^AudioPlayer\./.test(message.request.type) && !/^PlaybackController\./.test(message.request.type)) { | ||
| + reject({ | ||
| + error: 'Not valid Alexa request', | ||
| + description: 'It seems that event is not a valid Alexa request. In case you think this is an issue, please report it at https://github.com/stojanovic/alexa-skill-kit/issues and include debug info from this object.', | ||
| + debug: message | ||
| + }) | ||
| + } | ||
| + | ||
| + const parsedMessage = { | ||
| + type: message.request.type, | ||
| + request: message.request, | ||
| + intent: message.request.intent || null, | ||
| + session: message.session || null, | ||
| + sessionAttributes: (message.session && message.session.attributes) || {}, | ||
| + user: (message.context && message.context.user) || (message.session && message.session.user) || {}, | ||
| + originalRequest: message | ||
| + } | ||
| + | ||
| + resolve(parsedMessage) | ||
| + }) | ||
| +} |
19
lib/reply.js
| @@ -0,0 +1,19 @@ | ||
| +'use strict' | ||
| + | ||
| +module.exports = function reply(message) { | ||
| + if (typeof message === 'string' && message.trim().length) { | ||
| + return { | ||
| + version: '1.0', | ||
| + response: { | ||
| + shouldEndSession: true, | ||
| + outputSpeech: { | ||
| + type: 'PlainText', | ||
| + text: message | ||
| + } | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + if (typeof message === 'object' && message !== null && !Array.isArray(message)) | ||
| + return message | ||
| +} |
35
package.json
| @@ -0,0 +1,35 @@ | ||
| +{ | ||
| + "name": "alexa-skill-kit", | ||
| + "version": "1.0.0", | ||
| + "description": "Library for effortless Alexa Skill development with AWS Lambda", | ||
| + "main": "index.js", | ||
| + "scripts": { | ||
| + "pretest": "eslint lib spec *.js", | ||
| + "test": "node spec/support/jasmine-runner.js", | ||
| + "debug": "node debug spec/support/jasmine-runner.js" | ||
| + }, | ||
| + "keywords": [ | ||
| + "alexa", | ||
| + "skill", | ||
| + "amazon", | ||
| + "chatbots", | ||
| + "bots", | ||
| + "echo" | ||
| + ], | ||
| + "author": "Slobodan Stojanovic <slobodan@cloudhorizon.com> (http://slobodan.me/)", | ||
| + "license": "MIT", | ||
| + "devDependencies": { | ||
| + "eslint": "^3.15.0", | ||
| + "eslint-config-defaults": "^9.0.0", | ||
| + "jasmine": "^2.5.3", | ||
| + "jasmine-spec-reporter": "^3.2.0" | ||
| + }, | ||
| + "repository": { | ||
| + "type": "git", | ||
| + "url": "https://github.com/stojanovic/alexa-skill-kit.git" | ||
| + }, | ||
| + "bugs": { | ||
| + "url": "https://github.com/stojanovic/alexa-skill-kit/issues" | ||
| + }, | ||
| + "homepage": "https://github.com/stojanovic/alexa-skill-kit" | ||
| +} |
| @@ -0,0 +1,10 @@ | ||
| +/* global describe, it, expect */ | ||
| +'use strict' | ||
| + | ||
| +const underTest = require('../lib') | ||
| + | ||
| +describe('Alexa Skill Kit', () => { | ||
| + it('should export a function', () => { | ||
| + expect(typeof underTest).toBe('function') | ||
| + }) | ||
| +}) |
| @@ -0,0 +1,246 @@ | ||
| +/* global describe, it, expect */ | ||
| +'use strict' | ||
| + | ||
| +const underTest = require('../lib/parse') | ||
| + | ||
| +describe('Parse', () => { | ||
| + it('should be a function', () => { | ||
| + expect(typeof underTest).toBe('function') | ||
| + }) | ||
| + | ||
| + it('should return a promise', () => { | ||
| + expect(typeof underTest()).toBe('object') | ||
| + expect(typeof underTest().then).toBe('function') | ||
| + expect(typeof underTest().catch).toBe('function') | ||
| + }) | ||
| + | ||
| + it('should reject a promise if message is not an object', done => { | ||
| + Promise.all(['', 123, null, [], [1, 2, 3]].map(item => { | ||
| + return underTest(item) | ||
| + .then(() => { | ||
| + throw new Error('fail') | ||
| + }) | ||
| + .catch(err => { | ||
| + return expect(err).toEqual({ | ||
| + error: 'Not valid Alexa request', | ||
| + description: 'It seems that event is not a valid Alexa request. In case you think this is an issue, please report it at https://github.com/stojanovic/alexa-skill-kit/issues and include debug info from this object.', | ||
| + debug: item | ||
| + }) | ||
| + }) | ||
| + })) | ||
| + .then(done) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + it('should reject a promise if message is an invalid object', done => { | ||
| + Promise.all([ | ||
| + {}, | ||
| + { | ||
| + a: 1 | ||
| + }, | ||
| + { | ||
| + request: {} | ||
| + }, | ||
| + { | ||
| + request: { | ||
| + a: 1 | ||
| + } | ||
| + }, | ||
| + { | ||
| + request: { | ||
| + type: 'SomeCustomType' | ||
| + } | ||
| + } | ||
| + ].map(item => { | ||
| + return underTest(item) | ||
| + .then(() => { | ||
| + throw new Error('fail') | ||
| + }) | ||
| + .catch(err => { | ||
| + return expect(err).toEqual({ | ||
| + error: 'Not valid Alexa request', | ||
| + description: 'It seems that event is not a valid Alexa request. In case you think this is an issue, please report it at https://github.com/stojanovic/alexa-skill-kit/issues and include debug info from this object.', | ||
| + debug: item | ||
| + }) | ||
| + }) | ||
| + })) | ||
| + .then(done) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + const standardRequestTypes = ['LaunchRequest', 'IntentRequest', 'SessionEndedRequest'] | ||
| + standardRequestTypes.forEach(item => { | ||
| + it(`should parse the object it request type is ${item}`, done => { | ||
| + return underTest({ | ||
| + request: { | ||
| + type: item | ||
| + } | ||
| + }) | ||
| + .then(done) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + }) | ||
| + | ||
| + const otherRequestTypes = [ | ||
| + 'AudioPlayer.PlaybackStarted', | ||
| + 'AudioPlayer.PlaybackFinished', | ||
| + 'AudioPlayer.PlaybackStopped', | ||
| + 'AudioPlayer.PlaybackNearlyFinished', | ||
| + 'AudioPlayer.PlaybackFailed', | ||
| + 'PlaybackController.NextCommandIssued', | ||
| + 'PlaybackController.PauseCommandIssued', | ||
| + 'PlaybackController.PlayCommandIssued', | ||
| + 'PlaybackController.PreviousCommandIssued' | ||
| + ] | ||
| + otherRequestTypes.forEach(item => { | ||
| + it(`should parse the object it request type is ${item}`, done => { | ||
| + return underTest({ | ||
| + request: { | ||
| + type: item | ||
| + } | ||
| + }) | ||
| + .then(done) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + }) | ||
| + | ||
| + it('should return parsed object with default values when promise resolves', done => { | ||
| + const msg = { | ||
| + request: { | ||
| + type: 'LaunchRequest' | ||
| + } | ||
| + } | ||
| + underTest(msg) | ||
| + .then(parsedObject => { | ||
| + expect(parsedObject).toEqual({ | ||
| + type: 'LaunchRequest', | ||
| + request: msg.request, | ||
| + originalRequest: msg, | ||
| + intent: null, | ||
| + session: null, | ||
| + sessionAttributes: {}, | ||
| + user: {} | ||
| + }) | ||
| + done() | ||
| + }) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + it('should return parsed object with session and session attributes if they exists when promise resolves', done => { | ||
| + const msg = { | ||
| + request: { | ||
| + type: 'LaunchRequest' | ||
| + }, | ||
| + session: { | ||
| + attributes: { | ||
| + a: 1 | ||
| + } | ||
| + } | ||
| + } | ||
| + underTest(msg) | ||
| + .then(parsedObject => { | ||
| + expect(parsedObject).toEqual({ | ||
| + type: 'LaunchRequest', | ||
| + request: msg.request, | ||
| + originalRequest: msg, | ||
| + intent: null, | ||
| + session: msg.session, | ||
| + sessionAttributes: { | ||
| + a: 1 | ||
| + }, | ||
| + user: {} | ||
| + }) | ||
| + done() | ||
| + }) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + it('should return parsed object with user if it exists in context when promise resolves', done => { | ||
| + const msg = { | ||
| + request: { | ||
| + type: 'LaunchRequest' | ||
| + }, | ||
| + context: { | ||
| + user: { | ||
| + id: 123 | ||
| + } | ||
| + } | ||
| + } | ||
| + underTest(msg) | ||
| + .then(parsedObject => { | ||
| + expect(parsedObject).toEqual({ | ||
| + type: 'LaunchRequest', | ||
| + request: msg.request, | ||
| + originalRequest: msg, | ||
| + intent: null, | ||
| + session: null, | ||
| + sessionAttributes: {}, | ||
| + user: { | ||
| + id: 123 | ||
| + } | ||
| + }) | ||
| + done() | ||
| + }) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + it('should return parsed object with user if it exists in session when promise resolves', done => { | ||
| + const msg = { | ||
| + request: { | ||
| + type: 'LaunchRequest' | ||
| + }, | ||
| + session: { | ||
| + user: { | ||
| + id: 123 | ||
| + } | ||
| + } | ||
| + } | ||
| + underTest(msg) | ||
| + .then(parsedObject => { | ||
| + expect(parsedObject).toEqual({ | ||
| + type: 'LaunchRequest', | ||
| + request: msg.request, | ||
| + originalRequest: msg, | ||
| + intent: null, | ||
| + session: msg.session, | ||
| + sessionAttributes: {}, | ||
| + user: { | ||
| + id: 123 | ||
| + } | ||
| + }) | ||
| + done() | ||
| + }) | ||
| + .catch(done.fail) | ||
| + }) | ||
| + | ||
| + it('should return parsed object with intent if it exists when promise resolves', done => { | ||
| + const msg = { | ||
| + request: { | ||
| + type: 'LaunchRequest', | ||
| + intent: { | ||
| + name: 'GetZodiacHoroscopeIntent', | ||
| + slots: { | ||
| + ZodiacSign: { | ||
| + name: 'ZodiacSign', | ||
| + value: 'virgo' | ||
| + } | ||
| + } | ||
| + } | ||
| + } | ||
| + } | ||
| + underTest(msg) | ||
| + .then(parsedObject => { | ||
| + expect(parsedObject).toEqual({ | ||
| + type: 'LaunchRequest', | ||
| + request: msg.request, | ||
| + originalRequest: msg, | ||
| + intent: msg.request.intent, | ||
| + session: null, | ||
| + sessionAttributes: {}, | ||
| + user: {} | ||
| + }) | ||
| + done() | ||
| + }) | ||
| + .catch(done.fail) | ||
| + }) | ||
| +}) |
Oops, something went wrong.
0 comments on commit
02d33ba