Friday 7 September 2018

Alexa Integration Step By Step with Lambda function



What Is an Alexa skill


Alexa is Amazon’s voice service and the brain behind millions of devices including Amazon Echo. Alexa provides capabilities, or skills, that enable customers to create a more personalized experience.

There are many ways to start building a skill. You can design your own custom model or start with a pre-built model.  Pre-built models are interaction models that contain a package of intents and utterances that you can add to your skill.

SELECTED

Custom

Design a unique experience for your users. A custom model enables you to create all of your skill's interactions.

Flash Briefing

Give users control of their news feed. This pre-built model lets users control what updates they listen to.
"Alexa, what's in the news?"

Smart Home

Give users control of their smart home devices. This pre-built model lets users turn off the lights and other devices without getting up.
"Alexa, turn on the kitchen lights"

Video

Let users find and consume video content. This pre-built model supports content searches and content suggestions.
"Alexa, play Interstellar"

Create Skills



Choose a template

Choose
Select a quick start template to get started with a predefined skill or simply "Start from scratch"

SELECTED

Start from scratch

Design a unique experience for your users and define your custom model from scratch.

Fact Skill

Provided a list of interesting facts about a topic, Alexa will select a fact at random and tell it to the user when the skill is invoked. Includes 1 custom intent, and 4 built-in intents.

Quiz Game Skill

Provided a list of interesting facts about a topic, Alexa will quiz a user with facts from the list. Includes 1 custom intent with 1 slot, and 6 built-in intents.

High-Low Game Skill

Try to guess the target number. Alexa tells the player if the target number is higher or lower than their current guess. Includes 2 custom intents with 5 slots, and 5 built-in intents.

Step 1: Choose Start from scratch

Give your new skill a Name. This is the name that will be shown in the Alexa Skills Store, and the name your users will refer to. Push Next.
Select the Custom model at the top of the page to add to your skill and select the Create Skill button at the top right.

Build the Interaction Model for your skill

a.               On the left hand navigation panel. Select the Invocation tab. Enter a Skill Invocation Name. This is the name that your users will need to say to start your skill.
b.               Next, select the JSON Editor tab. In the textfield provided, replace any existing code with the code provided in the Interaction Model (make sure to pick the model that matches your skill's language), then click "Build Model".
Note: You should notice that Intents and Slot Types will auto populate based on the JSON Interaction Model that you have now applied to your skill. Feel free to explore the changes here, to learn about Intents, Slots, and Utterances open our technical documentation in a new tab.
Optional: Select an intent by expanding the Intents from the left side navigation panel. Add some more sample utterances for your newly generated intents. Think of all the different ways that a user could request to make a specific intent happen. A few examples are provided. Be sure to click Save Model and Build Model after you're done making changes here.

Step 2: Setting Up A Lambda Function Using Amazon Web Services


1.       Go to http://aws.amazon.com and sign in to the console. If you don't already have an account, you will need to create one. If you don't have an AWS account, check out this quick walkthrough for setting it up.
2.    Click "Services" at the top of the screen, and type "Lambda" in the search box. You can also find Lambda in the list of services. It is in the "Compute" section.
Lambda
3.       Check your AWS region. AWS Lambda only works with the Alexa Skills Kit in these regions: US East (N. Virginia), US West (Oregon), Asia Pacific (Tokyo) and EU (Ireland). Make sure you choose the region closest to your customers.

Check Region

4.    Click the orange "Create function" button. It should be near the top of your screen. (If you don't see this button, it is because you haven't created a Lambda function before. Click the blue "Get Started" button near the center of your screen.)
5.       There are three boxes labeled "Author from scratch", "Blueprints" and "Serverless Application Repository". Click the radio button in the box titled "Serverless Application Repository" We have created a repository as a shortcut to getting everything set up for your skill.
6.       Search for the application repository named "alexa-skills-kit-nodejs-factskill". You can find it using the provided search box.
7.       Click on the repository. This repository will create the Lambda function, add Alexa Skills Kit as it's trigger, and sets up an IAM role for you. It will also add the code from this GitHub repo and include it's dependencies to your Lambda function so that you don't have to upload it yourself.
8.       Click the deploy button at the bottom of the page.
9.       Wait for the status of all resources to change to CREATE_COMPLETE
10.   Click the Test App button to go to the Lambda console.
11.   Open the function that was just created by clicking on it.
12.   If you want to secure this Lambda function follow the instructions found here
13.   You should see the Amazon Resource Name (ARN) a unique identifier for this function in the top right corner of the page. Copy the ARN value for this Lambda function for use in the next section of the guide.

Step 3: Connecting Your Voice User Interface To Your Lambda Function

1.    Go back to the Amazon Developer Portal and select your skill from the list. You may still have a browser tab open if you started at the beginning of this tutorial.
2.       Select the Endpoint tab on the left side navigation panel.
3.       Select the "AWS Lambda ARN" option for your endpoint. You have the ability to host your code anywhere that you would like, but for the purposes of simplicity and frugality, we are using AWS Lambda. (Read more about Hosting Your Own Custom Skill Web Service.) With the AWS Free Tier, you get 1,000,000 free requests per month, up to 3.2 million seconds of compute time per month. Learn more at aws.amazon.com/free. In addition, Amazon now offers AWS Promotional Credits for developers who have live Alexa skills that incur costs on AWS related to those skills.
4.    Paste your Lambda's ARN (Amazon Resource Name) into the textbox provided for Default Region.
5.       Click the Save Endpoints button at the top of the main panel.

Step 4: Testing Your Skill

So far, we have created a Voice User Interfacea Lambda function, and connected the two together. Your skill is now ready to test.
1.      Go back to the Amazon Developer Portal and select your skill from the list. You may still have a browser tab open if you started at the beginning of this tutorial.
2.      Open the Test Pane, by selecting the Test link from the top navigation menu.
3.      Enable Testing by activating the Test is enabled for this skill slider. It should be underneath the top navigation menu.
4.      To validate that your skill is working as expected, invoke your skill from the Alexa Simulator. You can either type or click and hold the mic from the input box to use your voice.
a.                       Type "Open" followed by the invocation name you gave your skill in Step 1. For example, "Open City Guide".
b.                      Use your voice by clicking and holding the mic on the side panel and saying "Open" followed by the invocation name you gave your skill.
c.                       If you've forgotten the invocation name for your skill, revisit the Build panel on the top navigation menu and select Invocation from the sidebar to review it.
5.      Ensure your skill works the way that you designed it to.
·         After you interact with the Alexa Simulator, you should see the Skill I/O JSON Input and JSON Output boxes get populated with JSON data. You can also view the Device Log to trace your steps.
·         If it's not working as expected, you can dig into the JSON to see exactly what Alexa is sending and receiving from the endpoint. If something is broken, AWS Lambda offers an additional testing tool to help you troubleshoot your skill.
6.      Configure a test event in AWS Lambda. Now that you are familiar with the request and response boxes in the Service Simulator, it's important for you to know that you can use your requests to directly test your Lambda function every time you update it. To do this:
 .        Enter an utterance in the service simulator, and copy the generated Lambda Request for the next step.
a.       Open your Lambda function in AWS, open the Actions menu, and select "Configure test events."
Configure Test events drop down
b.      Select "Create New Test Event". Choose "Alexa Start Session" as the Event Template from the dropdown list.You can choose any test event in the list, as they are just templated event requests, but using "Alexa Start Session" is an easy one to remember.
Alexa Start Session
c.       Type in an Event Name into the Event Name Dialog box. Delete the contents of the code editor, and paste the Lambda request you copied above into the code editor. The Event Name is only visible to you. Name your test event something descriptive and memorable. For our example, we entered an event name as "startSession". Additionally, by copying and pasting your Lambda Request from the service simulator, you can test different utterances and skill events beyond the pre-populated templates in Lambda.
Copy request
d.      Click the "Create" button. This will save your test event and bring you back to the main configuration for your lambda function.
e.       Click the "Test" button to execute the "startSession" test event.
Test with event
This gives you visibility into four things:
·         Your response, listed in the "Execution Result."
execution result
·         A Summary of the statistics for your request. This includes things like duration, resources, and memory used.
Summary
·         Log output. By effectively using console.log() statements in your Lambda code, you can track what is happening inside your function, and help to figure out what is happening when something goes wrong. You will find the log to be incredibly valuable as you move into more advanced skills.
CloudWatch Logs
·         A link to your CloudWatch logs for this function. This will show you all of the responses and log statements from every user interaction. This is very useful, especially when you are testing your skill from a device with your voice. (It is the "Click here" link in the Log Output description.)
7.      Other testing methods to consider:
·         Echosim.io - a browser-based Alexa skill testing tool that makes it easy to test your skills without carrying a physical device everywhere you go.
·         Unit Testing with Alexa - a modern approach to unit testing your Alexa skills with Postman and Amazon API Gateway.
8.       If your sample skill is working properly, you can now customize your skill.