1

I have Luis and QnA running simultaneously , by using Dispatch cli. I was trying to implement Adaptive Card which is also working fine, i can add/view any type easily. The issue arises when i try to get response on Adaptive card submit button click. My bot backtracks it to the switch statement where when the intent is not being traced a default case is being triggered. On commenting the default i am getting this error

System.Collections.Generic.KeyNotFoundException: The given key ‘luisResult’ was not present in the dictionary.

at System.Collections.Generic.Dictionary`2.get_Item(TKey key)

at Microsoft.BotBuilderSamples.DispatchBot.DispatchToTopIntentAsync(ITurnContext`1

turnContext, String intent, RecognizerResult recognizerResult, CancellationToken cancellationToken) in F:\nikhil\Alfi\mts-qna+luis\Bots\DispatchBot.cs:line 76

at Microsoft.BotBuilderSamples.DispatchBot.OnMessageActivityAsync(ITurnContext`1

turnContext, CancellationToken cancellationToken) in F:\nikhil\Alfi\mts-qna+luis\Bots\DispatchBot.cs:line 45

at Microsoft.Bot.Builder.BotFrameworkAdapter.TenantIdWorkaroundForTeamsMiddleware.OnTurnAsync(ITurnContext

turnContext, NextDelegate next, CancellationToken cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder\BotFrameworkAdapter.cs:line 964

at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext

turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder\MiddlewareSet.cs:line 55

at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken

cancellationToken) in d:\a\1\s\libraries\Microsoft.Bot.Builder\BotAdapter.cs:line 167

I have tried this -

  if (turnContext.Activity.Value != null)
                    {
                        var activity = turnContext.Activity;
                        activity.Text = JsonConvert.SerializeObject(activity.Value);
                        await turnContext.SendActivityAsync(MessageFactory.Text("activity.Text"), cancellationToken);
                    }

On debugging i can see the values like this - activity.Value = {{ "startdate": "2017-10-12", "enddate": "2017-10-12" }}

namespace Microsoft.BotBuilderSamples
{
    public class DispatchBot : ActivityHandler
    {
        private ILogger<DispatchBot> _logger;
        private IBotServices _botServices;

        public DispatchBot(IBotServices botServices, ILogger<DispatchBot> logger)
        {
            _logger = logger;
            _botServices = botServices;
        }

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
            var recognizerResult = await _botServices.Dispatch.RecognizeAsync(turnContext, cancellationToken);

            // Top intent tell us which cognitive service to use.
            var topIntent = recognizerResult.GetTopScoringIntent();

            // Next, we call the dispatcher with the top intent.
            await DispatchToTopIntentAsync(turnContext, topIntent.intent, recognizerResult, cancellationToken);
        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            const string WelcomeText = "I am here to make your experience much more easier";

            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text($"Hi {member.Name}, I am at your service . {WelcomeText}"), cancellationToken);
                }
            }
        }

        private async Task DispatchToTopIntentAsync(ITurnContext<IMessageActivity> turnContext, string intent, RecognizerResult recognizerResult, CancellationToken cancellationToken)
        {
            var activity = turnContext.Activity;
            activity.Text = JsonConvert.SerializeObject(activity.Value);
            switch (intent)
            {
                case "l_mts-bot-809f":
                    activity.Text = JsonConvert.SerializeObject(activity.Value);
                    await ProcessHomeAutomationAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
                    break;

                case "q_mts-bot":
                    await ProcessSampleQnAAsync(turnContext, cancellationToken);
                    break;
                default:
                    await ProcessHomeAutomationAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
                    Console.WriteLine(">>>>>>>>>>>"+recognizerResult.Properties["luisResult"]);
                    break;
                    //    _logger.LogInformation($"Dispatch unrecognized intent: {intent}.");
                    //    activity.Text = JsonConvert.SerializeObject(activity.Value);
                    //    await turnContext.SendActivityAsync(MessageFactory.Text($"Dispatch unrecognized intent: {intent}."), cancellationToken);
                    //    break;
            }
        }
        private Activity CreateResponse(IActivity activity, Attachment attachment)
        {
            var response = ((Activity)activity).CreateReply();
            response.Attachments = new List<Attachment>() { attachment };
            return response;
        }

        private async Task ProcessHomeAutomationAsync(ITurnContext<IMessageActivity> turnContext, LuisResult luisResult, CancellationToken cancellationToken)
        {
            _logger.LogInformation("ProcessHomeAutomationAsync");

            // Retrieve LUIS result for Process Automation.
            var result = luisResult.ConnectedServiceResult;

            var topIntent = result.TopScoringIntent.Intent;
            var entity = result.Entities;


                if (topIntent == "welcome")
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("Hi,This is Alfie"), cancellationToken);
            }
            if (topIntent == "None")
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("Sorry I didnt get you!"), cancellationToken);
            }
            if (topIntent == "LeaveApplication")
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("Do you want to apply leaves for yourself or for someone else?"), cancellationToken);
            }
            if (topIntent == "LeaveSelfApplication")
            {

                var DatesRange = LeavesDatesAdaptiveCardAttachment();
                var response = CreateResponse(turnContext.Activity, DatesRange);
                await turnContext.SendActivityAsync(response, cancellationToken);




                if (turnContext.Activity.Value != null)
                {
                    var activity = turnContext.Activity;
                    activity.Text = JsonConvert.SerializeObject(activity.Value);
                    await turnContext.SendActivityAsync(MessageFactory.Text("2019-07-30"), cancellationToken);
                }


                //await turnContext.SendActivityAsync(MessageFactory.Text("2019-07-30"), cancellationToken);

            }

            if (topIntent == "LeavesDateTenure")
            {
                string jsonData = JsonConvert.SerializeObject(result);
                dynamic json = JsonConvert.DeserializeObject(jsonData);


                await turnContext.SendActivityAsync(MessageFactory.Text("Please Provide me with your Leaves Tenure"), cancellationToken);

            }



        }

        //string jsonData = JsonConvert.SerializeObject(result);

        private async Task ProcessSampleQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            _logger.LogInformation("ProcessSampleQnAAsync");

            var results = await _botServices.SampleQnA.GetAnswersAsync(turnContext);
            if (results.Any())
            {
                await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
            }
            else
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
            }
        }

        // Load attachment from file.
        private Attachment CreateAdaptiveCardAttachment()
        {
            // combine path for cross platform support
            string[] paths = { ".", "Cards", "AddingLeaveDetails.json" };
            string fullPath = Path.Combine(paths);
            var adaptiveCard = File.ReadAllText(fullPath);
            return new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(adaptiveCard),
            };
        }
        private Attachment LeavesDatesAdaptiveCardAttachment()
        {
            // combine path for cross platform support
            string[] paths = { ".", "Cards", "LeavesDates.json" };
            string fullPath = Path.Combine(paths);
            var adaptiveCard = File.ReadAllText(fullPath);



            return new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content = JsonConvert.DeserializeObject(adaptiveCard),

            };
        }



    }

}
Nikhil Bansal
  • 163
  • 3
  • 16
  • Did I solve this with [my update, here,](https://stackoverflow.com/a/57260070/10860086) or is this a separate issue? I'm relatively certain that the "Update" under "Capture User Input" solves this. – mdrichardson Jul 30 '19 at 17:54

0 Answers0