Search code examples
c#visual-studiobotframeworkazure-qna-maker

How do I parse a question that contains escape sequences from Microsoft QnA Maker in my bot project?


I'm testing and training a new QnA bot for my web application and I want to print out the right answer format when it encounters escape sequences. How can I implement such approach in order to have the bot recognize the escape sequences that I have added? The bot emulator adds an extra '\' at the beginning of '\n\n'

I am using Bot Framework emulator for sdvk 3 and QnA Maker website My answer is as follows:

\n\n 1. Visit the heroes Portal website.\n\n 2. Select the create button.\n\n 3. Click “choose class” under the classes \n your heroes section.\n\n 4. Follow the instructions provided.\n If you require further assistance, please email us \n at ###@$$$.com\n 
using Microsoft.Bot.Builder.CognitiveServices.QnAMaker;
using System;

namespace heroes.ChatBot.Dialogs.QnA
{
    [Serializable]
    [QnAMaker("####", "###",
        "Sorry I could not find an answer to your  question", 0.5, 1, "website" )]
    public class QnAHeroesDialog : QnAMakerDialog
    {

    }
}
1.Visit the heroes Portal website.

2.Select the create button.

3.Click “choose class” under the classes \n your heroes section.

4.Follow the instructions provided.\n
  If you require further assistance,\n
  please follow instruction.

Solution

  • What you are looking for is an override of the response provided by QnAMaker. There are some samples available in the official Github repo: https://github.com/Microsoft/BotBuilder-CognitiveServices/blob/master/CSharp/Samples/QnAMaker/QnABotWithOverrides/Dialogs/QnADialogWithOverrides.cs

    In a few words, override RespondFromQnAMakerResultAsync to handle this "double \n" problem

    It will look like the following:

    [Serializable]
    [QnAMaker("####", "###",
        "Sorry I could not find an answer to your  question", 0.5, 1, "website" )]
    public class QnAHeroesDialog : QnAMakerDialog
    {
        protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults results)
        {
            if (results.Answers.Count > 0)
            {
                var foundReply = results.Answers.First().Answer;
                var response = $"{foundReply.Replace("\n\n", "\n")}";
                await context.PostAsync(response);
            }
        }
    }
    

    My code may need a quick tuning for the Replace as I don't have the exact format of your response value