protected IMessageActivity AppendChoices(IMessageActivity prompt, string channelId, IList <Choice> choices, ListStyle style, ChoiceFactoryOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Get base prompt text (if any)
            var text = prompt != null && !string.IsNullOrEmpty(prompt.Text) ? prompt.Text : string.Empty;

            // Create temporary msg
            IMessageActivity msg;

            switch (style)
            {
            case ListStyle.Inline:
                msg = ChoiceFactory.Inline(choices, text, null, options);
                break;

            case ListStyle.List:
                msg = ChoiceFactory.List(choices, text, null, options);
                break;

            case ListStyle.SuggestedAction:
                msg = ChoiceFactory.SuggestedAction(choices, text);
                break;

            case ListStyle.None:
                msg      = Activity.CreateMessageActivity();
                msg.Text = text;
                break;

            default:
                msg = ChoiceFactory.ForChannel(channelId, choices, text, null, options);
                break;
            }

            // Update prompt with text and actions
            if (prompt != null)
            {
                // clone the prompt the set in the options (note ActivityEx has Properties so this is the safest mechanism)
                prompt = JsonConvert.DeserializeObject <Activity>(JsonConvert.SerializeObject(prompt));

                prompt.Text = msg.Text;
                if (msg.SuggestedActions != null && msg.SuggestedActions.Actions != null && msg.SuggestedActions.Actions.Count > 0)
                {
                    prompt.SuggestedActions = msg.SuggestedActions;
                }

                return(prompt);
            }
            else
            {
                msg.InputHint = InputHints.ExpectingInput;
                return(msg);
            }
        }
        public async Task ShouldDefaultToEnglishLocale(string activityLocale)
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            // Create new DialogSet.
            var dialogs = new DialogSet(dialogState);

            dialogs.Add(new ChoicePrompt("ChoicePrompt", defaultLocale: activityLocale));

            var helloLocale = MessageFactory.Text("hello");

            helloLocale.Locale = activityLocale;

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dc.PromptAsync(
                        "ChoicePrompt",
                        new PromptOptions
                    {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "favorite color?", Locale = activityLocale
                        },
                        Choices = _colorChoices,
                    },
                        cancellationToken);
                }
            })
            .Send(helloLocale)
            .AssertReply((activity) =>
            {
                // Use ChoiceFactory to build the expected answer, manually
                var expectedChoices = ChoiceFactory.Inline(_colorChoices, null, null, new ChoiceFactoryOptions()
                {
                    InlineOr        = English.InlineOr,
                    InlineOrMore    = English.InlineOrMore,
                    InlineSeparator = English.Separator,
                }).Text;
                Assert.AreEqual($"favorite color?{expectedChoices}", activity.AsMessageActivity().Text);
            })
            .StartTestAsync();
        }
Esempio n. 3
0
        protected IMessageActivity AppendChoices(IMessageActivity prompt, string channelId, IList <Choice> choices, ListStyle style, ChoiceFactoryOptions options = null)
        {
            // Get base prompt text (if any)
            var text = prompt != null && !string.IsNullOrEmpty(prompt.Text) ? prompt.Text : string.Empty;

            // Create temporary msg
            IMessageActivity msg;

            switch (style)
            {
            case ListStyle.Inline:
                msg = ChoiceFactory.Inline(choices, text, null, options);
                break;

            case ListStyle.List:
                msg = ChoiceFactory.List(choices, text, null, options);
                break;

            case ListStyle.SuggestedAction:
                msg = ChoiceFactory.SuggestedAction(choices, text);
                break;

            case ListStyle.None:
                msg      = Activity.CreateMessageActivity();
                msg.Text = text;
                break;

            default:
                msg = ChoiceFactory.ForChannel(channelId, choices, text, null, options);
                break;
            }

            // Update prompt with text and actions
            if (prompt != null)
            {
                prompt.Text = msg.Text;
                if (msg.SuggestedActions != null && msg.SuggestedActions.Actions != null && msg.SuggestedActions.Actions.Count > 0)
                {
                    prompt.SuggestedActions = msg.SuggestedActions;
                }

                return(prompt);
            }
            else
            {
                msg.InputHint = InputHints.ExpectingInput;
                return(msg);
            }
        }
        public async Task Prompt(ITurnContext context, List <Choice> choices, string prompt = null, string speak = null)
        {
            BotAssert.ContextNotNull(context);
            if (choices == null)
            {
                throw new ArgumentNullException(nameof(choices));
            }

            IMessageActivity msg;

            switch (Style)
            {
            case ListStyle.Inline:
                msg = ChoiceFactory.Inline(choices, prompt, speak, ChoiceOptions);
                break;

            case ListStyle.List:
                msg = ChoiceFactory.List(choices, prompt, speak, ChoiceOptions);
                break;

            case ListStyle.SuggestedAction:
                msg = ChoiceFactory.SuggestedAction(choices, prompt, speak);
                break;

            case ListStyle.None:
                msg       = Activity.CreateMessageActivity();
                msg.Text  = prompt;
                msg.Speak = speak;
                break;

            case ListStyle.Auto:
            default:
                msg = ChoiceFactory.ForChannel(context, choices, prompt, speak, ChoiceOptions);
                break;
            }

            msg.InputHint = InputHints.ExpectingInput;
            await context.SendActivity(msg);
        }
Esempio n. 5
0
        public void ShouldRenderChoicesInline()
        {
            var activity = ChoiceFactory.Inline(colorChoices, "select from:");

            Assert.AreEqual("select from: (1) red, (2) green, or (3) blue", activity.Text);
        }
        public async Task ShouldAcceptAndRecognizeCustomLocaleDict()
        {
            var convoState  = new ConversationState(new MemoryStorage());
            var dialogState = convoState.CreateProperty <DialogState>("dialogState");

            var adapter = new TestAdapter()
                          .Use(new AutoSaveStateMiddleware(convoState));

            // Create new DialogSet.
            var dialogs = new DialogSet(dialogState);

            var culture = new PromptCultureModel()
            {
                InlineOr      = " customOr ",
                InlineOrMore  = " customOrMore ",
                Locale        = "custom-custom",
                Separator     = "customSeparator",
                NoInLanguage  = "customNo",
                YesInLanguage = "customYes",
            };

            var customDict = new Dictionary <string, ChoiceFactoryOptions>()
            {
                { culture.Locale, new ChoiceFactoryOptions(culture.Separator, culture.InlineOr, culture.InlineOrMore, true) },
            };

            dialogs.Add(new ChoicePrompt("ChoicePrompt", customDict, null, culture.Locale));

            var helloLocale = MessageFactory.Text("hello");

            helloLocale.Locale = culture.Locale;

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);

                var results = await dc.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dc.PromptAsync(
                        "ChoicePrompt",
                        new PromptOptions
                    {
                        Prompt = new Activity {
                            Type = ActivityTypes.Message, Text = "favorite color?", Locale = culture.Locale
                        },
                        Choices = _colorChoices,
                    },
                        cancellationToken);
                }
            })
            .Send(helloLocale)
            .AssertReply((activity) =>
            {
                // Use ChoiceFactory to build the expected answer, manually
                var expectedChoices = ChoiceFactory.Inline(_colorChoices, null, null, new ChoiceFactoryOptions()
                {
                    InlineOr        = culture.InlineOr,
                    InlineOrMore    = culture.InlineOrMore,
                    InlineSeparator = culture.Separator,
                }).Text;
                Assert.AreEqual($"favorite color?{expectedChoices}", activity.AsMessageActivity().Text);
            })
            .StartTestAsync();
        }