调用PromptDialog.Choice()时出现“抱歉我的机器人代码有问题”消息

时间:2018-08-24 05:55:59

标签: c# botframework

代码流如下:

  • 我有一个DialogA,它通过其StartAsync方法调用qnaMaker对话框。
  • qnaMaker对话框回调回调了助手类中的promptDialog函数(顺便说这不是Dialog)
  • 一旦用户在提示上选择了适当的选项,就会调用一个回叫来发布消息
  • 发布消息(“有用” /“无用”)后出现错误

我的第一个倾向是在context.Wait()回调中发布消息后使用OnOptionSelectedForPrompt,似乎在这里调用wait以某种方式为ResumeAfter执行了DialogA回调

代码示例: DialogA.cs

[Serializable]
    public class DialogA : IDialog<object>
    {
        private string tool = "sometool";
        private string userInput = String.Empty;
        private bool sendToQnaMaker = false;

        public DialogA(string userQuery)
        {
            if (userQuery.Length > 0)
            {
                sendToQnaMaker = true;
                userInput = userQuery;
            }
        }

        public async Task StartAsync(IDialogContext context)
        {
            if(sendToQnaMaker)
            {
                await context.Forward(new QnaDialog(), QnaMakerDialogCallback, BotHelper.CreateMessageActivityForQnaMaker(userInput), CancellationToken.None);
            } else
            {
                await BotHelper.ShowWelcomeMessageOnMenuSection(context, tool);
                context.Wait(OnMessageReceived);
            }
        }

    private async Task QnaMakerDialogCallback(IDialogContext context, IAwaitable<object> result)
    {
        var response = await result;
        bool isInQnaMaker = Convert.ToBoolean(response);
        BotHelper botHelper = new BotHelper(tool);
        botHelper.showPrompt(context)
    }

BotHelper.cs

    public void showPrompt(IDialogContext context)
    {

        PromptDialog.Choice(context, OnOptionSelectedForPrompt, Constants.promptChoices, MiscellaneousResponses.answerFeedbackMessage, MiscellaneousResponses.invalidOptionMessage, 3);
    }

    public async Task OnOptionSelectedForPrompt(IDialogContext context, IAwaitable<string> result)
    {
            string optionSelected = await result;

            switch (optionSelected)
            {
                case MiscellaneousResponses.helpfulMessage:
                    string botreply = "Helpful";
                    await context.PostAsync(botreply);
                    break;
                case MiscellaneousResponses.notHelpfulMessage:
                    string botResponse = "Not Helpful";
                    await context.PostAsync(botResponse);
                    break;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

创建一个单独的对话框而不使用BotHelper类,并使用Context.DoneContext.Wait正确地处理对话框堆栈。