Connect Your Conversational App With JSON Web APIs

DZone 's Guide to

Connect Your Conversational App With JSON Web APIs

Want to bring the versatility of JSON web APIs to your Alexa skill, Google Home action, or Facebook messenger bot? Here's how to do it with Conversation.one.

· AI Zone ·
Free Resource

While the Conversation.one platform provides you with full support to develop your business logic using Node.js/Javascript code, there are cases where you would want to develop it using some other coding language. To support that, Conversation.one offers an easy connection to your own backend server using JSON web APIs calls.

In this example, you’ll build a basic “what happened today” conversational application that will communicate with your backend server to provide responses to user’s requests. A conversational app is essentially the container or project and it contains intents, entities, and the responses you want to deliver to your user. Intents are the mechanisms that pick up what your user is requesting (using entities) and direct the app to respond accordingly.

Conversation.one is a 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

Create a Conversational App

A Conversation.one application represents your conversational interface through Amazon Alexa, Google Home, or Facebook Messenger bot. To create an app:

  1. If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
  2. Click on Create New App in the navigation. If it’s your first application, you will be redirected to the Create New App wizard automatically.
  3. Select your industry and enter your company name. For the purpose of this tutorial, select “Other” for the industry, and enter “What Happened Today” as your company name. Uncheck “Add demo data” to start with an empty application. Click Next to continue.
  4. Select the devices you want to support with your app. For this demo select Messenger, Amazon Alexa, Google Home, and Chatbot. Click Next to continue.
  5. Your new application is ready. Click Customize to start developing your app.

Create an Intent

An intent maps what a user says to what your app does. The intent we will create now will be invoked when the user asks for information on what happened today.

To create an intent:

  1. Create a new intent named “whatHappenedToday”
  2. In the User Says section, enter examples of what you might expect a user to ask for. The more examples you provide, the more ways a user can ask a question and the conversational app will understand. Enter these examples:
    • What happened today in history
    • What happened on this day in the past
    • What took place today
    • What occurred today
  3. Click Save to apply your changes

Create Your Backend Code

For the purpose of this demo, we will explain here how to return a response from PHP backend code. However, you can use any language that you want to return a JSON response.

Add the following PHP code to your server

$day = strtolower(date("d",strtotime($t)));
$month = strtolower(date("m",strtotime($t)));
$curl = curl_init();
$url = sprintf("%s/%s/%s", "http://history.muffinlabs.com/date", $month,$day);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$json = json_decode($result, true);
$selectedEvent = $json['data']['Events'][array_rand($json['data']['Events'],1)];
$res = [];
$res['say'] = 'On this day,'.$selectedEvent['year'].' - '.$selectedEvent['text'];
header('Content-Type: application/json');
echo json_encode($res);

  • 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 inside the “say” attribute

We deployed the code above on Heroku. You can find it here https://conversation-one-demo.herokuapp.com/

Add a Response

  1. In the Response section, select “JSON API”
  2. Enter the URL of your server code, e.g. “https://conversation-one-demo.herokuapp.com/”
  3. 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.”

ai ,alexa skills ,conversational apps ,google home actions ,json web api ,messenger 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 }}