Please enable Javascript for better experience...
Build an Echo Bot and integrate with Microsoft QnA Maker service using VS2019 and C#
By Rahul Kumar Jha | May 25, 2020 | In Articles | Update: Jun 23, 2020 | Total Views [ 4078 ]
Taged In
(3 Like)
Rate

In this article we will learn working with bot framework, create a simple echo bot, setup QnA service using MS QnaMaker (https://www.qnamaker.ai) and integrate QnA maker with bot application created locally. Finally, we will test our bot using bot emulator.

Introduction

In this article, you will learn how to create simple echo bot, configuring Microsoft QnA maker and integrate it with our bot application.

Prerquisite

Before starting, take care of below prerequisite.

  • Install Visual Studio 2019
  • Add Bot extension to VS2019
  • Install Bot framework emulator

Adding bot extension

Go to Visual Studio > Open Extensions > Manage Extensions

00

A popup opens. Search bot and install

01

Installing Bot Emulator

Download Emulator from below link and install.

https://github.com/Microsoft/BotFramework-Emulator/releases

Once the bot emulator is installed, it looks like this.

02

Creating Echo bot

Echo bot is a simple bot that can communicate with user. To create an Echo bot follow below steps.

Open Visual Studio and create a project.

In search box type bot. You will see several project templates. Select Echo bot template. You can select empty project template as well but in that case, you have to configure everything from scratch. I have selected Echo Bot template and named the project as TestEchoBot.

03

Once project is added, you could see below files.

default.htm page which is landing page for this project

EchoBot.cs which is inherited from ActivityHandler class that inherits from IBot interface. This class override two methods 1) “OnMessageActivityAsync” which has logic related to bot conversation with end users, 2) “OnMembersAddedAsync” which is used to welcome a new member to chat.

BotController.cs – Is a controller for running bot

Appsettings.json – It contains bot related settings. You can run bot locally but if you want to make your bot public, you have register it and provide Microsoft app id and password.

{

  "MicrosoftAppId": "",

  "MicrosoftAppPassword": ""

}

04

Testing bot with Emulator

Now, as our simple bot is ready, it is time to test it using emulator locally. For that start bot emulator and add configuration. Run your bot project and copy URL. Now open File menu > New Bot Configuration… option. Add bot details like name and Endpoint URL (project URL). Microsoft app id and password are optional which can be added later and are not required if running bot locally. Click on “Save and Connect” button to start bot.

05

Bot is connected and ready to serve. As this is dummy app, bot will return what user is entering. You have to change the logic and add more functionality if you want bot to return proper response.

06

Setting up QnA Maker

QnA maker is a tool that enable bot to answer questions asked by user depending on the Knowledge base added into QnA maker. It is facilitate with AI and model is trained on Azure. To implement QnA maker, you have to first register on https://www.qnamaker.ai/. Once you are registered on QnA maker portal, first thing is to add knowledge base. You dashboard should look like this.

07

Next step is to create knowledge base. Screen look like below.

08

First step is to create a QnA resource in Azure.

09

Create QnA service in Azure portal

For that, you have to fill below details, additionally you have to create a resource in Azure portal and must have a subscription (free or paid).

To start with, first create a resource group.

Go to Azure portal > Click on Resource Group

10

Below page opens up. Just add required fields and click on “Review + Create”. This will validate the request. After validation is completed, press “Create” button to create resource.

11

This will take some time to deploy. You can see your resource on resource dashboard page.

12

Now configure QnAMaker at this URL.

https://portal.azure.com/#create/Microsoft.CognitiveServicesQnAMaker

13

After QnA is created, go to previous page and press “Refresh” button.

14

You can verify the same in azure portal as well that whether QnA resources has been created or not.

15

Now in step 2, select QnA service details created in step 1.

16

In step 3, enter name of Knowledge Base.

17

In Step 4, you will find option to upload KB document, which can in the form of file or URL. I am uploading excel file with dummy data.

18

You can also select Chit-Chat option having predefined trained data which can be used for greetings and general chat.

19

Now click on “Create your KB” button to Add KB. It may take some time to train the model based on your data.

20

Testing QnA KB online

Your data look like below. Here you will find options to add phrasing data as well which act like keywords. If you are satisfied, you can test data using “Test” button.

21

So here you can see that bot is working perfectly. It has chit-chat message and it can answer to several questions as well which were added as data.

22

Publish QnA maker KnowledgeBase

After you tested your QnA maker KB, It’s time to publish it so as to make it public for further integrate into app or other medium. For that click on “Publish” link and then click “Publish” button. It will take some time to publish you QnA service.

23

After publish is completed, you will get basic details like endpoint key and host to be used further. Now next step is to create a bot.

24

Integrate QnA Service in your desktop bot application

To start with, open bot application created earlier, click on project and open Nuget Package Manager. Install “Microsoft.Bot.Builder.AI.QnA” package.

25

Now open Startup.cs file and add below code in “ConfigureServices” method and use this reference. In “ConfigureSErvices” method either you can directly add values like host, endpoint and knoledgeBaseID or you can copy data in “appsettings.json” file and call here via “Configuration.GetValues() method.

using Microsoft.Bot.Builder.AI.QnA;

35

26

If you are getting error while getting attributes from appsettings.json file (Configuration.GetValue<string>(""), then install Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.Binder package as well. Now copy below code in appsettings.json file.

27

You can find these settings in QnAMaker portal under setting tab. Open QnAMaker.AI > Settings > Deployment

28

Now go to EchoBot.cs file and add below code.

Add reference: using Microsoft.Bot.Builder.AI.QnA;

Modify method “OnMessageActivityAsync” and replace with below code.

29

Add method “GetAnswerFromQnAMaker”. This method is used to communicate with QnAMaker service and return result based on the response from QnA maker. Then add a property “EchoBotQnA” and define constructor to set value to this property from “ConfigureServices” that is inside Startup.cs file.

30

Now code part is completed, so it is time to test the bot with new QnA service integrated so far. Run the application and open Emulator to test the bot. Configuration is already done, so it should find the existing one. Just click on “TestEchoBot” under My Bots section and it will open a new session for you.

31

As soon as the session starts, first it will give welcome message which was defined in “OnMembersAddedAsync” method in EchoBot.cs file. Now it’s time to say “hi” to bot. If you remember we configured “Chit-Chat” dataset. This is pre-defined dataset created for the mean of basic greetings chat. So till here your QnA FAQ does not come into the picture. Bot will simply return greetings. 

32

Now it’s time to ask something which we configured in QnA. Let’s ask question “Who are you”. QnA read the question and search in dataset we created and returns best matching answer. I have randomly asked from questions from bot and here are the answers. 

33

I think we are done for today. In this article you learn A-Z about bot with simple example and later we learned configuring and using QnA maker into the bot.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors