【问题标题】:How to create multiple dialogs in bot using MS bot framework so that bot remembers which dialog is in progress如何使用 MS bot 框架在 bot 中创建多个对话框,以便 bot 记住哪个对话框正在进行中
【发布时间】:2016-08-10 03:58:44
【问题描述】:

我正在使用 MS Bot Framework 和 C# 构建一个可以处理 3 个对话框的机器人。每个对话框都是使用 FormDialog 和 FormBuilder 构建的,如下所示:

    internal static IDialog<OrderDialogForm> BuildDialog()
    {
        return Chain.From(() => FormDialog.FromForm(BuildForm));
    }

当您第一次与机器人交谈时,它会让您选择三个对话框之一,例如“填写订单”、“输入您的用户资料”、“获得支持”、

一旦用户选择,例如,“填写订单”,机器人就会启动相应的对话框。

显然,用户应该继续回答对话中的问题,直到对话结束。

但是用户每次发送消息,都会传递给API控制器中的这个方法:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity) 

从这里开始,机器人需要确定三个对话中的哪一个当前正在进行中,然后继续该对话。

如何做到这一点,记住当前正在执行哪个对话框,并且对于来自用户的每条新消息,继续该对话框而不是将用户返回到主屏幕?

我的想法是创建某种全局变量或存储在其他地方的记录,可能在数据库中。该记录将包含该用户现在与机器人进行的当前对话的类型。每次机器人收到消息时,它都会查询数据库以找出用户的最后一次交互是与 OrderDialog 的交互,因此程序代码可以决定继续使用 OrderDialog。但它似乎很慢,并且可能在 Bot Framework 中有某种内置函数来存储有关用户的数据,例如它最后一次与之交互的对话类型。

【问题讨论】:

    标签: c# botframework


    【解决方案1】:

    使用机器人状态服务 https://docs.botframework.com/en-us/csharp/builder/sdkreference/stateapi.html

    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        // Detect if this is a Message activity
        if (activity.Type == ActivityTypes.Message)
        {
            // Get any saved values
            StateClient sc = activity.GetStateClient();
            BotData userData = sc.BotState.GetPrivateConversationData(
                activity.ChannelId, activity.Conversation.Id, activity.From.Id);
            var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete");
            if (!boolProfileComplete)
            {
                // Call our FormFlow by calling MakeRootDialog
                await Conversation.SendAsync(activity, MakeRootDialog);
            }
            else
            {
                // Get the saved profile values
                var FirstName = userData.GetProperty<string>("FirstName");
                var LastName = userData.GetProperty<string>("LastName");
                var Gender = userData.GetProperty<string>("Gender");
                // Tell the user their profile is complete
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("Your profile is complete.\n\n");
                sb.Append(String.Format("FirstName = {0}\n\n", FirstName));
                sb.Append(String.Format("LastName = {0}\n\n", LastName));
                sb.Append(String.Format("Gender = {0}", Gender));
                // Create final reply
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                Activity replyMessage = activity.CreateReply(sb.ToString());
                await connector.Conversations.ReplyToActivityAsync(replyMessage);
            }
        }
        else
        {
            // This was not a Message activity
            HandleSystemMessage(activity);
        }
        // Send response
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }
    

    样本来自: 使用 Microsoft Bot 框架介绍 FormFlow http://aihelpwebsite.com/Blog/EntryId/8/Introduction-To-FormFlow-With-The-Microsoft-Bot-Framework

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多