Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Conversations With Alexa

DZone's Guide to

Building Conversations With Alexa

Alexa skills have a lot of moving parts, but prompting users for more information, establishing a Dialog, doesn't have to be complicated.

· IoT Zone
Free Resource

Cisco IoT makes digital transformation a reality in factories, transportation, and utilities. Learn how to start integrating with Cisco DevNet.

Having an Amazon Echo Dot in my office is quite fun, and I've accidentally started writing more skills and giving a few talks about building skills for the Alexa toolchain. Today, I created a skill that uses multiple steps to make a conversation and thought I'd better write down what I did so I'd be able to remember!

The basic idea is that when creating the "intent", i.e. the action that you want Alexa to do, you also define "slots". The slots are the variables; if this were a command line tool, they'd be the arguments you typed. It's possible to include both intent and slots in your wording when you speak to Alexa, but equally, you can just invoke the skill and have it prompt you for the rest of the information.

Prompting the User for Information

This model of having multiple steps in the exchange between the human and Alexa is what Amazon calls a "Dialog", and to define this, you need to use their new graphical skill builder tool rather than just a JSON structure describing your intents and slots (a bit tedious for those of us who struggle with graphical interfaces). When you add a slot, you can specify the data type and the "prompt", so the words that the device will say when asking the human to supply the information. You can have one or many of these. In the request object that arrives when the skill is invoked, you should also see a dialogState element has appeared.

If this says STARTED, then you can ask Alexa to go ahead and prompt for the information from the user by sending a Dialog.Delegate directive (note that you must not send output speech with this directive, it gets upset). Once all the slots are filled, you'll get another IntentRequest and the dialogState will say COMPLETED. At this point, you should have all the information you'd need to figure out a response and send it back.

Dynamic User Prompts

Sometimes, we don't want to use automatic wording. For example, I have a skill that challenges the user to answer a simple math question — so the user input still needs to go into a slot, but I need to ask a different question every time. For that, we can use the Dialog.ElicitSlot and send some output speech to use as the prompt. Here's the response I send to achieve this:

{
   "outputSpeech":{
      "type":"PlainText",
      "text":"Ready? What is 4 plus 5?"
   },
   "directives":[
      {
         "type":"Dialog.ElicitSlot",
         "slotToElicit":"Number"
      }
   ],
   "shouldEndSession":"false"
}


Using this approach, you can nominate whichever slots up front, and then prompt the user as appropriate, either using predefined wording or using your application to create and prompt. Once I understood how all the moving parts worked, this was fairly simple to get working, so hopefully it helps someone else get quickly started to making something awesome too!

Cisco is a software company. Surprised? Don’t be. Join DevNet to explore APIs, tools, and techniques that developers are using to add collaboration, IoT, security, network priority, and more!

Topics:
iot ,amazon echo ,alexa skills ,dialog ,tutorial

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}