In previous article, I have explained how to educate QnA maker for questions and answers. In this article we will discuss about how to consume QnA maker service in Bot application created using Microsoft Bot Framework.
I am going to build a bot which will answer FAQs related to LUIS with the help of QnA maker service we have created in previous article.
Prerequisite
- Download Visual Studio 2015/2017 and update all extensions.
- Download and install bot application template from here.
- Download Bot Framework Emulator. The emulator is a desktop application that lets you test a bot application on localhost or running remotely.
Create bot application
Open Visual Studio and create a new C# project. Choose the Bot Application template. If required, also update NuGet Packages installed in project.


Template contains all components required to build a bot as well as initial code setup.
Code
In solution explorer, you will see WebApiConfig.cs file, Controllers and Dialogs folders. 'MessageController' is inherited from 'System.Web.Http.ApiController'. Bot application is nothing but webapis which will be called from chat window to get response.

When user is going to enter query in chat window, 'post( )' method within controller will get called. Then controller will invoke specified dialog, which will process the query and return the result.
For our chat bot, I have added AnswerDialog.cs (see image above) to process questions from user. You can pull code from GitHub repository.
MessageController.cs
Updated code for invoking AnswerDialog instead of default RootDialog.
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- if (activity.Type == ActivityTypes.Message)
- {
- await Conversation.SendAsync(activity, () => new Dialogs.AnswerDialog());
- }
- else
- {
- HandleSystemMessage(activity);
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
AnswerDialog.cs
- public class QnAMakerResult
- {
- [JsonProperty(PropertyName = "answers")]
- public List<Result> Answers { get; set; }
- }
- public class Result
- {
- [JsonProperty(PropertyName = "answer")]
- public string Answer { get; set; }
- [JsonProperty(PropertyName = "questions")]
- public List<string> Questions { get; set; }
- [JsonProperty(PropertyName = "score")]
- public double Score { get; set; }
- }
- public Task StartAsync(IDialogContext context)
- {
- context.Wait(QuestionReceivedAsync);
- return Task.CompletedTask;
- }
- private async Task QuestionReceivedAsync(IDialogContext context, IAwaitable<object> result)
- {
- var activity = await result as Activity;
- await context.PostAsync(GetAnswer(activity.Text));
- }
- private string GetAnswer(string query)
- {
- string responseString = string.Empty;
- var knowledgebaseId = Convert.ToString(ConfigurationManager.AppSettings["KNOWLEDGE_BASE_ID"], CultureInfo.InvariantCulture);
- //Build the URI
- var builder = new UriBuilder(string.Format(Convert.ToString(ConfigurationManager.AppSettings["QNA_SERVICE_URL"], CultureInfo.InvariantCulture), knowledgebaseId));
- //Add the question as part of the body
- var postBody = string.Format("{{\"question\": \"{0}\"}}", query);
- //Send the POST request
- using (WebClient client = new WebClient())
- {
- //Set the encoding to UTF8
- client.Encoding = System.Text.Encoding.UTF8;
- //Add the subscription key header
- var qnamakerSubscriptionKey = Convert.ToString(ConfigurationManager.AppSettings["SUBSCRIPTION_KEY"], CultureInfo.InvariantCulture);
- client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);
- client.Headers.Add("Content-Type", "application/json");
- responseString = client.UploadString(builder.Uri, postBody);
- }
- QnAMakerResult result = JsonConvert.DeserializeObject<QnAMakerResult>(responseString);
- return result.Answers[0].Answer;
- }




Join the conversation! Your thoughts help the community grow.