Developing for Alexa

alt text

Serverless makes it easy to develop Alexa skills.

Alexa Skill

To create a new Alexa skill, you have to go to Amazon Developer Website and create an Alexa Skill. create a new skill

Now, you should add a new skill.

create a new skill

It's important to give your Alexa Skill a unique invocation name. It has to be two words at least unless you have a copyright for a fancy brand name like (c) Apple. alexa invocation name

Our Alexa app has quite simple interaction model. When you say 'Alexa, reason test one hello', it'll respond with greeting. You can use Interaction Builder to build a model:

alexa model Press Build model and wait for 2-3 minutes while Amazon is building your interaction model.

 

Now, we are ready to create a Lambda function that will process requests from Alexa.

 

The serverless.yml will look like this:

service: alexa-event-handler

provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-1
  profile: default
  memorySize: 256 # optional, in MB, default is 1024
  stage: dev

functions:  
  skillReasonTestOne:
    handler: handler.skillReasonTestOne
    events:
      - alexaSkill

The handler.js will look like this:

module.exports.skillReasonTestOne = (event, context, callback) => {
  console.log('EVENT', JSON.stringify(event))
  const response = {
    version: '1.0',
    response: {
      outputSpeech: {
        type: 'PlainText',
        text: `Hello from Reason Serverless!`,
      },
      shouldEndSession: false,
    },
  };

  callback(null, response);
}

The last step for our skill to work is to get our Lambda ARN and save it to our Amazon Alexa skill.

serverless deploy -v

you'll get the Stack Output:

Stack Outputs
SkillReasonTestOneLambdaFunctionQualifiedArn:
arn:aws:lambda:eu-west-1:xxxxxxxxxxxx:function:alexa-event-handler-dev-skillReasonTestOne:1

Here :1 is our Lambda version number. With each deployment, it'll increase. If you would like to fix your Alexa to use specific Lambda version number and not use Latest deployment, just include it as part of ARN. To save Lambda ARN to our Alexa skill, go to Configuration tab: lambda configuration

Now, Alexa Skill is ready to be tested. If you have Alexa connected to your account, you can just say Alexa reason test one hello and you should receive a response Hello from Reason Serverless. Otherwise, you can head to Test tab and provide the utterance hello in Service Simulator section. You should receive a response from your lambda:

{
  "version": "1.0",
  "response": {
    "outputSpeech": {
      "type": "PlainText",
      "text": "Hello from Reason Serverless!"
    },
    "speechletResponse": {
      "outputSpeech": {
        "text": "Hello from Reason Serverless!"
      },
      "shouldEndSession": false
    }
  },
  "sessionAttributes": {}
}