c# - 带有 if-else 条件的 Azure Bot 对话框
问题描述
我正在使用示例 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?
解决方案
在这种情况下,您必须在从用户获取输入时执行此操作,并且必须像给定格式一样检查该输入。我会推荐你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);
}
}
希望它会帮助你。
推荐阅读
- service - 如何在 solaris 11 崩溃后自动重启服务?
- python - 覆盖 get_queryset 方法后,Django 管理列表页面需要永远加载
- java - 如何将 java 类类型转换为其基类型?
- python - 无法在 python kivy 音频中打开混音器
- sql - 使用前一行计算/条件的 Oracle SQL 滞后函数
- javascript - 在 html 表中呈现 JSON 树对象
- java - ResourceAccessException:POST 请求的 I/O 错误
- reactjs - Typescript 给了我一个错误:Expected 0 arguments, but got 1
- python - 使用来自另一个数据帧的值将熊猫条样式应用于数据帧
- javascript - 标题下方的 Bootstrap3 图像,重新缩放图像以适合