0
votes

I am using the C# SDK of BotBuilder for the BotFramework and want to do the following.

I have a FormFlow Dialog that collects information for reserving a table. One of the items in the FormFlow is asking for Name. In another dialog I am collecting the name and saving it to the userData.

context.UserData.SetValue<string>("Name", userName);

My Formflow looks like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.FormFlow.Advanced;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace DinnerBot.Dialogs
{   
    [Serializable]
    public class ReservationDialog
    {

        public enum SpecialOccasionOptions
        {
            Birthday,
            Anniversary,
            Engagement,
            none
        }

        [Prompt(new string[] { "What is your name?" })]
        public string Name { get; set; }

        [Prompt(new string[] { "What is your email?" })]
        public string Email { get; set; }

        [Pattern(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$")]
        public string PhoneNumber { get; set; }

        [Prompt("What date would you like to dine with us? example: today, tomorrow, or any date like 04-06-2017 {||}", AllowDefault = BoolDefault.True)]
        [Describe("Reservation date, example: today, tomorrow, or any date like 04-06-2017")]
        public DateTime ReservationDate { get; set; }

        public DateTime ReservationTime { get; set; }

        [Prompt("How many people will be joining us?")]
        [Numeric(1, 20)]
        public int? NumberOfDinners;
        public SpecialOccasionOptions? SpecialOccasion;

        [Numeric(1, 5)]
        [Optional]
        [Describe("for how you enjoyed your experience with Dinner Bot today (optional)")]
        public double? Rating;

        public static IForm<ReservationDialog> BuildForm()
        {
            return new FormBuilder<ReservationDialog>()
                .Field(nameof(Name))
                .Field(nameof(Email), validate: ValidateContactInformation)
                .Field(nameof(PhoneNumber))
                .Field(nameof(ReservationDate))
                .Field(new FieldReflector<ReservationDialog>(nameof(ReservationDialog.ReservationTime))
                    .SetPrompt(PerLinePromptAttribute("What time would you like to arrive?"))
                    ).AddRemainingFields()
                .Build();
        }

        private static Task<ValidateResult> ValidateContactInformation(ReservationDialog state, object response)
        {
            var result = new ValidateResult();
            string contactInfo = string.Empty;
            if (GetEmailAddress((string)response, out contactInfo))
            {
                result.IsValid = true;
                result.Value = contactInfo;
            }
            else
            {
                result.IsValid = false;
                result.Feedback = "You did not enter valid email address.";
            }
            return Task.FromResult(result);
        }

        private static bool GetEmailAddress(string response, out string contactInfo)
        {
            contactInfo = string.Empty;
            var match = Regex.Match(response, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
            if (match.Success)
            {
                contactInfo = match.Value;
                return true;
            }
            return false;
        }

        private static PromptAttribute PerLinePromptAttribute(string pattern)
        {
            return new PromptAttribute(pattern)
            {
                ChoiceStyle = ChoiceStyleOptions.PerLine
            };
        }


    }
}

In my root dialog, I call it like this.

                        context.Call(FormDialog.FromForm<ReservationDialog>(ReservationDialog.BuildForm,
                        FormOptions.PromptInStart), this.ReservationFormComplete);

How can I have the formflow skip the name field and take the Name from userData if it exists?

2

2 Answers

0
votes

Define FieldReflector.SetActive for the Name property. For example:

.Field(new FieldReflector<ReservationDialog>(nameof(ReservationDialog.Name))
    .SetActive((state) => SetFieldActive(state, nameof(ReservationDialog.Name)))
    ...

Then in your SetFieldActive delegate check if the name already exists and if so, set the value to the state and return false. Then the prompt won't be shown. If you find no name, return true to display the prompt.

I've written a brief blog post and a sample about this subject you might be interested in.

0
votes

You have some simpler options.
1) Make your fields nullable and if you set them from your saved bot state, they will by default by skipped. (Unless you pass in FormOptions.PromptFieldsWithValues.) 2) Turn your values into EntityRecommendations. If they match fields in your form, they will be assigned and skipped as well.