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

Use Entities in Alexa Skills, Google Home Actions, and Chatbots

DZone's Guide to

Use Entities in Alexa Skills, Google Home Actions, and Chatbots

Let's take a look at how you can use entities within Alexa skills and Google Home actions as dynamic parameters for a conversational app.

· IoT Zone
Free Resource

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

In the previous guide, you built a “what happened today” conversational application that can recognize requests from users and serve the dynamic and personalized information the user is requesting using web APIs. In this tutorial, we will use Entities to further enhance our application with the ability get users parameters during the conversation.

Conversation.one is a free build-once-deploy-anywhere platform where your app can be deployed as an Alexa skill, Google Home action, Facebook Messenger bot, or a chatbot on your website. For this tutorial, you will need a Conversation.one account, If you have an account, log in, otherwise sign up for free.

In some places, entities are also called “Slots”, but have exactly the same function.

Entities are powerful tools used for extracting parameter values from natural language inputs. Any important data you want to get from a user’s request will have a corresponding entity. The entities used in a particular application and intent will depend on the parameter values that are needed for actionable data. There are two types of entities: system (defined by Conversation.one), and custom (defined by a developer) entities.

Add a New Entity

We will use entities to differentiate between the two questions: “What happened today?” and “Who was born today?”

  1. Click “Edit Entities”
  2. Enter “Events” in “Create a new type of Entity” and click “Add entry”
  3. Add the values below and click Save
    • what happened
    • who was born

Use the Entity in Intents

  1. Open the “whatHappenedToday” Intent
  2. Delete the previously configured samples from “User Says”
  3. Open Entities
  4. Add a new Entity by entering “eventType” and clicking “Add entry”
  5. Select “Event” as the type for “eventType”
  6. Enter the new samples in “User Says”
    • {eventType} today
    • {eventType} on this day
    • {eventType} on this day in the past
    • {eventType} today in history
  7. Click Save

Read the Entity Value in Your Code

We can read the entity value using the slot method of the request object.

if (req.slot("eventType") == "who was born") {
  // Code for who was born
}
else if (req.slot("eventType") == "what happened") {
  // Code for what happened
}
else {
  // Empty or unknown value 
}


Pay attention that entities can be empty or contain an unknown value, and make sure your code is ready to handle any possible scenario.

Modify the Code Response

The history API that we used in Building a Dynamic Conversational App Using Web APIs returns both Events data, which we used so far, and Births data, which we will use now if the user selected “who was born”.

var backendcontext = sdk.conversationHelper.getBackendContext(context, req, res);
if (!backendcontext) return;
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
return fetch("http://history.muffinlabs.com/date/"+month+"/"+day)
 .then(function(res) {
    return res.json();
 }).then(function(json) {
    var suffix = "";
    var events = json.data.Events;
    if (req.slot("eventType") == "who was born") {
        var events = json.data.Births;
        suffix = ", was born";
    }
    var randomEvent = Math.floor(Math.random() * (events.length));
    var selectedEvent = events[randomEvent];
    res.say("Hi "+ backendcontext.firstname+".");
    res.say('On this day,'+selectedEvent.year+' - '+selectedEvent.text.replace(/(\(.*\))/g,'')+suffix);
 });


If the entity “eventType” value is “who was born”, we will return one of the results from “json.data.Births” and will also modify the returned text by adding “was born”

Try It Out

Now that your app can return dynamic information based on users parameters, try out what you have so far.

In the chat console on the right, type in a request “Who was born today?” After you type the request, hit “Send”.

The chatbot will answer with an interesting person that born on this day in the past. For example “On this day,1853 – John Drew, Jr., American actor, was born.”

Next, In the chat console on the right, type in a request “What happened on this day in the past?”. After you type the request, hit “Send”.

The chatbot will answer with some interesting event from the past. For example,  "On this day,1093 – Battle of Alnwick English victory over the Scots, Malcolm III of Scotland, and his son Edward, are slain.”

What’s Next?

In the next part, you’ll learn how to integrate your application with Zapier.

Build an open IoT platform with Red Hat—keep it flexible with open source software.

Topics:
google home actions ,alexa skills ,chat bots ,tutorial

Published at DZone with permission of Rachel Batish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}