如何再次从Bot Framework v4获取自适应卡列表?

时间:2019-12-24 05:25:42

标签: c# botframework chatbot

这是带来我的自适应卡的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. 此过程将一直持续到选择“结束”按钮为止。**

1 个答案:

答案 0 :(得分:0)

您可以使用:

stepContext.ReplaceDialogAsync(nameof(MainDialog))

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

但是请记住,您应该始终以

结束对话框
stepContext.EndDialogAsync();

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