首页 > 解决方案 > 使用 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);
            }


        }

标签: .net-corebotframework

解决方案


我想通了,在 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);
    }

推荐阅读