Example #1
0
        public async Task NumberPrompt_Validator()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <int>(Culture.English, async(ctx, result) =>
                {
                    if (result.Value < 0)
                    {
                        result.Status = RecognitionStatus.TooSmall;
                    }
                    if (result.Value > 100)
                    {
                        result.Status = RecognitionStatus.TooBig;
                    }
                });
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var numberResult = await numberPrompt.Recognize(context);
                    if (numberResult.Succeeded())
                    {
                        Assert.IsInstanceOfType(numberResult.Value, typeof(int));
                        Assert.IsTrue(numberResult.Value < 100);
                        Assert.IsNotNull(numberResult.Text);
                        context.Reply(numberResult.Value.ToString());
                    }
                    else
                    {
                        context.Reply(numberResult.Status.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("asdf df 123")
            .AssertReply(RecognitionStatus.TooBig.ToString())
            .Send(" asdf asd 12 adsfsdf ")
            .AssertReply("12")
            .StartTest();
        }
Example #2
0
        public async Task NumberPrompt_Float()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <float>(Culture.English);
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var numberResult = await numberPrompt.Recognize(context);
                    if (numberResult.Succeeded())
                    {
                        Assert.IsTrue(numberResult.Value != float.NaN);
                        Assert.IsNotNull(numberResult.Text);
                        Assert.IsInstanceOfType(numberResult.Value, typeof(float));
                        context.Reply(numberResult.Value.ToString());
                    }
                    else
                    {
                        context.Reply(numberResult.Status.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("test test test")
            .AssertReply(RecognitionStatus.NotRecognized.ToString())
            .Send("asdf df 123")
            .AssertReply("123")
            .Send(" asdf asd 123.43 adsfsdf ")
            .AssertReply("123.43")
            .StartTest();
        }
        public async Task NumberPrompt_Int()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <TestState>(new MemoryStorage()));

            await new TestFlow(adapter, async(context) =>
            {
                var state        = ConversationState <TestState> .Get(context);
                var numberPrompt = new NumberPrompt <int>(Culture.English);
                if (!state.InPrompt)
                {
                    state.InPrompt = true;
                    await numberPrompt.Prompt(context, "Gimme:");
                }
                else
                {
                    var result = await numberPrompt.Recognize(context);
                    if (result == null)
                    {
                        context.Reply("null");
                    }
                    else
                    {
                        Assert.IsInstanceOfType(result.Value, typeof(int));
                        Assert.IsNotNull(result.Text);
                        context.Reply(result.Value.ToString());
                    }
                }
            })
            .Send("hello")
            .AssertReply("Gimme:")
            .Send("test test test")
            .AssertReply("null")
            .Send("asdf df 123")
            .AssertReply("123")
            .Send(" asdf asd 123.43 adsfsdf ")
            .AssertReply("null")
            .StartTest();
        }
Example #4
0
        public async Task OnTurn(ITurnContext context)
        {
            if (context.Activity.Type == ActivityTypes.Message)
            {
                var state = context.GetConversationState <SimplePromptState>();

                // Name prompt
                if (!state.PromptinName && !state.PromptinAge)
                {
                    // Prompt for Name
                    state.PromptinName = true;
                    await namePrompt.Prompt(context, "Hello! What is your name?");
                }
                else if (state.PromptinName)
                {
                    // Attempt to recognize the user name
                    var name = await namePrompt.Recognize(context);

                    if (!name.Succeeded())
                    {
                        // Not recognized, re-prompt
                        await namePrompt.Prompt(context, "Sorry, I didn't get that. What is your name?");
                    }
                    else
                    {
                        // Save name and set next state
                        state.Name         = name.Value;
                        state.PromptinName = false;
                    }
                }

                // Age Prompt
                if (!string.IsNullOrEmpty(state.Name) && state.Age == 0)
                {
                    // Prompt for age
                    if (!state.PromptinAge)
                    {
                        state.PromptinAge = true;
                        await agePrompt.Prompt(context, $"How old are you, {state.Name}?");
                    }
                    else
                    {
                        var age = await agePrompt.Recognize(context);

                        if (!age.Succeeded())
                        {
                            // Not recognized, re-prompt
                            await agePrompt.Prompt(context, "Sorry, that doesn't look right. Ages 13 to 90 only. What is your age?");
                        }
                        else
                        {
                            // Save age and continue
                            state.Age         = age.Value;
                            state.PromptinAge = false;
                        }
                    }
                }

                // Display provided information (if complete)
                if (!string.IsNullOrEmpty(state.Name) && state.Age != 0)
                {
                    await context.SendActivity($"Hello {state.Name}, You are {state.Age}.");

                    // Reset sample by clearing state
                    state.Name = null;
                    state.Age  = 0;
                }
            }
        }