In Chapter 6, we designed a simple chatbot framework in Java which we called the IRIS (Intent Recognition and Information Service) framework. We discussed the core components of IRIS, such as how to define intents and how state machine can be implemented for defining state and transitions for building a conversational chatbot. An example use case focused on the insurance domain. In the example, we outlined specific capabilities that IRIS is supposed to perform such as providing market trend details, stock price information, weather details, and claim status.
In this chapter, we will focus on the integration modules of IRIS, which shows how we can connect with external data sources and third-party APIs for information retrieval.
Integration with Third-Party APIs
Market trends
Stock prices
Weather information
Market Trends
Stock time series data
Physical and digital/crypto currencies (e.g., Bitcoin)
Technical indicators
Sector performances
All APIs are in real time: the latest data points are derived from the current trading day.
Upon providing just three necessary details, which are the type of user, institution/organization name, and an email address, we get an API key, and it is free for a lifetime as per the website. The free API key can be obtained by providing details at www.alphavantage.co/support/#api-key .
Stock time series
Forex
Technical indicators
Cryptocurrencies
Sector performances
More details on each of these APIs can be found at www.alphavantage.co/documentation/ .
For our example use case, we want to know how to get the current market trend and stock price of a particular stock. For the current market trend, we leverage the Sector Performances API, details of which are available at www.alphavantage.co/documentation/#sector-information .
A sample HTTP GET request to obtain real-time, sector-wise performance details is at www.alphavantage.co/query?function=SECTOR&apikey=demo .
Stock Prices
What is the current stock price of microsoft
Pru stock price
Infy stock today
Share price of hdfc
When the utterance is received by IRIS Core, it passes the utterance to the intent classification engine, which knows that the user is looking for 'STOCK_PRICE'. Based on this intent and current state, a transition to StockPriceState happens. The execute method of this state then makes a call to the third-party API.
To retrieve the stock price details, we use a TIME_SERIES_DAILY API from the Stock time series API suite of Alpha Vantage.
A sample HTTP GET request:
www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
Weather Information
There are plenty of digital bots available that provide weather details. People often ask Siri, Google voice assistant, and Alexa to give details on the weather. Let’s see how we can use a third-party API to integrate with IRIS for weather information.
To get the weather report, we leverage http://openweathermap.org , which provides the API to get weather details of the requested city. It offers multiple data points such as current weather data, 5-day forecast, 16-day forecast, and other historical information about the city. It currently includes weather details for over 200,000 cities around the world. The current weather is frequently updated based on global models and data from more than 40,000 weather stations. OpenWeather also provides APIs for relief maps, managing personal weather stations, bulk downloading, weather alerting, UV index, and air pollution.
For our example, we need the current weather in a given city. We need to obtain an API key. OpenWeather provides multiple API plans, the details of which can be found at https://openweathermap.org/price .
- Call the current weather data for one location:
By city name
By city ID
By geographic coordinates
By ZIP code
- Call the current weather data for several cities:
Cities within a rectangle zone
Cities in cycle
Call for several city IDs
A sample HTTP GET request when querying by city name (passed in q): http://api.openweathermap.org/data/2.5/weather?appid=YOUR_APP_ID&q=dublin
Connecting to an Enterprise Data Store
In our example, we use GetClaimStatus to demonstrate how to connect to a database and query claims information.
Integration Module
Many open source tools provide simple customized integrations with various channels without us having to write a lot of code, and they take care of a lot of complexity required for the integrations. In this illustration, we focus on bootkit, a leading development tool providing customized combinations with multiple messaging platforms:
https://github.com/howdyai/botkit .
A Facebook page: A Facebook page is used as the identity of your bot. When people chat with your app, they see the page name and profile picture.
A Facebook developer account: Your developer account is required to create new apps, which are the core of any Facebook integration. You can create a new developer account by going to Facebook for Developers and clicking the Get Started button.
Facebook App for Web: The Facebook app contains the settings for your Messenger bot, including access tokens.
A webhook URL: Actions that take place in conversations with your bot, such as new messages, are sent as events to your webhook. This is the URL of our integration module, which we cover next.
The setup process requires adding the Messenger platform to your Facebook app, configuring the webhook of the app, and subscribing your app to the Facebook page. The details on setting up the Facebook app can be found at https://developers.facebook.com/docs/messenger-platform/getting-started/app-setup/ .
As described above, setting up the webhook requires an HTTPS callback URL. This URL is the API endpoint of our integration module, which will receive messages from Messenger. We create a NodeJS application for this purpose because it is the requirement for setting up the webhook.
More details on setting up are described in Facebook developer page at
https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup .
- 1.Create an HTTP Server (server.js):// Import modules required in server.jsvar express = require('express');var bodyParser = require('body-parser');var https = require('https');var http = require('http');var fs = require('fs');var localtunnel = require('localtunnel');// Custom JavaScriptsvar conf = require(__dirname + '/conf.js');function server(ops) {// Create App/* Express is a popular Node web framework and provides a mechanism to write handlers.*/var app = express();/* body-parser is Node.js middleware that parses incoming request bodies in a middleware before handlers.*/// parse JSONapp.use(bodyParser.json());//Returns middleware that only parses urlencoded bodiesapp.use(bodyParser.urlencoded({//This object will contain key-value pairs, where the value can be a string or array (when extended is false), or any type (when extended is true).extended: true}));// Path to static filesapp.use(express.static(__dirname + conf.static_dir));/* Declare option and create a HTTPS server by reading SSL key and SSL cert path from config file .*/var options = {port : conf.securePort,key : fs.readFileSync(conf.sslKeyPath),cert : fs.readFileSync(conf.sslCertPath),requestCert : false,rejectUnauthorized : false};https.createServer( options, app).listen(conf.securePort, conf.hostname, function() {console.log('** Starting secure webserver on port ' + conf.securePort);});http.createServer(app).listen(conf.port, conf.hostname, function() {console.log('** Starting webserver on port ' + conf.port);});/*localtunnel exposes localhost to the world for easy testing and sharing. It will connect to the tunnel server, set up the tunnel, and tell you what URL to use for your testing. We used localtunnel to get an https endpoint on http://localhost:9080/respond to test with Facebook Messenger .*/if(ops.lt) {var tunnel = localtunnel(conf.port, {subdomain: 'askiris'}, function(err, tunnel) {if (err) {console.log(err);process.exit();}console.log("Your bot is available on the web at the following URL: " + tunnel.url + '/facebook/receive');});tunnel.on('close', function() {console.log("Your bot is no longer available on the web at the local tunnel.me URL.");process.exit();});}return app;}/* module.exports is an object that the current module returns when it is "required" in another program or module*/module.exports = server;
- 2.Add the Facebook webhook endpoints (Webhooks.js):const fetch = require("node-fetch");// This is the IRIS API URL endpointconst url = "http://localhost:9080/respond";function webhooks(controller){/* This is the initial message a user sees before interacting with Iris for THE first time .*/controller.api.messenger_profile.greeting('Hi, my name is IRIS. I am continuously training to become your Digital Virtual Assistant's.');// All messages will be sent to the API.controller.hears(['.*'], 'message_received,facebook_postback', function(bot, message) {// Facebook request message contains text, senderID, seq, and timestamp.var params = {message: message.text,sender: message.sender.id,seq: message.seq,timestamp: message. timestamp};var esc = encodeURIComponent;var query = Object.keys(params).map(k => esc(k) + '=' + esc(params[k])).join('&');/* fetch makes a HTTP GET call and receives a response and passes the 'message' back to Facebook .*/fetch(url +query).then(response => {response.json().then(json => {bot.reply(message, json.message);});}).catch(error => {bot.reply(message, "");});});}module.exports = webhooks;
- 3.
Add webhook verification. More details on specific of botkit-facebook integration are explained in detail at www.botkit.ai/docs/readme-facebook.html .
Once we are all done with the above and have tested the endpoints, we are all set to start interacting with IRIS on Facebook Messenger.
Demonstration of AskIris Chatbot in Facebook Messenger
Account Balance
Claim Status
Identification of intent as well as slot required for intent fulfilment in one utterance. In example 1 below, intent (CLAIM_STATUS) and claim ID (gi123) are obtained at the same time.
This demonstrates the potential of natural language processing to analyze natural user utterances, including spelling mistakes.
Also, it shows the handling of variations in which the user asks for the same information. In example 1, we have natural language-based inference of intent and slot. In example 2, since the slot is not obtained, IRIS prompts for this information just like any other typical conversation. See Figure 8-10.
Weather Today
What’s the weather in Letterkenny today
Dublin weather today
Weather in Ranchi now
Frequently Asked Questions
Context Switch Example
In Figure 8-13, the user starts by asking for a life insurance quote. IRIS prompts for age, smoker info, height, and weight. The user provides all other information as expected except for weight. Instead of providing weight, user asks for a stock price. Instead of replying to the user that they have not entered the weight correctly or that their weight was not recognized, IRIS understands the switch in context and hence the switch in intent and seamlessly provides the requested detail. Later, when the user asks for life insurance again, notice that the already answered questions are not asked again. This is done because of short term memory in which these details were stored.
Summary
In this chapter, we showed how to extend the functionalities of IRIS to support integration with various third-party services. Connecting with an enterprise database to fetch user data was also explained. We discussed the integration module through which IRIS is exposed to the outside world. We implemented integration of IRIS with Facebook Messenger and followed the step-by-step process required for a successful integration.
Finally, we illustrated what interaction with Facebook Messenger and IRIS looks like. We showed a few use cases via examples and explained behind the scenes what was discussed in previous chapters.
In the next chapter, we will discuss deploying this in-house framework to the cloud. We will explore various ways in which we can deploy our services to AWS. We will also discuss how IRIS can be integrated with Alexa in less than 5 minutes by going through a step-by-step process. We will conclude by discussing how this framework can be improved and the scope for further enhancements such as the implementation of a feedback loop.