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.
Join the DZone community and get the full member experience.Join For Free
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:
- If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
- 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.
- 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.
- 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.
- 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:
- Create a new intent named “whatHappenedToday”
- 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
- 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
<?php $t=date('d-m-Y'); $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); curl_close($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:
- Find today’s date
- Call the history API using fetch API
- 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
- In the Response section, select “JSON API”
- Enter the URL of your server code, e.g. “https://conversation-one-demo.herokuapp.com/”
- 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.”
Published at DZone with permission of Rachel Batish, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.