【问题标题】:How to retain data in JSON object while switching from one Dialog to other in MS Bot Framework如何在 MS Bot Framework 中从一个对话框切换到另一个对话框时保留 JSON 对象中的数据
【发布时间】:2017-01-03 18:19:18
【问题描述】:

我正在尝试使用 MS Bot Framework 构建一个机器人,并且我有两个对话框(父级和子级)。我从 ParentDialog 中的 web api 获取 JSON 响应并将其映射到数据模型类 WebResults。基于一些特殊活动,我必须切换到 ChildDialog,并且我需要 DataResults.title 字符串。但是在切换到 ChildDialog 之后,所有数据映射都会返回 null。有没有办法在 ChildDialog 中获取 DataResults.title 字符串?

下面是我的 MessageController

public class MessagesController : ApiController
{
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new ParentDialog());

        }
        else
        {
            HandleSystemMessage(activity);
        }

        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;


   }

父对话框

public class ParentDialog : IDialog
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(ActivityReceivedAsync); 
    }


    private async  Task ActivityReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {

        var activity = await result as Activity;

        webApiResponse = await httpClient.GetStringAsync(Uri);
        webApiJsonResponse = JsonConvert.DeserializeObject<WebResults>(webApiResponse);
        DataResult[] infoResult = wikiJsonResponse.query.pages;

    If (activity.Text == "Something Special") 
    {
        await context.Forward(new ChildDialog(), ActivityReceivedAsync, activity, CancellationToken.None);
    } 
    else
    {
        //some handling on inforesult
        await context.PostAsync(replyMessage);
        context.Done(false);
    }

}

子对话框

public class ChildDialog : IDialog
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(ActivityReceivedAsync); 
    }


    private async  Task ActivityReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {

       DataResult data = new DataResult(); //Goes null
       string pageTiltle = data.title;

       // Some handling with pageTitle

       context.Done(true);
   }

数据模型

public class WebResults
{
    public string batchcomplete { get; set; }
    public Query query { get; set; }
}

public class Query
{
    public DataResult[] pages { get; set; }
}

public class DataResult
{
    public string title { get; set; }
    public Coordinate[] coordinates { get; set; }
    public string extract { get; set; }
    public Thumbnail thumbnail { get; set; }
    public string pageimage { get; set; }

}

【问题讨论】:

    标签: c# .net json dialog botframework


    【解决方案1】:

    您遇到的第一个问题是在 ChildDialog 中您正在创建 DataResult 的新实例,这是属性为空的原因。

    另一种方法是将您在 ParentDialog 中构建的 DataResult 数组传递给 ChildDialog。您可以通过构造函数传递它,因此在 context.Forward 行中您可以执行以下操作:

    await context.Forward(new ChildDialog(infoResult), ActivityReceivedAsync, activity, CancellationToken.None);
    

    解决此问题的另一种方法是使用开箱即用的 BotBuilder 状态数据包。 Here 你会找到一个关于如何使用状态包的例子,这里是它们周围的core concepts

    【讨论】:

    • 谢谢,它有效!好像你是唯一一个在这里回答 Bot 框架问题的人 :)
    猜你喜欢
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2015-12-14
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多