1. Home
  2. Docs
  3. Tutorials
  4. Collect information from your user

Collect information from your user

In the previous guide, you built a contextual “what happened today” conversational application. In this tutorial, we will learn how to collect missing information from your user in your application; Alexa skill, Google Home action or your chatbot.

Some of the information in this guide is more developer-centric, but don’t be discouraged. If you follow the steps as they’re outlined, you will have a deployed, working “what happened today” app.

User information in conversational applications is collected using entities. For this tutorial, you will need to cover first the how to use entities to extract parameter values from natural language inputs tutorial.

Requesting and collect missing information

To request missing information we will use the method sdk.conversationHelper.askForEntity. We will collect the information later using a dedicated intent that calls the method sdk.conversationHelper.FillEntity. Conversation.oen will automatically store and handle the context for you.

Add new Entities

Instead of providing events that happened only on this day, we will collect the required month and day from the user. We will use two entities, one for the month and one for the day.

  1. Click “Edit Entities”
  2. Enter “Month” in “Create a new type of Entity” and click “Add entry”
  3. Add the values below and click Save
    • January
    • February
    • March
    • April
    • May
    • June
    • July
    • August
    • September
    • October
    • November
    • December

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 “Month” and clicking “Add entry”
  5. Select “Month” as the type for “Month”
  6. Add a new Entity by entering “Day” and clicking “Add entry”
  7. Select “Number” as the type for “Day”
  8. Enter the new samples in “User Says”
    • {eventType} in history
    • {eventType} in the past
    • {eventType} on {Month} {Day}
    • {eventType} on {Month} {Day}
    • {eventType} on {Day}
  9. Click Save

Detect and request Missing Information

The code below, check if the entity “Month” has an empty value, and if needed, requests the user to provide the month.

if (!req.slot("Month")) {
  return sdk.conversationHelper.askForEntity(context, req, res, [
    [{
      "name": "Month",
      "target": "Month"
    }]
  ], "On which month?", "For example you can say, On February.");
}

Collect Missing Information

We will need to create a new intent for each entity we would like to collect.

  1. Create a new intent named “EnterMonth”
  2. Open Entities
  3. Add a new Entity by entering “Month” and clicking “Add entry”
  4. Select “Month” as the type for “Month”
  5. Enter the new samples in “User Says”
    • {Month}
    • On {Month}
  6. Create a new Script response with the code
    return sdk.conversationHelper.FillEntity([{
            "name": "Month"
        }], context, req, res);
  7. Create a new intent named “EnterDay”
  8. Open Entities
  9. Add a new Entity by entering “Day” and clicking “Add entry”
  10. Select “Number” as the type for “Day”
  11. Enter the new samples in “User Says”
    • {Day}
    • On {Day}
  12. Create a new Script response with the code
    return sdk.conversationHelper.FillEntity([{
            "name": "Day"
        }], context, req, res);
  13. Click Save to apply your changes

Modify the code response

Edit the code response of the “whatHappenedToday” intent to the code:

var backendcontext = sdk.conversationHelper.getBackendContext(context, req, res);
if (!backendcontext) return;

if (!req.slot("Month")) {
  return sdk.conversationHelper.askForEntity(context, req, res, [
    [{
      "name": "Month",
      "target": "Month"
    }]
  ], "On which month?", "For example you can say, On February.");
}

if (!req.slot("Day")) {
  return sdk.conversationHelper.askForEntity(context, req, res, [
    [{
      "name": "Day",
      "target": "Day"
    }]
  ], "On which day?", "For example you can say, twenty four.");
}
  
var day = parseInt(req.slot("Day"));
var month = new Date(Date.parse(req.slot("Month") + " 1, 2012")).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 selectedEvent = events[0];
    res.say("Hi "+ backendcontext.firstname+".");
    res.say('On '+ json.date + ',' + selectedEvent.year +' - '+selectedEvent.text.replace(/(\(.*\))/g,'')+suffix);
  if (events.length > 1) {
        req.getSession().set('onYes', JSON.stringify({
            callback: 'whatHappenedTodayOnYes',
            data: {
                lastIndex: 0,
                suffix:suffix,
                events: events,
                date:json.date
            }
        }));
        req.getSession().set('onNo', JSON.stringify({
            callback: 'whatHappenedTodayOnNo',
            data: {}
        }));
        res.say(" Would you like to hear more?");
    } else {
        sdk.conversationHelper.addAnythingElseMessage(res);
    }
 });

Lines 4 to 20, make sure we have all the needed information to provide an answer. In this case, it’s both Month and Day. If one of the is missing, we will ask the user to provide the information.

Try it out

Now that your app can collect missing information, try out what you have so far.

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

The chatbot will ask you to provide a month. In the chat console on the right, type in a month e.g. “July”. After you type the request, hit “Send”.

The chatbot will ask you to provide a day. In the chat console on the right, type in a day e.g. “28”. After you type the request, hit “Send”.

The chatbot will answer with an interesting event from the selected date.

Was this article helpful to you? Yes No 1

How can we help?