【问题标题】:Azure Bot dialog with if-else condition带有 if-else 条件的 Azure Bot 对话框
【发布时间】:2020-02-27 18:53:04
【问题描述】:

我正在使用示例 Azure MSGraph Bot 身份验证 (https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/24.bot-authentication-msgraph/Dialogs/MainDialog.cs#L112),没有任何代码更改(仅更改 APP id 和连接名称)。这是代码的一部分,对我来说是错误的:

private async Task<DialogTurnResult> ProcessStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (stepContext.Result != null)
            {
                var tokenResponse = stepContext.Result as TokenResponse;

                if (tokenResponse?.Token != null)
                {
                    var parts = ((string)stepContext.Values["command"] ?? string.Empty).ToLowerInvariant().Split(' ');

                    var command = parts[0];

                    if (command == "me")
                    {
                        await OAuthHelpers.ListMeAsync(stepContext.Context, tokenResponse);
                    }
                    else if (command.StartsWith("send"))
                    {
                        await OAuthHelpers.SendMailAsync(stepContext.Context, tokenResponse, parts[1]);
                    }
                    else if (command.StartsWith("recent"))
                    {
                        await OAuthHelpers.ListRecentMailAsync(stepContext.Context, tokenResponse);
                    }
                    else
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Your token is: {tokenResponse.Token}"), cancellationToken);
                    }
                }
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("We couldn't log you in. Please try again later."), cancellationToken);
            }

            return await stepContext.EndDialogAsync(cancellationToken: cancellationToken);
        }

当我输入“我”命令时,接收我的凭据。之后,我输入了另一个命令(例如,“recent”),机器人再次欢迎我。我认为这可能是因为 ProcessStepAsync 方法只调用一次,然后对话框返回到第一步。但我希望机器人每次收到我的命令时都返回到 if/else 对话框。

换句话说,当我输入命令时,我怎样才能改变对话的轮次,所以它会再次回到 ProcessStepAsync?

【问题讨论】:

  • 问题解决了吗?让我知道你的更新。
  • 好的,我会在尝试解决方案后立即通知您,谢谢
  • 听起来不错,编码时间很长???

标签: c# azure botframework


【解决方案1】:

在这种情况下,您必须在接受用户输入时执行此操作,并且必须像给定格式一样检查该输入。我会推荐你​​switch-case 相当单调的 if-else 块。

试试这样:

 dynamic checkUserInput = turnContext.Activity.Text;

                //Check Each User Input
                switch (checkUserInput.ToLower())
                {
                    case "me":
                        await turnContext.SendActivityAsync(MessageFactory.Text("You have typed me"), cancellationToken);

                        await turnContext.SendActivityAsync(MessageFactory.Text("Once you begin using solution workspace, you'll see checklist that will be help you too:"), cancellationToken);
                        //You can add any additional flow here if needed
                        var overview = OverViewFlow();
                        await turnContext.SendActivityAsync(overview).ConfigureAwait(false);
                        break;
                    case "send":
                          await turnContext.SendActivityAsync(MessageFactory.Text("You have typed send"), cancellationToken);
                         break;
                      default: //When nothing found in user intent
                        await turnContext.SendActivityAsync(MessageFactory.Text("What are you looking for?"), cancellationToken);
                        break;

                }

虽然它不是必需的,但我正在向您展示如何完成这些事情。

 public IMessageActivity OverViewFlow()
        {
            try
            {
                var replyToConversation = Activity.CreateMessageActivity();
                replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                replyToConversation.Attachments = new List<Attachment>();

                Dictionary<string, string> cardContentList = new Dictionary<string, string>();
                cardContentList.Add("Link 1", "https://via.placeholder.com/300.png/09f/fffC/O");
                cardContentList.Add("Link 2", "https://via.placeholder.com/300.png/09f/fffC/O");
                cardContentList.Add("Link 3", "https://via.placeholder.com/300.png/09f/fffC/O");

                foreach (KeyValuePair<string, string> cardContent in cardContentList)
                {
                    List<CardImage> cardImages = new List<CardImage>();
                    cardImages.Add(new CardImage(url: cardContent.Value));

                    List<CardAction> cardButtons = new List<CardAction>();

                    CardAction plButton = new CardAction()
                    {
                        Value = $"",
                        Type = "imBack",
                        Title = "" + cardContent.Key + ""
                    };

                    cardButtons.Add(plButton);

                    HeroCard plCard = new HeroCard()
                    {
                        Title = $"",
                        Subtitle = $"",
                        Images = cardImages,
                        Buttons = cardButtons
                    };

                    Attachment plAttachment = plCard.ToAttachment();
                    replyToConversation.Attachments.Add(plAttachment);
                }

                return replyToConversation;
            }
            catch (Exception ex)
            {
                throw new NotImplementedException(ex.Message, ex.InnerException);
            }
        }

希望对你有帮助。

【讨论】:

  • 我应该把这段代码粘贴到哪里?如果我将它粘贴到 ProcessStepAsync 方法中,我会看到相同的行为 - 在第一个命令之后,所有对话框都返回到 LoginStepAsync 方法。顺便说一句,我收到一个错误:“检测到类型为'System.Reflection.RuntimeModule'的属性'模块'的自引用循环。路径'context.adapter.onTurnError.method.module.assembly.entryPoint
  • 在程序入口点。我已将其放入 protected override async Task OnMessageActivityAsync(ITurnContext&lt;IMessageActivity&gt; turnContext, CancellationToken cancellationToken) 作为我的机器人入口点
  • 它有效 :) 但是现在跳过了登录步骤,我可以在没有身份验证的情况下做一些事情。如何在切换块中的任何情况之前通过登录步骤(MainDialog.cs 中的 loginStepAsync)?
  • 只需首先输入登录流程,然后每次检查令牌是否有效,然后跳过令牌流程并重定向到您预期的流程,如果有代码,那就太好了容易解决,不用担心我在这里一起解决^^
  • 但是登录步骤在 MainDialog 类中,有自己的逻辑和参数(并且 tokenResponse 在 WaterFallStepContext 中)。如何从 DialogBot.cs 访问它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多