0

Hi I am working on Microsoft Bot Framework and in that I am working QnAMaker with including LUIS application.

Here my question is when I am working QnAMaker its providing the Exact answer and when I am adding the LUIS service I am getting the Exact answer in debugging and when I am providing the reply to the user by using await context.PostAsync(answer); its by default going to the exception section and its showing exception is like below.

Object reference is not set be instance of object.

Here I can't understand where is the null is occurred because of I am getting the data from QnAMaker.

In the below I am sharing the code what I have write for getting the response to user form QnAMaker after the LUIS identify the Intents and Entities

private async Task toGetTheResponsefromQnAMakerAsync(IDialogContext context, Activity activity)
    {
        var messagetext = (activity.Text).ToLower();
        var knowledgebaseId = ConfigurationManager.AppSettings["KnowledgeBaseId"];

        //Use subscription key assigned to you.
        var qnamakerSubscriptionKey = ConfigurationManager.AppSettings["QnAmakerSubscriptionKey"];
        var client = new HttpClient();
        var queryString = HttpUtility.ParseQueryString(string.Empty);

        // Request headers
        client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);

        var uri = "https://westus.api.cognitive.microsoft.com/qnamaker/v2.0/knowledgebases/subscription-id/generateAnswer?" + queryString;

        HttpResponseMessage response;

        // Request body
   byte[] byteData = Encoding.UTF8.GetBytes($"{{\"question\": \"{messagetext}\"}}");

        using (var content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            response = await client.PostAsync(uri, content);
        }
        try
        {
            if (response.IsSuccessStatusCode)
            {
                var responseContent = response.Content.ReadAsStringAsync().Result;

                var QnAMakerResponse = JsonConvert.DeserializeObject<RootObject>(responseContent);

                if (QnAMakerResponse.answers != null)
                {
                    foreach (var item in QnAMakerResponse.answers)
                        // return our QnAMakerResponse to the user
                        answer = item.answer.ToString();
                        await context.PostAsync(answer);

                }
            }
        }
        catch (Exception e)
        {

        }
    }
sateesh
  • 595
  • 5
  • 16
  • If you send pain text message, for example `await context.PostAsync("this is an answer")`, does it throw exception? Besides, you can share the code of your QnADialog, which would help understand and troubleshoot the issue. – Fei Han Apr 17 '18 at 02:43
  • @FeiHan please see my updated question and please suggest me the solution on this – sateesh Apr 17 '18 at 04:46

2 Answers2

3

I think right now, you are using old version of Bot Connector and Bot Builder DLL's. So, that’s why you are getting null reference exception from the source location of Microsoft.Bot.Connector.

So, update all you are DLLs related to Bot Framework with latest version like 3.15.0.

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Pradeep
  • 3,671
  • 8
  • 41
  • 83
  • Thanks Pradeep and now its working fine and after I am seeing your answer I am updated the all the DLL's. – sateesh Apr 17 '18 at 06:46
  • Hi @sateesh, what is the version of Bot sdk you used before? I'm not using 3.15.0, and as i mentioned in my reply, the code also work for me. – Fei Han Apr 17 '18 at 06:57
  • Hi @FeiHan before that I am used the old version of Bot Sdk its 3.13.0 and now my code is working fine. – sateesh Apr 17 '18 at 09:46
0

Based on your description and code, it seems that you make request to QnAMaker API to retrieve the answer for the given question, and send the answer to user. I modify the code you provided and do a test on my side, I do not get that exception, you can check it.

try
{
    if (response.IsSuccessStatusCode)
    {
        var responseContent = response.Content.ReadAsStringAsync().Result;

        var QnAMakerResponse = JsonConvert.DeserializeObject<Microsoft.Bot.Builder.CognitiveServices.QnAMaker.QnAMakerResults>(responseContent);

        if (QnAMakerResponse.Answers != null)
        {
            var answer = QnAMakerResponse.Answers.First().Answer;
            await context.PostAsync(answer);
        }
    }
}
catch (Exception e)
{

}

Note:

  • In my code, I deserialize the JSON to Microsoft.Bot.Builder.CognitiveServices.QnAMaker.QnAMakerResults object
  • You do not specify top field in your request body, so it will return only one answer in response, you do not need to use foreach loop.
  • To troubleshoot the issue, you can try to create a new bot application, then put and run the code you provided in MessageReceivedAsync method to check if the code can work without using LUIS dialog.

Test result:

enter image description here

Fei Han
  • 21,907
  • 1
  • 16
  • 28