如何从Messenger位置快速回复中接收位置

时间:2019-03-26 10:09:43

标签: c# botframework messenger

嗨,我有这段代码,提示用户输入他们的位置。 我正在使用Botframework v4和C#。我将Messenger的快速回复放在附件提示中。

  if (response == "test location")
        {
            Activity reply = stepContext.Context.Activity.CreateReply();

            reply.ChannelData = JObject.FromObject(
            new
            {
                text = "loc",
                quick_replies = new object[]
                {
                    new
                    {
                        content_type = "location",
                    },
                },
            });

            return await stepContext.PromptAsync(
               ATTACHPROMPT,
               new PromptOptions
               {
                   Prompt = reply,
               });
        }

但是在用户发送其位置之后。机器人崩溃。我该如何处理位置信息的返回,使机器人不会崩溃?这是例外

  

System.ArgumentNullException:值不能为null。参数名称:Microsoft.Bot.Builder.AI.Luis.LuisRecognizer.d * 23.MoveNext()处的讲话---从上次引发异常的位置开始的堆栈跟踪---在System.Runtime.ExceptionServices.ExceptionDispatchInfo处。 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的Microsoft.Bot.Builder.AI.Luis.LuisRecognizer.d * 10.MoveNext( )-从先前抛出异常的位置开始的堆栈跟踪--在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.BotBuilderSamples.BasicBot.d * 24.MoveNext()中的.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务),位于C:\ Users \ bguevarra \ Desktop \ finko \ FinkoBot \ Bots \ BasicBot.cs:第104行---堆栈结束从exe的先前位置追踪在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()被抛出了ption .Builder.MiddlewareSet.d * 3.MoveNext()-从上一个引发异常的位置开始的堆栈跟踪--在System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()处。 Microsoft.Bot.Builder.BotAdapter.d__13.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的ThrowForNonSuccess(任务任务)==对不起,看来有点问题。

OnTurnAsync()

    public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        var dc = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
        var activity = turnContext.Activity;

        var userstate = await _basicAccessors.BasicUserStateAccessor.GetAsync(turnContext, () => new BasicUserState(), cancellationToken);
        var state = await _basicAccessors.BasicStateAccessor.GetAsync(turnContext, () => new BasicState(), cancellationToken);

        if (turnContext.Activity.Type == ActivityTypes.Message)
        {
            turnContext.TurnState.Add("BasicAccessors", _basicAccessors);

            string text = string.IsNullOrEmpty(turnContext.Activity.Text) ? string.Empty : turnContext.Activity.Text.ToLower();

            var luisResults = await _services.LuisServices[LuisConfiguration].RecognizeAsync(dc.Context, cancellationToken);
            var topScoringIntent = luisResults?.GetTopScoringIntent();
            var topIntent = topScoringIntent.Value.intent;
            turnContext.TurnState.Add("topIntent", topIntent);

            turnContext.TurnState.Add("text", text);

            var interrupted = await IsTurnInterruptedAsync(turnContext);
            if (interrupted)
            {
                await _basicAccessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
                await _basicAccessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
                return;
            }

            // Continue the current dialog
            var dialogResult = await dc.ContinueDialogAsync();

            // if no one has responded,
            if (!dc.Context.Responded)
            {
                // examine results from active dialog
                switch (dialogResult.Status)
                {
                    case DialogTurnStatus.Empty:

                        switch (topIntent) 
                        {
                            case MainDialogIntent:
                                await dc.BeginDialogAsync(MainDialogId);
                                break;

                            case LoanCalculatorIntent:
                                await dc.BeginDialogAsync(LoanCalculatorId);
                                break;

                            case RealEstateIntent:
                                await dc.BeginDialogAsync(RealEstateDialogId);
                                break;

                            case GreetingIntent:
                                Random rnd = new Random();
                                int x = rnd.Next(1, 5);
                                switch (x)
                                {
                                    case 1:
                                        await dc.Context.SendActivityAsync("Hi!");
                                        break;

                                    case 2:
                                        await dc.Context.SendActivityAsync("Hello!");
                                        break;

                                    case 3:
                                        await dc.Context.SendActivityAsync("Good day!");
                                        break;

                                    default:
                                        break;
                                }

                                break;

                            case NoneIntent: // NoneIntent:
                            default:
                                await dc.Context.SendActivityAsync("I didn't understand what you just said to me.");
                                break;
                        }

                        break;

                    case DialogTurnStatus.Waiting:
                        // The active dialog is waiting for a response from the user, so do nothing.
                        break;

                    case DialogTurnStatus.Complete:
                        await dc.EndDialogAsync();
                        break;

                    default:
                        await dc.CancelAllDialogsAsync();
                        break;
                }
            }

            await _basicAccessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
            await _basicAccessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }
        else if (activity.Type == ActivityTypes.ConversationUpdate)
        {
            if (activity.MembersAdded != null)
            {
                // Iterate over all new members added to the conversation.
                foreach (var member in activity.MembersAdded)
                {
                    // Greet anyone that was not the target (recipient) of this message.
                    // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
                    if (member.Id != activity.Recipient.Id)
                    {
                        await SendWelcomeMessageAsync(turnContext, cancellationToken);
                    }
                }
            }
        }

        await _basicAccessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
        await _basicAccessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
    }

1 个答案:

答案 0 :(得分:1)

当用户单击Facebook Messenger中的位置快速答复时,传入的活动不具有text属性,该属性会在调用LUIS时导致“值不能为null”错误。目前在BotFramework中这是已知的issue,并且开发团队目前正在努力解决此问题。同时,在调用LUIS识别器之前,请检查并确保传入活动的text属性不为null或为空。

if(!string.IsNullOrEmpty(turnContext.Activity.Text)) {
  var luisResults = await _services.LuisServices[LuisConfiguration].RecognizeAsync(dc.Context, cancellationToken);
} 

希望这会有所帮助!