【问题标题】:How to rebuild chat bot dialog using bot framework如何使用机器人框架重建聊天机器人对话框
【发布时间】:2018-08-28 14:30:23
【问题描述】:

我正在使用机器人框架开发一个多对话框表单流聊天机器人。

以下是我的对话代码之一,我希望在其中获得用户的确认,如有必要,需要更改提供的客户选择/参数。

下面是我正在处理的代码

对话框

    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

【问题讨论】:

    标签: c# botframework chatbot


    【解决方案1】:

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

    P.S.顺便说一句,我不确定您为什么要尝试使用FormDialog.FromForm 和其他不必要的代码,您可以将HardwareDialog 简化如下:

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

    【讨论】:

    • 非常感谢@ashwin,没有.Confirm(),它现在要求确认。刚刚按照您的建议从 Dialog 类中删除了不需要的代码部分。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多