【问题标题】:How can I get an adaptive card list from Bot Framework v4 again?如何再次从 Bot Framework v4 获取自适应卡片列表?
【发布时间】:2019-12-24 15:17:45
【问题描述】:

这是自带自适应卡的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.卡片按钮和卡片按钮被列出。

  1. 选择其中一个按钮以显示所选类型的卡片。

  2. 显示卡片,同时再次发送卡片重选评论和卡片列表。

  3. 这会一直持续到选择结束按钮为止。**

【问题讨论】:

  • 您的问题解决了吗?

标签: c# botframework chatbot


【解决方案1】:

你可以使用:

stepContext.ReplaceDialogAsync(nameof(MainDialog))

这实际上将重新启动您所在的当前对话框 MainDialog
您可以根据用途选择添加一些条件来做到这一点。

但请记住,您应该始终以

结束对话
stepContext.EndDialogAsync();

否则您将陷入替换当前对话框并重新开始的无限循环中,因此在您的情况下,EndDialogAsync() 应该仅在用户选择 End 按钮时出现 否则你需要 ReplaceDialogAsync() 重新启动它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2020-10-21
    • 2020-02-14
    • 2020-03-21
    • 2020-11-07
    • 2019-03-31
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多