Programming Amazon Alexa

Story Telling using ALEXA Device.

💁This article is intended to explain the complete process of creating a custom Alexa skill which can be added to your device.

✋Note that you don’t need any physical device to build and test the application.

🚥 Accounts needed: Developer Portal, AWS Console, Echosim, Alexa Online

  • Login to https://developer.amazon.com/home.html
  • Move to the Alexa skill kit — 3rd tab.
  • Click on the “Create Skill” button
  • Give your skill a name. We’ll call ours PowerfulStories.
  • Choose the Custom Model
  • Click on the Create Skill Button in the top right corner.
  • This brings you to a page where we can move through the Build, Test, Distribution, Certification, and Analytics steps of creating an Alexa skill. The goal of this article is to progress through building and testing the skill.
  • In the Build page, let’s choose a template. Select “Start from scratch” and click on the “Choose” button in the top right corner.

The pointers in the above screen shot indicate many action items that require our attention. Most important among them is the item outlined in light blue → Intents, Samples, Utterances, Slots.

🙋 Invocation Name: Click on the first item in the pane on the right to update the invocation name. This is the name that we use to invoke Alexa and starts the interaction with your new custom skill. This is not required to be unique across all Alexa applications. Choose a couple of words which will make a good name for the application. Again, ours is “Powerful Stories.” So, to begin using our new Alexa skill, we would say “Alexa, open Powerful Stories.” Save the model by clicking on the “Save Model” button in the top left corner of the same pane. Once it is saved, click on the Build tab. You may immediately see that the build failed with a valid reason. That’s okay at this point.

🙋 Intents, Samples, and Slots: Now we are entering into the most important section for Alexa. Understanding this section will give a complete picture of the Alexa Model. Click on this and stay focused.

  • Create a custom Intent. Our interactions with Alexa will be fulfilled by Intents. Also, here we will specify what the user will say in order to invoke our custom intent.
  • Imagine the skill as a client/server application, where Alexa is acting as the client. When a user says something, Alexa has the responsibility of invoking an intent which matches the words spoken by the user. This intent invocation sends a request to a server side program (a lambda function) that has business logic associated with the intent triggered by the user. So, the intent you are creating on this UI must match with the same word on the server side so that it can be handled properly.
  • On the client-side, an intent will be composed of a set of utterances. Utterances are the possible phrases that we might say to convey this intent to Alexa. So, enter “ReadStory” for the name of our custom intent and create it.

💁Now the relation is as follows:

🙎(User) → says invocation (Alexa, open Powerful Stories) → 👽Alexa opens the skill, begins listening for the user’s intent → User then gives his intent by saying one of several utterances → Alexa determines intent based on the utterance it hears → invokes the server side code (read further) → Response returns, and is read by Alexa.

  • Now, it’s time to create utterances. You can enter one or multiple utterances, any of which can be used to interact with the device. Programmatically, what might a user say to invoke this intent? The utterances screen has many controls, as below.
Most of the controls are self explanatory — must know the meaning of all rounded once.
Example Utterances — words representing ancient moral stories from India for future generations

Every line above represents an utterance. You may notice that the utterances all follow a similar pattern, “Read _ Stories.” Creating utterances for each type of story can be tedious and may result in a set of disorganized utterances. Instead, we can create a single utterance with a parameter inside it, like the utterance below.

Alexa, Read a ${} Story.

So, Time for slots. Slots are the parameters/variables contained within an utterance. The value of this slot will be passed as a parameter to an intent which will be invoked to run on the server. Create a custom slot type, as below.

Enter Slot Values

After adding/removing multiple slot values this is how the screen looks like.

⏱ Don’t forget to save the model when you finish a change or action.

Below is an example of creating/updating a slot inside an utterance that can dynamically supplied to an intent.

Slot and Its type. Type contain all slot values. Observe the supplied slot to an utterance.

A slot and its type. The slot type contains all possible slot values. Notice how the slot is supplied to an utterance.

Three Arrows:

  1. TypeInput is the slot name which is supplied to the utterance.
  2. 1. The slot type here is StoryTypes, which includes a list of story types as its possible values. Using this slot, a user can supply multiple dynamic values instead of creating multiple utterances. Wondering how we created this type? On the left-hand side under intents you can find the Slot Types menu option — click on “Add” to create one.

3. So, we can now delete those utterances that can be handled through a slot from our list of sample utterances.

It looks much simpler, now. Time to 💾 ⏱ (Save and Build) 😃

👌Recap:

So far, we have done the following:

  1. Created our custom intent
  2. Created a new slot type
  3. Added the possible values for that slot type
  4. Created utterances, one of which uses the slot with our type as a parameter

That’s it! We are almost ready to interact with the device now, with only one missing piece.

When the intent is invoked, we need something at runtime to run the business logic. You’ll need to either build a web application yourself, or you could take advantage of AWS’s platform for serverless computing, AWS Lambda. Lambda allows you to run code without provisioning or managing any servers, requiring little-to-no administrative work. For this Alexa skill, Lambda will execute the code related to the intent that we just created. In this case, our business logic can send the content of a story, chosen based on the slot value provided in the utterance spoken by the user to invoke our intent. Lambda will execute this code in response to the intent without concerning you, the developer, about the infrastructure which runs the program.

Hopefully, at this point you have been reassured that Lambda will be able to handle the server-side code for your intent. You can choose multiple languages to write your lambda. For ease and simplicity, let’s choose a Node.js service. We will get back to the lambda shortly. Let’s first finish the client (skill) configurations.

So, coming back to our Build page you should see that the first three sections are now complete, upon building. Now, it’s time to define the endpoint.

Clicking on the fourth section will take you to the page below:

Choosing the Lambda option will ask you to enter the ARN of your lambda, which we haven’t created yet. Here is where we will initiate the handshake from client to server.

Now we need to go back and finish our lambda. To do this, we need to go to the AWS Console from the developer portal.

Click “Create function” to create a new function.

💁Choose “Blueprints.”

Add a filter for Alexa and choose the “factskill” (alexa-skill-kit-sdk-factskill).
Supply the basic information and create a function

Now time to edit the generated Code: First thing first:

const APP_ID = undefined; ← On the Service Endpoint Type screen from the developer portal, you can copy the the Skill ID to your clipboard. Click “Copy to Clipboard” and replace “undefined” with the Skill ID that you copied.

Copy to Clipboard

The purpose of this generated code is to return a random fact out of a list. We need to rewrite this for storytelling. We will replace every fact with a story for now, but we will enhance this in upcoming articles.

🎵 Don’t forget that the intent must be invoked by saying one of the utterances we created. Here is the code that was generated/written for the lambda.

The below code represents the complete Lambda function. Copy the ARN and paste into the “Default Region” field on the Service Endpoint page.

Enable your skill at here: https://alexa.amazon.com/

⏲ Time to test our new skill:

Go to https://www.echosim.io/ and login with your developer account. Click on the 🎤 icon in the screen below and say “Alexa, open PowerfulStories”. That’s it, you can now start interacting with your skill, declaring your intent through the utterances that you created.

The last thing that you may be missing is the mapping between the lambda and the skill. That mapping happens on the page below:

When you add the Alexa Skills Kit to your lambda for the first time, you must provide the correct Skill ID. Use the “Copy to Clipboard” button from the End Point Service instead of copying by hand, paste the ID into the proper field, and save it.

Thanks to James Land for reviewing the content and making it simple to understand.

Thanks to my daughters Greeshma & Geetika Krishna Kancharla (6 & 5 years old) for making the colorful image annotations while I am working on the content.

--

--