.net-core - 使用 HeroCards 作为选择提示:无法选择选项
问题描述
当用户可以选择要走的路径时,我使用的是 aHeroCard
而不是s。PromptOption
中的选项HeroCard
出现后ProductStepAsync
,下一个瀑布步骤ChoiceStepAsync
将被跳过,因为您没有机会选择选项,因此它直接在开关中变为默认值。我究竟做错了什么?
编辑:在开关的默认情况下,ChoiceStepAsync
我尝试返回上一个任务(ProductStepAsync)
和当前任务,但问题是它直接进入开关中的默认值。
这是我的代码:
ProductDialog.cs
public class ProductDialog : ComponentDialog
{
private const string UserInfoProduct = "values-userInfo-Product";
public ProductDialog() : base(nameof(ProductDialog)) {
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
ProductStepAsync,
ChoiceStepAsync,
MoreInfoStepAsync,
MoreInfoChoiceStepAsync,
}));
AddDialog(new PhoneDialog());
InitialDialogId = nameof(WaterfallDialog);
}
private static async Task<DialogTurnResult> ProductStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values[UserInfoProduct] = new UserProfile();
var getProduct = stepContext.Context.Activity.CreateReply();
var ProductOptions = new HeroCard
{
Text = "What can I help you with?",
Buttons = new List<CardAction>
{
new CardAction() { Title = " Contact", Type = ActionTypes.ImBack, Value = "Contact" },
new CardAction() { Title = " Newsletter", Type = ActionTypes.ImBack, Value = "Newsletter" },
new CardAction() { Title = "About us", Type = ActionTypes.ImBack, Value = "About us" },
},
};
getProduct.Attachments = new List<Attachment>() { ProductOptions.ToAttachment() };
await stepContext.Context.SendActivityAsync(getProduct, cancellationToken);
return await stepContext.NextAsync(stepContext.Values[UserInfoProduct], cancellationToken);
}
在这里,它直接在开关中默认为:
private async Task<DialogTurnResult> ChoiceStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var userProfile = (UserProfile)stepContext.Values[UserInfoProduct];
userProfile.Product = (stepContext.Result).ToString();
string choice = userProfile.Product.ToLowerInvariant();
switch (choice)
{
case "contact":
{
await stepContext.Context.SendActivityAsync($"Info");
return await stepContext.NextAsync(stepContext.Values[UserInfoProduct], cancellationToken);
}
case "newsletter":
{
return await stepContext.NextAsync(stepContext.Values[UserInfoProduct], cancellationToken);
case "about us":
{
return await stepContext.NextAsync(stepContext.Values[UserInfoProduct], cancellationToken);
}
default:
{
return await stepContext.NextAsync(stepContext.Values[UserInfoProduct], cancellationToken);
}
}
}
private const string YesOption = "Yes";
private const string NoOption = "No";
private const string BackOption = "Return";
private static List<Choice> ConfirmOptions = new List<Choice>()
{
new Choice(YesOption) {Value = "Yes", Synonyms = new List<string> { "Yes" } },
new Choice(NoOption) { Value = "No",Synonyms = new List<string> { "No" }},
new Choice(BackOption) { Synonyms = new List<string> { "Return" }}
};
private async Task<DialogTurnResult> MoreInfoStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
var userProfile = (UserProfile)stepContext.Values[UserInfoProduct];
await stepContext.Context.SendActivityAsync($"Would you like to leave your phone number?", cancellationToken: cancellationToken);
return await stepContext.PromptAsync(nameof(ChoicePrompt), new PromptOptions()
{
Choices = ConfirmOptions,
},
cancellationToken);
}
private async Task<DialogTurnResult> MoreInfoChoiceStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var userProfile = (UserProfile)stepContext.Values[UserInfoProduct];
var optionSelected = ((FoundChoice)stepContext.Result).Value;
switch (optionSelected)
{
case YesOption:
return await stepContext.BeginDialogAsync(nameof(PhoneDialog), cancellationToken);
case NoOption:
return await stepContext.EndDialogAsync(stepContext.Values[UserInfoProduct], cancellationToken);
case BackOption:
return await stepContext.ReplaceDialogAsync(nameof(ProductDialog), cancellationToken);
}
await stepContext.Context.SendActivityAsync("I don't recognize this option", cancellationToken: cancellationToken);
return await stepContext.EndDialogAsync(stepContext.Values[UserInfoProduct], cancellationToken);
}
}
解决方案
我想通了,在 HeroCard 之后添加一个新PromptOptions()
的ChallengeStepAsync
,然后使用PromptAsync
. 所以我改变了ChallengeStepAsync
这个:
private static async Task<DialogTurnResult> ProductStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values[UserInfoProduct] = new UserProfile();
var getProduct = stepContext.Context.Activity.CreateReply();
var ProductOptions = new HeroCard
{
Text = "What can I help you with?",
Buttons = new List<CardAction>
{
new CardAction() { Title = " Contact", Type = ActionTypes.ImBack, Value = "Contact" },
new CardAction() { Title = " Newsletter", Type = ActionTypes.ImBack, Value = "Newsletter" },
new CardAction() { Title = "About us", Type = ActionTypes.ImBack, Value = "About us" },
},
};
getProduct.Attachments = new List<Attachment>() { ProductOptions.ToAttachment() };
var options = new PromptOptions() {
Prompt = getChallenge,
RetryPrompt = MessageFactory.Text("I don't recognize this option")
};
return await stepContext.PromptAsync(nameof(TextPrompt), options, cancellationToken);
}
推荐阅读
- reactjs - 如何在 Vercel 上托管 React 应用程序及其在共享主机上的后端
- python - Python 文件管理
- sql-server - 根据时间戳查询 CosmosDB
- python - 仅从一列中删除重复项,同时保持行的其余部分完整数据框熊猫
- python - 如何在 python 套接字模块中接收 sendall
- html - 如何利用网络上的 CSS 按钮?
- python-3.x - 等效于 bash 中的 python base32 编码
- c# - 实例化预制件时出现 NullReferenceException
- android - 为什么将 gdbserver 附加到本机 android 应用程序失败?
- javascript - 如何通过单击带有 alpine js 的元素来访问受模块保护的功能?