如何使用Bot框架重建聊天Bot对话框

时间:2018-08-28 05:45:10

标签: c# botframework chatbot

我正在使用bot框架开发多对话框表单流聊天bot。

下面是我要获取用户确认的对话代码之一,如有必要,需要更改提供的客户选择/参数。

下面是我正在处理的代码

对话框

    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();
        }


    }
}

查询构建器

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();
        }
    }
}

预期结果

Asking for confirmation Updating the result set

1 个答案:

答案 0 :(得分:0)

要确认在FormFlow中选择的值,可以在构建表单时使用.Confirm()。如果您想提示输入自定义消息,可以使用.Confirm("Do you confirm the chosen hardware - {Hardware} and your email - {Email} and phone number {PhoneNumber}")。有关详细方法,请查看official documentation。根据确认的选择,bot框架的表单流将自动处理问题中的过帐,以更改和更新结果。

PS 附带说明,我不确定为什么要尝试使用FormDialog.FromForm和其他不必要的代码,您可以按如下方式简化HardwareDialog:< / p>

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);
        }
    }