![](/img/trans.png)
[英]Adaptive Card response from a WaterfallStep Dialog MS Bot framework v4
[英]How can I get an adaptive card list from Bot Framework v4 again?
這是帶來我的自適應卡的Dialogs/MainDialog.cs的來源。
如果您已經在下面的 ShowCardStepAsync() 部分中選擇了一張卡片,我想再向您展示一張卡片列表以進行重新選擇。 我要你告訴我這個時候該怎么做。
namespace Microsoft.BotBuilderSamples
{
public class MainDialog : ComponentDialog
{
protected readonly ILogger _logger;
public MainDialog(ILogger<MainDialog> logger)
: base(nameof(MainDialog))
{
_logger = logger;
// Define the main dialog and its related components.
AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
ChoiceCardStepAsync,
ShowCardStepAsync,
}));
// The initial child Dialog to run.
InitialDialogId = nameof(WaterfallDialog);
}
// 1. Prompts the user if the user is not in the middle of a dialog.
// 2. Re-prompts the user when an invalid input is received.
private async Task<DialogTurnResult> ChoiceCardStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
_logger.LogInformation("MainDialog.ChoiceCardStepAsync");
// Create the PromptOptions which contain the prompt and re-prompt messages.
// PromptOptions also contains the list of choices available to the user.
var options = new PromptOptions()
{
Prompt = MessageFactory.Text("카드를 선택 해 주세요 "),
RetryPrompt = MessageFactory.Text("올바르지 않은 선택입니다. 카드를 다시 선택해 주세요"),
Choices = GetChoices(),
};
// Prompt the user with the configured PromptOptions.
return await stepContext.PromptAsync(nameof(ChoicePrompt), options, cancellationToken);
}
// Send a Rich Card response to the user based on their choice.
// This method is only called when a valid prompt response is parsed from the user's response to the ChoicePrompt.
private async Task<DialogTurnResult> ShowCardStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
_logger.LogInformation("MainDialog.ShowCardStepAsync");
// Cards are sent as Attachments in the Bot Framework.
// So we need to create a list of attachments for the reply activity.
var attachments = new List<Attachment>();
// Reply to the activity we received with an activity.
var reply = MessageFactory.Attachment(attachments);
// Decide which type of card(s) we are going to show the user
switch (((FoundChoice)stepContext.Result).Value)
{
case "Adaptive Card":
// Display an Adaptive Card
reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
break;
case "Animation Card":
// Display an AnimationCard.
reply.Attachments.Add(Cards.GetAnimationCard().ToAttachment());
break;
case "Audio Card":
// Display an AudioCard
reply.Attachments.Add(Cards.GetAudioCard().ToAttachment());
break;
case "Hero Card":
// Display a HeroCard.
reply.Attachments.Add(Cards.GetHeroCard().ToAttachment());
break;
case "Receipt Card":
// Display a ReceiptCard.
reply.Attachments.Add(Cards.GetReceiptCard().ToAttachment());
break;
case "Signin Card":
// Display a SignInCard.
reply.Attachments.Add(Cards.GetSigninCard().ToAttachment());
break;
case "Thumbnail Card":
// Display a ThumbnailCard.
reply.Attachments.Add(Cards.GetThumbnailCard().ToAttachment());
break;
case "Video Card":
// Display a VideoCard
reply.Attachments.Add(Cards.GetVideoCard().ToAttachment());
break;
default:
// Display a carousel of all the rich card types.
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
reply.Attachments.Add(Cards.GetAnimationCard().ToAttachment());
reply.Attachments.Add(Cards.GetAudioCard().ToAttachment());
reply.Attachments.Add(Cards.GetHeroCard().ToAttachment());
reply.Attachments.Add(Cards.GetReceiptCard().ToAttachment());
reply.Attachments.Add(Cards.GetSigninCard().ToAttachment());
reply.Attachments.Add(Cards.GetThumbnailCard().ToAttachment());
reply.Attachments.Add(Cards.GetVideoCard().ToAttachment());
break;
}
// Send the card(s) to the user as an attachment to the activity
await stepContext.Context.SendActivityAsync(reply, cancellationToken);
// Give the user instructions about what to do next
var ment = MessageFactory.Text("다른 선택지를 골라 주세요.");
// Prompt the user with the configured PromptOptions.
//return await stepContext.PromptAsync(nameof(ChoicePrompt), new PromptOptions, cancellationToken);
await stepContext.Context.SendActivityAsync(ment, cancellationToken);
//await ChoiceCardStepAsync(stepContext, cancellationToken);
return await stepContext.EndDialogAsync();
}
private IList<Choice> GetChoices()
{
var cardOptions = new List<Choice>()
{
new Choice() { Value = "Adaptive Card", Synonyms = new List<string>() { "adaptive" } },
new Choice() { Value = "Animation Card", Synonyms = new List<string>() { "animation" } },
new Choice() { Value = "Audio Card", Synonyms = new List<string>() { "audio" } },
new Choice() { Value = "Hero Card", Synonyms = new List<string>() { "hero" } },
new Choice() { Value = "Receipt Card", Synonyms = new List<string>() { "receipt" } },
new Choice() { Value = "Signin Card", Synonyms = new List<string>() { "signin" } },
new Choice() { Value = "Thumbnail Card", Synonyms = new List<string>() { "thumbnail", "thumb" } },
new Choice() { Value = "Video Card", Synonyms = new List<string>() { "video" } },
new Choice() { Value = "All cards", Synonyms = new List<string>() { "all" } },
};
return cardOptions;
}
}
}
為了解釋這個順序,
** 1.卡片按鈕和卡片按鈕被列出。
選擇按鈕之一以顯示所選類型的卡片。
出示卡片,同時再次發送卡片重選評論和卡片列表。
這一直持續到選擇結束按鈕。**
您可以使用:
stepContext.ReplaceDialogAsync(nameof(MainDialog))
這實際上將重新啟動您所在的當前對話框MainDialog
您可以根據用途選擇添加一些條件來完成此操作。
但請記住,您應該始終以以下方式結束對話
stepContext.EndDialogAsync();
或者您將陷入替換當前對話框並重新開始的無限循環中,因此在您的情況下EndDialogAsync()應該僅在用戶選擇結束按鈕時才出現,否則您需要ReplaceDialogAsync()重新啟動它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.