• Stars
    star
    280
  • Rank 147,492 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for effortless Alexa Skill development with AWS Lambda

  Alexa Skill Kit  

Alexa Skill Kit

npm npm

Library for effortless Alexa Skill development with AWS Lambda.

🚀 Installation 🤔 How it works 🤖 Usage 📕 API reference 🤘 How to contribute ⚖️ License

Installation

Alexa Skill Kit is available as a Node.js module on NPM. To install it, run the following command in an existing Node.js project:

npm install alexa-skill-kit --save

How it works

Alexa Skill Kit flow

Usage

Alexa Skill Kit is a library that simplifies the development of Alexa Skills with Node.js and AWS Lambda. It doesn't require any specific deploy style, it can work with manually created Lambda functions, deployment via Claudia.js, etc.

Since I recommend Claudia.js because of it's simplicity all the following examples will use it. You can get it from NPM here: npmjs.com/package/claudia.

After installing the Alexa Skill Kit from NPM, require it in your code:

const alexaSkillKit = require('alexa-skill-kit')

or with import* syntax:

import alexaSkillKit from 'alexa-skill-kit'

* import syntax is not supported in Node.js, you need to use additional library like Babel to make it work.

After requiring it, you simply need to pass event and context from Lambda function, beside event and function, you need to pass a handler function as the last param.

For Example:

const alexaSkillKit = require('alexa-skill-kit')

exports.handler = function(event, context) {
  alexaSkillKit(event, context, parsedMessage => {
    // Do something
  })
}

How to reply

Alexa Skill Kit simplifies replying to Alexa requests. There's a few things you can do:

| 📝 Replying with a simple text | 🛠 Replying with an object | 💄 More complex replies | ⏳ Async replies | | ----- | ----- | ----- | ----- | ----- |

Keep on reading and all of them will be explained bellow.

Replying with a simple text

If you want Alexa to reply with a simple text you simply need to return the text in a handler function. Alexa Skill Kit will wrap the text in a valid object.

For example:

const alexaSkillKit = require('alexa-skill-kit')

exports.handler = function(event, context) {
  alexaSkillKit(event, context, parsedMessage => {
    return 'Hello'
  })
}

An example above will reply to Alexa with the following object:

{
  "version": "1.0",
  "response": {
    "shouldEndSession": true,
    "outputSpeech": {
      "type": "PlainText",
      "text": "Hello"
    }
  }
}

As you can see, session will be closed by default. To keep the session opened or to answer with more complex request you can send an object instead of the text.

Replying with an object

If you want to keep the session opened or you want to return something more than a simple output speech you can send an object and Alexa Skill Kit will not modify it.

For example:

const alexaSkillKit = require('alexa-skill-kit')

exports.handler = function(event, context) {
  alexaSkillKit(event, context, parsedMessage => {
    return {
      version: '1.0',
      response: {
        shouldEndSession: false,
        outputSpeech: {
          type: 'PlainText',
          text: 'Hello'
        },
        card: {
          type: 'Simple',
          title: 'Simple Card',
          content: 'An example for a simple card reply'
        }
      }
    }
  })
}

More complex replies

Building objects manually is hard and boring. I recommend using Alexa Message Builder module to simplify building more complex replies.

Alexa Message Builder is available on NPM, and you can add it to your project with following command:

npm install alexa-message-builder --save

After that simply require it with Alexa Skill Kit like this:

const alexaSkillKit = require('alexa-skill-kit')
const AlexaMessageBuilder = require('alexa-message-builder')

exports.handler = function(event, context) {
  alexaSkillKit(event, context, parsedMessage => {
    return new AlexaMessageBuilder()
      .addText('Hello')
      .addSimpleCard('Simple Card', 'An example for a simple card reply')
      .keepSession()
      .get()
  })
}

An example above is the same as the example from "Replying with an object" section.

Why Alexa Message Builder is not the part of Alexa Skill Kit?

Well, without a real reason, Alexa Message Builder was developed before Alexa Skill Kit, and they are separate modules in case anyone needs just one of them without the other.

Async replies

Alexa Skill Kit uses promises to handle async replies. To be able to reply asynchronously simply return the promise and resolve it with a text or an object and Alexa Skill Kit will apply the same rules as above.

For example:

const alexaSkillKit = require('alexa-skill-kit')

exports.handler = function(event, context) {
  alexaSkillKit(event, context, parsedMessage => {
    return fetchSomethingFromTheServer()
      .then(result => doSomethingWithTheResult(result))
      .then(processedResult => {
        return `Here's an info from the server: ${processedResult}`
      })
  })
}

Alexa Skill Kit will answer with the text:

`Here's an info from the server: ${processedResult}`

API reference

Alexa Skill Kit (function) receives following attributes:

  • Event (object, required) — an unmodified object received from Amazon Alexa. If your Lambda function receives an object that is not valid Amazon Alexa request it will not parse it and your handler function will never be called.
  • Context (object, required) — a context object received on AWS Lambda, it is recommended not to modify it, but Alexa Skill Kit will use context.succeed and context.fail functions only, so you can pass a custom object that contains those two functions from and original context object.
  • Handler (function, required) — a function that receives parsed event and returns a reply that will be passed to Amazon Alexa.

Handler function is a function that receives parsed event and can return string, object or a promise that will return string or object when it is resolved.

In case you don't want Alexa to reply simply return null or don't return at all.

Parsed object is an object that contains following properties:

  • type (string) - type of the request, it can be 'LaunchRequest', 'IntentRequest', 'SessionEndedRequest', or one of the 'AudioPlayer' or 'PlaybackController' requests
  • request (object) - full request object from Alexa request
  • intent: (object, can be null) - intent object from Alexa request, if the request does not contain intent, this property will be null
  • session: (object, can be null) - session object from Alexa request, if session doesn't exists in the request, this property will be null
  • sessionAttributes: (object) - an object with session attributes from Alexa request, if they does not exist, this property will be an empty object ({})
  • user: (object) - user object from Alexa request, Alexa Skill Kit will try to load user from the context, then from the session and finally, it'll set an empty object if user info is not found
  • originalRequest: (object) - full Alexa request, as received from AWS Lambda and as described in an official documentation.

Contribute

Folder structure

The main body of code is in the lib directory.

The tests are in the spec directory, and should follow the structure of the corresponding source files. All executable test file names should end with -spec, so they will be automatically picked up by npm test. Any additional project files, helper classes etc that must not be directly executed by the test runner should not end with -spec. You can use the spec/helpers directory to store Jasmine helpers, that will be loaded before any test is executed.

Running tests

We use Jasmine for unit and integration tests. Unless there is a very compelling reason to use something different, please continue using Jasmine for tests. The existing tests are in the spec folder. Here are some useful command shortcuts:

Run all the tests:

npm test

Run only some tests:

npm test -- filter=prefix

Get detailed hierarchical test name reporting:

npm test -- full

We use ESLint for syntax consistency, and the linting rules are included in this repository. Running npm test will check the linting rules as well. Please make sure your code has no linting errors before submitting a pull request.

License

MIT - See LICENSE

More Repositories

1

scottyjs

Deploy static websites and single page apps to AWS S3 and CloudFront with a single command
JavaScript
705
star
2

alexa-message-builder

Simple message builder for Alexa replies.
JavaScript
100
star
3

lambda-audio

Run Sound eXchange (SoX), the Swiss Army knife of audio manipulation, with Lame on AWS Lambda
JavaScript
58
star
4

space-explorer-bot

A small FB Messenger chat bot using NASA API and Claudia Bot Builder.
JavaScript
57
star
5

huh

"Virus due to computers having unsafe sex." - Random BOFH-style excuses
JavaScript
33
star
6

vreme

Date formatting by example
JavaScript
31
star
7

souffleur

Simple command line prompt with retry for empty answers
JavaScript
21
star
8

cyrillic-to-latin

Convert cyrillic characters to latin.
JavaScript
20
star
9

fuckyou

Kill any process (╯°□°)╯︵ ssǝɔoɹd
JavaScript
14
star
10

random

4
star
11

crypto-skill

Crypto Skill - an Alexa skill developed at NoSlidesConf 2017 live coding session
JavaScript
4
star
12

holyjs-bot

Simple Telegram bot for HolyJS conference
JavaScript
4
star
13

space-explorer-bot-viber

Space Explorer bot for Viber
JavaScript
4
star
14

laptop-friendly-bot

LaptopFriendly chatbot
JavaScript
4
star
15

artproject-scraper

Get random art and metadata from Google Art Project
JavaScript
3
star
16

web-bot-ui

JavaScript
2
star
17

bartender-skill

Bartender skill for Alexa can recommend a cocktail and give you an info about ingredients and preparation.
JavaScript
2
star
18

serverless-apps-with-typescript-article

[WIP]
TypeScript
1
star
19

website

Personal website
1
star
20

test-video

1
star
21

how-to-build-a-website-that-will-eventually-work-on-mars

Conferece talk
1
star
22

workshop-meetup-app-frontend

JavaScript
1
star
23

amsterdam-facts

A serverless API demo for AmsterdamJS conference
JavaScript
1
star
24

for-conferences

Abstracts and links for videos/slides of my conference talks
1
star
25

fake-css-mouse-tracking

Just an old demo
CSS
1
star
26

serverless-svg-to-png-converter

1
star
27

scottyjs.com

Website for Scotty.js
HTML
1
star
28

flipout

Flip text upside-down
JavaScript
1
star
29

offlineweb.rocks

1
star
30

github-issue-to-slack-channel

Send Github issues to Slack channel via AWS Lambda and NodeJS
JavaScript
1
star