1. Home
  2. Docs
  3. Tutorials
  4. Building a Dynamic Conversational App Using Web APIs

Building a Dynamic Conversational App Using Web APIs

In the previous guide, you built a basic “what happened today” conversational application that can recognize requests from users. In order to serve the dynamic information the user is requesting, you’ll need to call web APIs.

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.

Add code response

  1. In the Response section, select “From Code” and enter the code:
    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 events = json.data.Events;
         var randomEvent = Math.floor(Math.random() * (events.length));
         var selectedEvent = events[randomEvent];
         res.say('On this day,'+selectedEvent.year+' - '+selectedEvent.text.replace(/(\(.*\))/g,''));
     });

    The code above is running the following steps: 1. Find today’s date 2. Call the history API using fetch API

    3. Select random event and return it using “res.say” command

  2. Click Save to apply your changes

Try it out

Now that your app can return real historical events to the user, try out what you have so far.

In the chat console on the right, type in a request. The request can be a little different than the examples you provided in the User Says section. This can be something like “what popped up today”. After you type the request, hit “Send”.

The chatbot will answer with some interesting event from the past. For example “On this day,1974 – The Rumble in the Jungle boxing match between Muhammad Ali and George Foreman takes place in Kinshasa, Zaire. Ali wins by KO in the eighth round, regaining the title of World Heavyweight Champion and causing Foreman´s first professional defeat.”

What’s next?

In the next part, you’ll enable account linking, to help you provide a personalized response to the user.

Next

Was this article helpful to you? Yes 1 No

How can we help?