In this tutorial we will cover how to build an information search bot - Zummer using Bing Web Search API, Language Understanding Intelligent Services (LUIS)
-
Microsoft Bot Framework to host and publish to multiple platforms. You can download Bot Framework Emulator from here. More details in this documentation article
-
Bing Web Search API to fetch the most relevant Wikipedia article on any given topic.
-
Luis.ai to understand the query intent
Here is a simple flowc of what the Zummer bot logic is:
Zummer bot is trained to understand the following intents:
-
Greeting
-
Article Search by topic
-
Sign in and Create an application on www.luis.ai Note: You can either import the LUIS application JSON file “ZummerLuisApp.json” found in the sample folder
-
Create intent, entities and train LUIS
-
Train your models by clicking “Train”
-
Publish your application
-
Save your published endpoint URL to be used to create a bot on bot framework
-
Create a IntentDialog as in app.js One of the benefits of IntentDialog is that we can bind some functions to LUIS intents and other functions to regex.
const LuisModelUrl = process.env.LUIS_MODEL_URL || 'https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/b550e80a-74ec-4bb4-bcbc-fe35f5b1fce4?subscription-key=a6d628faa2404cd799f2a291245eb135'; // Main dialog with LUIS var recognizer = new builder.LuisRecognizer(LuisModelUrl); var intents = new builder.IntentDialog({ recognizers: [recognizer] })
-
Create functions that are responsible to handel LUIS (Greetings and Search) intents.
var intents = new builder.IntentDialog({ recognizers: [recognizer] }) .matches('Greeting', [ (session) => { session.send(zummerStrings.GreetOnDemand).endDialog(); } ]) .matches('Search', [[(session, args) => { ... }])
-
Create a free tier “Key” that will be used for calling the Bing APIs on Microsoft Cognitive Service subscriptions
-
Bing Web Search API request format details can be found at Bing Web API reference page This tutorial implements communication with Bing Web Search API service and mainpulating the user's query to get response with only Wikipedia articles through "** *** *findsArticles" in bing-search-service.js
var apiHandler = require('./api-handler-service'); const BING_SEARCH_API_URL = "https://api.cognitive.microsoft.com/bing/v7.0/search/", BING_SEARCH_API_KEY = process.env.BING_SEARCH_API_KEY; var headers = { "Ocp-Apim-Subscription-Key": BING_SEARCH_API_KEY } module.exports = { findArticles: (query) => { return apiHandler.getResponse(BING_SEARCH_API_URL, { "q": query + " site:wikipedia.org" }, headers); } }
Note: api-handler-service module is responsible to issue requests for the passed endpoints, code could be found “api-handler-service.js”
-
Anonymous function that is binded to "Search" intent contains
-
Calling “findArticles” function to receive the BingSearch response
-
Fetching first result and extracting information needed using"prepareZummerResult" function, then sending a formatted response to the user
var intents = new builder.IntentDialog({ recognizers: [recognizer] }) .matches('Greeting', [ (session) => {...} ]) .matches('Search', [ (session, args) => { var entityRecognized; var query; if ((entityRecognized = builder.EntityRecognizer.findEntity(args.entities, 'ArticleTopic'))) { query = entityRecognized.entity; } else { query = session.message.text; } bingSearchService.findArticles(query).then((bingSearch) => { session.send(zummerStrings.SearchTopicTypeMessage); var zummerResult = prepareZummerResult(query, bingSearch.webPages.value[0]); var summaryText = util.format("### [%s](%s)\n%s\n\n", zummerResult.title, zummerResult.url, zummerResult.snippet); summaryText += util.format("*%s*", util.format(zummerStrings.PoweredBy, util.format("[Bing™](https://www.bing.com/search/?q=%s site:wikipedia.org)", zummerResult.query))); session.send(summaryText).endDialog(); }); } ])
function prepareZummerResult(query, bingSearchResult) { var myUrl = urlObj.parse(bingSearchResult.url, true); var zummerResult = {}; if (myUrl.host == "www.bing.com" && myUrl.pathname == "/cr") { zummerResult.url = myUrl.query["r"]; } else { zummerResult.url = bingSearchResult.url; } zummerResult.title = bingSearchResult.name; zummerResult.query = query; zummerResult.snippet = bingSearchResult.snippet; return zummerResult; }
-
You will see the following when connecting the Bot to the Emulator:
To get more information about how to get started in Bot Builder for .NET, Bing Web Search API and LUIS please review the following resources: