Creating Simple FAQ Chatbot using QnA Maker API

Chatbot could be utilized to automate business-to-client interactions such as for creating customer service application. Not every task could be handled by the bot though, but it could handle a lot of tasks before some of the more complex ones are delegated to human. However, a lot of heavy-lifting is required to create the AI the bot, such as processing the input of the users, implementing the language understanding, training the machine, testing it, etc. Sometimes, we just want a simple bot that answers to frequently asked questions (FAQs). But different people ask differently, right? And maybe we are wondering how are we able to make our bot understand different questions that might have the same meaning and context. For this scenario, we could make use of QnA Maker API. With QnA Maker API we can build, train, and publish a simple question and answer bot based on FAQ URLs, structured documents or editorial content in minutes.

This tutorial will show you the step-by-step tutorial on how to create a chatbot of McDelivery Service FAQs. Note that this chatbot service doesn't handle the actual business process for ordering a food. That will be saved for another post...

I do not represent McDelivery or McDonald's entity in any way and this tutorial is created just for learning purpose.

Step 1: Create a QnA Service

Navigate to https://qnamaker.ai and then log in using your Microsoft Account. Then, click "Create new service" button to create a new QnA Service. After that, you will need to specify your service name.

You have two initial options to populate your knowledge base either by URLs or by files. For this tutorial, I'll be using the URL option to populate my knowledge base. I have already found a McDelivery FAQs website containing all of the questions are answers (https://www.mcdelivery.co.id/id/support-faq.html?locale=en) so I just need to paste it down into the URL column.

 

Step 2: Review & Publish The Knowledge Base

After you submit the newly created QnA Service, it will automatically crawl the website and extract any questions and answers from it.

You can review your knowledge base at this moment. If you are satisfied with your current knowledge base, click "Publish" to publish your knowledge base on the internet.

 

 

Step 3: Retrieve Your HTTP Service Endpoint

After publishing your knowledge base you will get a sample HTTP request along with its HTTP headers and body information. Save this information, because we will use this for later.

Step 4: Create Bot Application Project in Visual Studio

Now the creating the bot app part, you need to open Visual Studio 2017 and create a new project using Bot Application Project Template.

Step 5: Modify RootDialog.cs

Bot application project is similar to an ASP.NET MVC project where it contains a MessageController.cs as the controller for the message input will be handled. The MessageController act as an entry point of the bot application where then it will pass the message of the user to the RootDialog of the application. Dialogs are the conversation model and state and could be managed by the developers. A bot service will have at least one dialog (which is RootDialog). I will not explain the details, but you could learn more about MessageController and dialogs from the following link.

For this demo, we just need to make small code changes from this...

[Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;

            // calculate something for us to return
            int length = (activity.Text ?? string.Empty).Length;

            // return our reply to the user
            await context.PostAsync($"You sent {activity.Text} which was {length} characters");

            context.Wait(MessageReceivedAsync);
        }
    }

To this...

[Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;
            var text = (activity.Text ?? string.Empty);
            var url =
                "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/0788fe21-02a8-408d-a4e7-9eec59a44432/generateAnswer";
            var httpContent = new StringContent("{'question':'" + text + "'}", Encoding.UTF8, "application/json");

            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "a34858b21b34413f85553656670dfbf7");
            var httpResponse = await httpClient.PostAsync(url, httpContent);
            var httpResponseMessage = await httpResponse.Content.ReadAsStringAsync();
            dynamic httpResponseJson = JsonConvert.DeserializeObject(httpResponseMessage);
            var replyMessage = (string) httpResponseJson.answers[0].answer;
            await context.PostAsync(replyMessage);

            context.Wait(MessageReceivedAsync);
        }
    }

Note that if you encounter any problem while running the program, try rebuilding the project to restore any missing packages. This project requires Microsoft.Bot.Builder and Newtonsoft.Json NuGet packages in order to run successfully.

Step 6: Deploy & Run The Bot Application

In order to see the result we just need to deploy the bot application. You could deploy your bot application to localhost and run it using Bot Framework Emulator or you can deploy it directly to the cloud for example by using Azure Bot Service.

Here is the final result of the chatbot app...

You could try asking following questions:

  1. Is McDelivery available 24-hours a day, 7 days a week?
  2. Is there a minimum order value for delivery?
  3. How can I use a promotional coupon?
  4. Can I choose my Happy Meal toy through online order?
  5. How do I check the status of my order?
  6. I am new to McDelivery. How do I set up my account?
  7. Can I cancel my order?
  8. How can I change/add more item to my previous order?
  9. Is there a delivery surcharge?
  10. Do you offer the same menu options for McDelivery as in the restaurants?

Or you could even try to ask the same question but with a little different in word or sentence structure. See if QnA Maker API engine could figure that out.

List of complete questions: https://www.mcdelivery.co.id/id/support-faq.html?locale=en

References