hitcai
hitcai

Reputation: 13

botFramework v4 how to handle dialog response after LUIS call

I have a bot written in C# that is using LUIS to determine intents. I have a method that makes a call to the LUIS service and then looks for an 'Open_Case' intent. The model has a CaseNumber entity defined which may or may not be included in the response from the LUIS service. If the response doesn't have a case number entity I start a dialog to ask the user for the case number. Once I have a case number I then want to return a card with case information.

Here's the code I have:-

/// <summary>
/// Dispatches the turn to the requested LUIS model.
/// </summary>
private async Task DispatchToLuisModelAsync(ITurnContext context, string appName, DialogContext dc, CancellationToken cancellationToken =
default (CancellationToken)) {
var result = await botServices.LuisServices[appName].RecognizeAsync(context, cancellationToken);
var intent = result.Intents ? .FirstOrDefault();

    string caseNumber = null;

    if (intent ? .Key == "Open_Case") {
    if (!result.Entities.ContainsKey("Case_CaseNumber")) {
    var dialogResult = await dc.BeginDialogAsync(CaseNumberDialogId, null, cancellationToken);
     } else {
      caseNumber = (string)((Newtonsoft.Json.Linq.JValue) result.Entities["Case_CaseNumber"].First).Value;
      var cardAttachment = botServices.CaseInfoServices.LookupCase(caseNumber);
      var reply = context.Activity.CreateReply();
      reply.Attachments = new List < Attachment > () {
       cardAttachment
      };
      await context.SendActivityAsync(reply, cancellationToken);
     }
    }
   }

What I'm struggling with is where the code send the card response should sit. In the code I currently have I send the card if the number was returned in the LUIS response, but if there was no number and I start the dialog then I only get access to the number either in the final step of the dialog or in the dialog result in the root turn handler. I've currently duplicated the reply inside the final step in the dialog, but it feels wrong and inelegant. I'm sure there must be a way that I can collect the number from LUIS or the dialog and THEN send the response from a single place instead of duplicating code.

Any suggestions gratefully received...

Upvotes: 0

Views: 196

Answers (1)

hitcai
hitcai

Reputation: 13

I came to the conclusion that I need to put the code that displays the card into a method on the bot class, then call it from the else in code snippet and also from the turn handler when the dialogTurnStatus is equal to Complete

Upvotes: 0

Related Questions