Exemplo n.º 1
0
        public async Task NumberPrompt()
        {
            var activities = TranscriptUtilities.GetFromTestContext(TestContext);


            PromptValidatorEx.PromptValidator <NumberResult <int> > validator = async(ctx, result) =>
            {
                if (result.Value < 0)
                {
                    result.Status = PromptStatus.TooSmall;
                }
                if (result.Value > 100)
                {
                    result.Status = PromptStatus.TooBig;
                }
                await Task.CompletedTask;
            };

            var convState    = new ConversationState(new MemoryStorage());
            var testProperty = convState.CreateProperty <Dictionary <string, object> >("test");

            var adapter = new TestAdapter()
                          .Use(convState);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                if (turnContext.Activity.Type == ActivityTypes.Message)
                {
                    var state  = await testProperty.GetAsync(turnContext, () => new Dictionary <string, object>());
                    var prompt = new NumberPrompt <int>(Culture.English, validator);

                    var dialogCompletion = await prompt.ContinueAsync(turnContext, state);
                    if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                    {
                        await prompt.BeginAsync(turnContext, state,
                                                new PromptOptions
                        {
                            PromptString      = "Enter a number.",
                            RetryPromptString = "You must enter a valid positive number less than 100."
                        });
                    }
                    else if (dialogCompletion.IsCompleted)
                    {
                        var numberResult = (NumberResult <int>)dialogCompletion.Result;
                        await turnContext.SendActivityAsync($"Bot received the number '{numberResult.Value}'.");
                    }
                }
            })
            .Test(activities)
            .StartTestAsync();
        }
Exemplo n.º 2
0
        public async Task TextPrompt()
        {
            var activities = TranscriptUtilities.GetFromTestContext(TestContext);

            PromptValidatorEx.PromptValidator <TextResult> validator = async(ctx, result) =>
            {
                if (result.Value.Length <= 3)
                {
                    result.Status = PromptStatus.TooSmall;
                }
                await Task.CompletedTask;
            };

            var convState    = new ConversationState(new MemoryStorage());
            var testProperty = convState.CreateProperty <Dictionary <string, object> >("test");

            var adapter = new TestAdapter()
                          .Use(convState);

            await new TestFlow(adapter, async(turnContext, cancellationToken) =>
            {
                if (turnContext.Activity.Type == ActivityTypes.Message)
                {
                    var state  = await testProperty.GetAsync(turnContext, () => new Dictionary <string, object>());
                    var prompt = new TextPrompt(validator);

                    var dialogCompletion = await prompt.ContinueAsync(turnContext, state);
                    if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                    {
                        await prompt.BeginAsync(turnContext, state,
                                                new PromptOptions
                        {
                            PromptString      = "Enter some text.",
                            RetryPromptString = "Make sure the text is greater than three characters."
                        });
                    }
                    else if (dialogCompletion.IsCompleted)
                    {
                        var textResult = (TextResult)dialogCompletion.Result;
                        await turnContext.SendActivityAsync($"Bot received the text '{textResult.Value}'.");
                    }
                }
            })
            .Test(activities)
            .StartTestAsync();
        }