/// <summary>
        /// Decorate a message so it displays options "facebook style"
        /// </summary>
        internal static async Task AddActionsToMessage(Message message, BotFlow flow)
        {
            List <Action> actions = null;

            if (flow.Options != null)
            {
                actions = flow.Options.Select(n => new Action()
                {
                    Title   = n.OptionString,
                    Message = n.Url == null ? n.OptionString : null,
                    Url     = n.Url
                }).ToList();
            }

            var messageString = await flow.GetMessage();

            message.Attachments = new List <Attachment>()
            {
                new Attachment()
                {
                    Text         = messageString,
                    Actions      = actions,
                    ThumbnailUrl = flow.ImageUrl
                }
            };

            message.Text = messageString;
        }
Beispiel #2
0
        public async Task BotFlow_BuildSimpleDataStructure_Verify()
        {
            var myBotFlow =
                BotFlow.DisplayMessage("Hello! Do you want milk?", "http://www.mysite.com/milk.png")
                .WithOption("Yes",
                            BotFlow.DisplayMessage("Here's your milk."))
                .WithOption("No",
                            BotFlow.DisplayMessage("Well, then what do you want?")
                            .WithOption("Cookies",
                                        BotFlow.DisplayMessage("Here are your cookies"))
                            .WithOption("Waffles",
                                        BotFlow.DisplayMessage("Here are your waffles"))
                            .WithOption("Nothing",
                                        BotFlow.DisplayMessage("Sorry, I don't have anything else for breakfast!")))
                .FinishWith("You have been served breakfast!");

            (await myBotFlow.GetMessage()).Should().Be("Hello! Do you want milk?");
            myBotFlow.ImageUrl.Should().Be("http://www.mysite.com/milk.png");
            myBotFlow.CompletionMessage.Should().Be("You have been served breakfast!");
            myBotFlow.Options.Count.Should().Be(2);
            myBotFlow.Options[0].OptionString.Should().Be("Yes");
            myBotFlow.Options[1].OptionString.Should().Be("No");
            myBotFlow.Options[0].NextFlow.Options.Should().BeEmpty();
            (await myBotFlow.Options[0].NextFlow.GetMessage()).Should().Be("Here's your milk.");
            myBotFlow.Options[1].NextFlow.Options.Should().HaveCount(3);
        }
Beispiel #3
0
        public State GetCurrentUserState(UserContext userContext, BotFlow botFlow)
        {
            var stateId = userContext.StateId;

            var state = string.IsNullOrEmpty(stateId) ? default : botFlow.States[stateId];

                        if (state == default)
                        {
                            state = botFlow.States.Values.Single(x => x.IsRoot);
                        }

                        return(state);
        }
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <Message> Post([FromBody] Message message)
        {
            if (message.Type == "Message")
            {
                var wrongAnswerBotFlow = BotFlow.DisplayMessage("Wrong answer!")
                                         .Do(() =>
                {
                    /* Report score to the server */
                });
                var finishedBotFlow = BotFlow.DisplayMessage("Very nice! You got all questions right!")
                                      .Do(() =>
                {
                    /* Report score to the server */
                });

                var thirdQuestionBotFlow = BotFlow.DisplayMessage("Nice answer! Now a tough one: How much is 7 * 7?")
                                           .WithOption("49", finishedBotFlow)
                                           .WithOption("54", wrongAnswerBotFlow)
                                           .WithOption("11", wrongAnswerBotFlow);

                var secondQuestion = BotFlow.DisplayMessage("Now a tough one: How much is 10 / 5?")
                                     .WithOption("2", thirdQuestionBotFlow)
                                     .Do(() =>
                {
                    /* Report score to the server */
                })
                                     .WithOption("5", wrongAnswerBotFlow)
                                     .WithOption("1", wrongAnswerBotFlow);

                var firstQuestion = BotFlow.DisplayMessage("Let's do this! How much is 2 + 1?")
                                    .WithOption("1", wrongAnswerBotFlow)
                                    .WithOption("3", secondQuestion)
                                    .WithOption("4", wrongAnswerBotFlow);

                var botflow = BotFlow.DisplayMessage("This is a small math quiz - are you ready?")
                              .WithOption("Yes", firstQuestion)
                              .WithOption("No", BotFlow.DisplayMessage("Call me when you're ready!"))
                              .FinishWith("Thanks for playing!");

                return(await Conversation.SendAsync(message, () => botflow.BuildDialogChain()));
            }
            else
            {
                return(HandleSystemMessage(message));
            }
        }
        public async Task <Message> Post([FromBody] Message message)
        {
            if (message.Type == "Message")
            {
                BotFlow myBotFlow =
                    BotFlow.DisplayMessage("Hello I am breakfastbot and will do my best to serve you breakfast! Do you want milk?")
                    .WithThumbnail("http://www.mysite.com/milk.png")
                    .WithOption("Yes",
                                BotFlow.DisplayMessage("Here's your milk.")
                                .Do(() => Ordering.OrderMilk()))
                    .WithOption("No",
                                BotFlow.DisplayMessage("Well, then what do you want? ;)")
                                .WithOption("Cookies",
                                            BotFlow.DisplayMessage("Here are your cookies")
                                            .Do(() => Ordering.OrderCookies()))
                                .WithOption("Waffles",
                                            BotFlow.DisplayMessage("Here are your waffles")
                                            .Do(() => Ordering.OrderWaffles()))
                                .WithOption("Nothing",
                                            BotFlow.CalculateMessage(async() =>
                {
                    // Retrieve breakfast from database
                    string top10Breakfasts = await DatabaseLookup.GetTop10Breakfasts();
                    return("Maybe you want some ideas - here are the top 10 breakfasts according to our database: " + top10Breakfasts +
                           ". Do any of these sound good?");
                })
                                            .WithOption("Yes",
                                                        BotFlow.DisplayMessage("Great! Click here to browse to our breakfast webpage to find them.")
                                                        .WithOptionLink("Breakfast website", "http://www.microsoft.com")
                                                        )
                                            .WithOption("No",
                                                        BotFlow.DisplayMessage("You must be kidding - everybody likes breakfast!")
                                                        )
                                            )
                                )
                    .FinishWith("Have a great day!");

                return(await Conversation.SendAsync(message, () => myBotFlow.BuildDialogChain()));
            }

            return(HandleSystemMessage(message));
        }
 public OptionsDialog(BotFlow botflow)
 {
     _botflow = botflow;
 }
Beispiel #7
0
        public async Task <State> GetNextUserStateAsync(UserContext userContext, State lastState, BotFlow botFlow)
        {
            var nextStateId = lastState.DefaultOutput.StateId;

            foreach (var outputCondition in lastState.OutputConditions)
            {
                var matchCondition = await outputCondition.Conditions.AllAsync(async o =>
                {
                    string toCompare = default;

                    switch (o.Source)
                    {
                    case ConditionSource.Input:
                        toCompare = (await _variableService.GetVariableValueAsync("input", userContext)).ToString();
                        break;

                    case ConditionSource.Context:
                        toCompare = await _variableService.ReplaceVariablesInStringAsync(o.Variable, userContext);
                        break;

                    case ConditionSource.Intent:
                        toCompare = (await userContext.NlpResponse)?.Intent?.IntentName;
                        break;

                    case ConditionSource.Entity:
                        toCompare = (await userContext.NlpResponse)?.Entities?[o.Entity];
                        break;
                    }

                    var comparisonType = _comparisonService.GetComparisonType(o.Comparison);

                    switch (comparisonType)
                    {
                    case ComparisonType.Unary:
                        var unaryComparer = _comparisonService.GetUnaryConditionComparator(o.Comparison);
                        return(unaryComparer(toCompare));

                    case ComparisonType.Binary:
                        var binaryComparer = _comparisonService.GetBinaryConditionComparator(o.Comparison);
                        return(o.Values.Any(v => binaryComparer(toCompare, v)));
                    }

                    return(false);
                });

                if (matchCondition)
                {
                    nextStateId = outputCondition.StateId;
                    break;
                }
            }

            return(botFlow.States[nextStateId] ?? botFlow.States.Single(x => x.Value.IsRoot).Value);
        }