Search code examples
c#botframeworkchatbot

How to rebuild chat bot dialog using bot framework


I am working on a multi dialog form flow chat bot using bot framework.

Below is one of my dialog code where I want to get the confirmation from the user and if necessary, needs to alter customer selection/ parameters provided.

below is the code I'm working on

Dialog

    namespace FormBot.Dialogs
{

    [Serializable]
    public class HardwareDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
            await context.PostAsync("Welcome to the Hardware solution helpdesk!");
            var HardwareFormDialog = FormDialog.FromForm(this.BuildHardwareForm, FormOptions.PromptInStart);
            context.Call(HardwareFormDialog, this.ResumeAfterHardwareFormDialog);
        }

        private IForm<HardwareQuery> BuildHardwareForm()
        {
            OnCompletionAsyncDelegate<HardwareQuery> HardwareRequest = async (context, state) =>
            {
                string message = string.Empty;            
                await context.PostAsync($"Ok. {message}.  Once we resolve it; we will get back to you....");
            };

            return new FormBuilder<HardwareQuery>()
                .Field(nameof(HardwareQuery.Hardware))
                .Message($"We are Creating Ticket your request ...")
                .AddRemainingFields()
                .OnCompletion(HardwareRequest)
                .Build();
        }

        private async Task ResumeAfterHardwareFormDialog(IDialogContext context, IAwaitable<HardwareQuery> result)
        {
            try
            {

            }
            catch (FormCanceledException ex)
            {
                string reply;

                if (ex.InnerException == null)
                {
                    reply = "You have canceled the operation. Quitting from the HardwareDialog";
                }
                else
                {
                    reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
                }

                await context.PostAsync(reply);
            }
            finally
            {
                context.Done<object>(null);
            }
        }

        public static IForm<HardwareDialog> BuildForm()
        {
            return new FormBuilder<HardwareDialog>()
                    .Message("Welcome to Service Ticket Bot!")
                    .Build();
        }


    }
}

Query builder

public enum HardwareOptions
    {
        Desktop, KeyBoard, Laptop, Monitor, Mouse, Printer, Scanner, Server, Tablet
    };

    [Serializable]
    public class HardwareQuery
    {

        [Prompt("Choose your {&} ? {||}")]
        public HardwareOptions? Hardware;

        [Prompt("Please enter {&}")]
        [Pattern(Utility.Phone)]
        public string PhoneNumber { get; set; }


        [Prompt("Please enter {&} ")]
        [Pattern(Utility.Email)]
        public string Email { get; set; }

        [Prompt("Please provide your business need / {&} below")]
        public string Justification { get; set; }

        public static IForm<HardwareQuery> BuildForm()
        {
            OnCompletionAsyncDelegate<ServiceTicket> processOrder = async (context, state) =>
            {
                await context.PostAsync($"Once we resolve it; we will get back to you....");
            };

            return new FormBuilder<HardwareQuery>()
                    .Message("Welcome !")
                    .Build();
        }
    }
}

Expected result

Asking for confirmation Updating the result set


Solution

  • To ask for a confirmation of the selected values in FormFlow, you could use the .Confirm() while building the form. In case you want to prompt for a customized message, you could use .Confirm("Do you confirm the chosen hardware - {Hardware} and your email - {Email} and phone number {PhoneNumber}"). For detailed approaches, have a look at the official documentation. Based on the choice of the confirmation, bot framework's form flow will automatically handle the posting in the questions to change and updating the results.

    P.S. On a side note, I am not sure why you are trying to use FormDialog.FromForm and the other unnecessary code, you could simplify the HardwareDialog as follows:

    namespace FormBot.Dialogs
    {
    
        [Serializable]
        public class HardwareDialog : IDialog<object>
        {
            public async Task StartAsync(IDialogContext context)
            {
                await context.PostAsync("Welcome to the Hardware solution helpdesk!");
                var HardwareFormDialog = new FormDialog<HardwareQuery>(new HardwareQuery(), HardwareQuery.BuildForm, FormOptions.PromptInStart);
                context.Call(HardwareFormDialog, this.ResumeAfterHardwareFormDialog);
            }
    
        private async Task ResumeAfterHardwareFormDialog(IDialogContext context, IAwaitable<HardwareQuery> result)
        {
            try
            {
    
            }
            catch (FormCanceledException ex)
            {
                string reply;
    
                if (ex.InnerException == null)
                {
                    reply = "You have canceled the operation. Quitting from the HardwareDialog";
                }
                else
                {
                    reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
                }
    
                await context.PostAsync(reply);
            }
            finally
            {
                context.Done<object>(null);
            }
        }