public IForm <JObject> BuildJsonForm()
    {
        var schema = JObject.Parse(FormJson);
        var form   = new FormBuilderJson(schema)
                     .AddRemainingFields()
                     .Build();

        return(form);
    }
Exemplo n.º 2
0
        public static IForm <JObject> BuildJsonFormExplicit()
        {
            List <BotCaseStatus> statusList = new List <BotCaseStatus>();

            statusList = GetStatusList().Result.ToList();
            string jsonString = "{\"References\": [ \"Microsoft.Bot.Sample.AnnotatedSandwichBot.dll\"],\"Imports\": [ \"Microsoft.Bot.Sample.AnnotatedSandwichBot.Resource\"],\"type\": \"object\",\"required\": [\"Length\" ],\"Templates\": {\"NotUnderstood\": {\"Patterns\": [ \"I do not understand , Try again, I don't get\" ]},\"EnumSelectOne\": {\"Patterns\": [ \"Choose Status? {||}\" ],\"ChoiceStyle\": \"Auto\"}},\"properties\": {\"Length\": {\"Prompt\": {\"Patterns\": [ \"What size of sandwich do you want? {||}\" ]},\"type\": [\"string\",\"null\"],\"enum\": [";
            int    length     = statusList.Count();

            for (int i = 0; i < length; i++)
            {
                jsonString += "\"" + statusList[i].Code + "\"";
                if ((i + 1) == length)
                {
                    continue;
                }
                jsonString += ",";
            }
            jsonString += "]} }}";
            var schema = JObject.Parse(jsonString);
            OnCompletionAsyncDelegate <JObject> processOrder = async(context, state) =>
            {
            };
            var builder = new FormBuilderJson(schema);

            return(builder
                   .Message("Welcome to the sandwich order bot")
                   .Field("Length")
                   .Confirm(async(state) =>
            {
                return new PromptAttribute("");
            })
                   .AddRemainingFields()
                   .Message("Thanks for ordering a sandwich!")
                   .OnCompletion(processOrder)
                   .Build());
        }
Exemplo n.º 3
0
        public static IForm <JObject> BuildJsonFormExplicit()
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Bot.Sample.AnnotatedSandwichBot.AnnotatedSandwich.json"))
            {
                var schema = JObject.Parse(new StreamReader(stream).ReadToEnd());
                OnCompletionAsyncDelegate <JObject> processOrder = async(context, state) =>
                {
                    await context.PostAsync(new Activity()
                    {
                        Type = ActivityTypes.Message, Text = DynamicSandwich.Processing
                    });
                };
                var builder = new FormBuilderJson(schema);
                return(builder
                       .Message("Welcome to the sandwich order bot!")
                       .Field("Sandwich")
                       .Field("Length")
                       .Field("Ingredients.Bread")
                       .Field("Ingredients.Cheese")
                       .Field("Ingredients.Toppings",
                              validate: async(state, response) =>
                {
                    var value = (IList <object>)response;
                    var result = new ValidateResult()
                    {
                        IsValid = true
                    };
                    if (value != null && value.Contains("Everything"))
                    {
                        result.Value = (from topping in new string[] {
                            "Avocado", "BananaPeppers", "Cucumbers", "GreenBellPeppers",
                            "Jalapenos", "Lettuce", "Olives", "Pickles",
                            "RedOnion", "Spinach", "Tomatoes"
                        }
                                        where !value.Contains(topping)
                                        select topping).ToList();
                    }
                    else
                    {
                        result.Value = value;
                    }
                    return result;
                }
                              )
                       .Message("For sandwich toppings you have selected {Ingredients.Toppings}.")
                       .Field("Ingredients.Sauces")
                       .Field(new FieldJson(builder, "Specials")
                              .SetType(null)
                              .SetActive((state) => (string)state["Length"] == "FootLong")
                              .SetDefine(async(state, field) =>
                {
                    field
                    .AddDescription("cookie", DynamicSandwich.FreeCookie)
                    .AddTerms("cookie", Language.GenerateTerms(DynamicSandwich.FreeCookie, 2))
                    .AddDescription("drink", DynamicSandwich.FreeDrink)
                    .AddTerms("drink", Language.GenerateTerms(DynamicSandwich.FreeDrink, 2));
                    return true;
                }))
                       .Confirm(async(state) =>
                {
                    var cost = 0.0;
                    switch ((string)state["Length"])
                    {
                    case "SixInch": cost = 5.0; break;

                    case "FootLong": cost = 6.50; break;
                    }
                    return new PromptAttribute(string.Format(DynamicSandwich.Cost, cost));
                })
                       .Field("DeliveryAddress",
                              validate: async(state, value) =>
                {
                    var result = new ValidateResult {
                        IsValid = true, Value = value
                    };
                    var address = (value as string).Trim();
                    if (address.Length > 0 && (address[0] < '0' || address[0] > '9'))
                    {
                        result.Feedback = DynamicSandwich.BadAddress;
                        result.IsValid = false;
                    }
                    return result;
                })
                       .Field("DeliveryTime", "What time do you want your sandwich delivered? {||}")
                       .Confirm("Do you want to order your {Length} {Sandwich} on {Ingredients.Bread} {&Ingredients.Bread} with {[{Ingredients.Cheese} {Ingredients.Toppings} {Ingredients.Sauces}]} to be sent to {DeliveryAddress} {?at {DeliveryTime:t}}?")
                       .AddRemainingFields()
                       .Message("Thanks for ordering a sandwich!")
                       .OnCompletion(processOrder)
                       .Build());
            }
        }
Exemplo n.º 4
0
        public static IForm <JObject> BuildForm()
        {
            OnCompletionAsyncDelegate <JObject> processResult = async(context, state) =>
            {
                bool showSource = false;
                var  responses  = new Dictionary <string, string>();

                // Iterate the responses and do what you like here
                foreach (JProperty item in (JToken)state)
                {
                    responses.Add(item.Name, item.Value.ToString());

                    // Here we're only interested in the final answer to determine response
                    if (item.Name.Equals("Question10"))
                    {
                        // Display the repo url only if requested
                        if ((bool)item.Value == true)
                        {
                            showSource = true;
                        }
                    }
                }

                var msg = context.MakeMessage();
                if (showSource)
                {
                    string url = ConfigurationManager.AppSettings["repo_url"];
                    msg.Text = "Great, hope you find this sample code useful: " + url;
                }
                else
                {
                    msg.Text = "Enjoy building your bot!";
                }

                await context.PostAsync(msg);

                // Wang in your own telemetry here
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.TrackEvent("BotSample", responses);
            };

            // The Questions.json file is an embedded resource that we can simply read with a stream
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BotSample.Questions.json"))
            {
                var schema = JObject.Parse(new StreamReader(stream).ReadToEnd());

                // The FormBuilder will manage where we are in the form flow and ask each subsequent question as they get answered
                var form = new FormBuilderJson(schema)
                           // This is a bot, so let's be friendly
                           .Message(new PromptAttribute("Hey party people, I'm a (MS Bot Framework) Sample Bot - here to help you build your own me."))

                           //Questions not yet answered
                           .AddRemainingFields()
                           .Message("Thanks for sticking with me, processing responses..")

                           //Callback once user has finished all the questions so we can process the result
                           .OnCompletion(processResult)
                           .Build();

                return(form);
            }
        }
 public static IForm<JObject> BuildJsonFormExplicit()
 {
     using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Bot.Sample.AnnotatedSandwichBot.AnnotatedSandwich.json"))
     {
         var schema = JObject.Parse(new StreamReader(stream).ReadToEnd());
         OnCompletionAsyncDelegate<JObject> processOrder = async (context, state) =>
         {
             await context.PostAsync(DynamicSandwich.Processing);
         };
         var builder = new FormBuilderJson(schema);
         return builder
                     .Message("Welcome to the sandwich order bot!")
                     .Field("Sandwich")
                     .Field("Length")
                     .Field("Ingredients.Bread")
                     .Field("Ingredients.Cheese")
                     .Field("Ingredients.Toppings",
                     validate: async (state, response) =>
                     {
                         var value = (IList<object>)response;
                         var result = new ValidateResult() { IsValid = true };
                         if (value != null && value.Contains("Everything"))
                         {
                             result.Value = (from topping in new string[] {
                             "Avocado", "BananaPeppers", "Cucumbers", "GreenBellPeppers",
                             "Jalapenos", "Lettuce", "Olives", "Pickles",
                             "RedOnion", "Spinach", "Tomatoes"}
                                             where !value.Contains(topping)
                                             select topping).ToList();
                         }
                         else
                         {
                             result.Value = value;
                         }
                         return result;
                     }
                     )
                     .Message("For sandwich toppings you have selected {Ingredients.Toppings}.")
                     .Field("Ingredients.Sauces")
                     .Field(new FieldJson(builder, "Specials")
                         .SetType(null)
                         .SetActive((state) => (string)state["Length"] == "FootLong")
                         .SetDefine(async (state, field) =>
                         {
                             field
                             .AddDescription("cookie", DynamicSandwich.FreeCookie)
                             .AddTerms("cookie", Language.GenerateTerms(DynamicSandwich.FreeCookie, 2))
                             .AddDescription("drink", DynamicSandwich.FreeDrink)
                             .AddTerms("drink", Language.GenerateTerms(DynamicSandwich.FreeDrink, 2));
                             return true;
                         }))
                     .Confirm(async (state) =>
                     {
                         var cost = 0.0;
                         switch ((string)state["Length"])
                         {
                             case "SixInch": cost = 5.0; break;
                             case "FootLong": cost = 6.50; break;
                         }
                         return new PromptAttribute(string.Format(DynamicSandwich.Cost, cost));
                     })
                     .Field("DeliveryAddress",
                         validate: async (state, value) =>
                         {
                             var result = new ValidateResult { IsValid = true, Value = value };
                             var address = (value as string).Trim();
                             if (address.Length > 0 && (address[0] < '0' || address[0] > '9'))
                             {
                                 result.Feedback = DynamicSandwich.BadAddress;
                                 result.IsValid = false;
                             }
                             return result;
                         })
                     .Field("DeliveryTime", "What time do you want your sandwich delivered? {||}")
                     .Confirm("Do you want to order your {Length} {Sandwich} on {Ingredients.Bread} {&Ingredients.Bread} with {[{Ingredients.Cheese} {Ingredients.Toppings} {Ingredients.Sauces}]} to be sent to {DeliveryAddress} {?at {DeliveryTime:t}}?")
                     .AddRemainingFields()
                     .Message("Thanks for ordering a sandwich!")
                     .OnCompletion(processOrder)
             .Build();
     }
 }