Making REST calls using API.ai is an essential part of creating conversational experiences and integrating with various web services. API.ai, now known as Dialogflow, provides a platform for building conversational interfaces for applications and devices. This article will guide you through the process of making REST calls using API.ai in order to fetch data from external web services.
Step 1: Create an API.ai Agent
To start with, you need to create an API.ai agent. An agent is a virtual assistant that understands and responds to user requests. You can define intents, entities, and other conversational elements within your agent to handle user queries and provide appropriate responses.
Step 2: Define Intents and Entities
Intents represent the user’s intention, while entities are the important data within the user’s query. You need to define the intents and entities relevant to your use case. For example, if you want to fetch weather information, you can create an intent named “GetWeather” and entities such as “city” and “date”.
Step 3: Set up the Fulfillment
Once the user’s query is matched with the right intent and entities, you can define a fulfillment to handle the logic of making REST calls. For this, you can use the inline editor provided by Dialogflow or set up a separate web service to handle the fulfillment logic.
Step 4: Make REST Calls
Inside the fulfillment logic, you can use the Node.js or Python client libraries to make REST calls to the external web service. For example, to fetch weather information, you can use the ‘request’ library in Node.js or ‘requests’ library in Python to send HTTP requests to the weather API.
Here’s an example of how the fulfillment logic might look in Node.js:
“`javascript
const request = require(‘request’);
function getWeather(city, date) {
const apiKey = ‘your-api-key’;
const apiUrl = `https://api.weather.com/forecast/${city}/${date}?apikey=${apiKey}`;
return new Promise((resolve, reject) => {
request(apiUrl, (error, response, body) => {
if (!error && response.statusCode == 200) {
const weatherData = JSON.parse(body);
resolve(weatherData);
} else {
reject(error);
}
});
});
}
function weatherIntentHandler(agent) {
const city = agent.parameters[‘city’];
const date = agent.parameters[‘date’];
return getWeather(city, date)
.then((weatherData) => {
agent.add(`The weather in ${city} on ${date} is ${weatherData.weather}.`);
})
.catch((error) => {
agent.add(‘Sorry, I could not fetch the weather information at the moment.’);
});
}
“`
Step 5: Handle the Response
Once you receive the data from the external web service, you can format it and send it back as a response to the user’s query. In the above example, the weather information is added to the agent’s response using `agent.add()`.
In summary, making REST calls using API.ai involves creating an agent, defining intents and entities, setting up the fulfillment logic, making REST calls to external web services, and handling the response. This allows you to create conversational experiences that can fetch and present data from various sources, enhancing the user experience with intelligent, interactive responses.