示例#1
0
        public async Task TextPrompt()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <Dictionary <string, object> >(new MemoryStorage()));

            await new TestFlow(adapter, async(turnContext) =>
            {
                var state  = ConversationState <Dictionary <string, object> > .Get(turnContext);
                var prompt = new TextPrompt();

                var dialogCompletion = await prompt.Continue(turnContext, state);
                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.Begin(turnContext, state, new PromptOptions {
                        PromptString = "Enter some text."
                    });
                }
                else if (dialogCompletion.IsCompleted)
                {
                    var textResult = (TextResult)dialogCompletion.Result;
                    await turnContext.SendActivity($"Bot received the text '{textResult.Value}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter some text.")
            .Send("some text")
            .AssertReply("Bot received the text 'some text'.")
            .StartTest();
        }
示例#2
0
        public async Task OnTurn(ITurnContext context)
        {
            var state = ConversationState <Dictionary <string, object> > .Get(context);

            var prompt  = new TextPrompt();
            var options = new PromptOptions {
                PromptString = "Hello, I'm the demo bot. What is your name?"
            };

            switch (context.Activity.Type)
            {
            case ActivityTypes.ConversationUpdate:
                if (context.Activity.MembersAdded[0].Id != context.Activity.Recipient.Id)
                {
                    await prompt.Begin(context, state, options);
                }
                break;

            case ActivityTypes.Message:
                var dialogCompletion = await prompt.Continue(context, state);

                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.Begin(context, state, options);
                }
                else if (dialogCompletion.IsCompleted)
                {
                    var textResult = (Microsoft.Bot.Builder.Prompts.TextResult)dialogCompletion.Result;
                    await context.SendActivity($"Bot received the text '{textResult.Value}'.");
                }
                break;
            }
        }
示例#3
0
        public async Task TextPromptValidator()
        {
            TestAdapter adapter = new TestAdapter()
                                  .Use(new ConversationState <Dictionary <string, object> >(new MemoryStorage()));

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

            await new TestFlow(adapter, async(turnContext) =>
            {
                var state  = ConversationState <Dictionary <string, object> > .Get(turnContext);
                var prompt = new TextPrompt(validator);

                var dialogCompletion = await prompt.Continue(turnContext, state);
                if (!dialogCompletion.IsActive && !dialogCompletion.IsCompleted)
                {
                    await prompt.Begin(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.SendActivity($"Bot received the text '{textResult.Value}'.");
                }
            })
            .Send("hello")
            .AssertReply("Enter some text.")
            .Send("hi")
            .AssertReply("Make sure the text is greater than three characters.")
            .Send("hello")
            .AssertReply("Bot received the text 'hello'.")
            .StartTest();
        }