如何在消息控制器之类的对话框之外访问Bot Framework ConversationData? [英] How can I access Bot Framework ConversationData outside of a dialog like in messages controller?

查看:109
本文介绍了如何在消息控制器之类的对话框之外访问Bot Framework ConversationData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的漫游器中的一个对话框中,我将标志值存储在ConversationData中,如下所示:

In a dialog within my bot, I store a flag value in the ConversationData like so:

context.ConversationData.SetValue("SomeFlag", true);

稍后,我需要在我的MessagesController中检查该标志,之前将消息分派到对话框.根据这个先前的问题,我尝试通过中检索ConversationData StateClient像这样:

Later, I need to check that flag in my MessagesController, before the message is dispatched to a dialog. As per this previous question I tried retrieving the ConversationData in via the StateClient like this:

public async Task<HttpResponseMessage> Post([FromBody] Activity incomingMessage)
{
    StateClient stateClient = incomingMessage.GetStateClient();
    BotData userData = await stateClient.BotState.GetConversationDataAsync(message.ChannelId, message.Conversation.Id);
    bool finishedQuote = userData.GetProperty<bool>("SomeFlag");
    //...
    // do conditional logic, then dispatch to a dialog as normal
}

但是,在运行时,userData变量保存一个BotData对象,其中userData.Data为null,并且我无法通过GetProperty检索任何存储的标志.我没有在相关文档中看不到任何内容有助于阐明这个问题-在这里我可能做错了什么?我有误会吗?

However, at runtime, the userData variable holds a BotData object where userData.Data is null, and I'm unable to retrieve any stored flags via GetProperty. I don't see anything in the relevant documentation that helps shed light on this issue - what might I be doing wrong here? Is there something I'm misunderstanding?

推荐答案

以下内容应可满足您的需求:

The following should work for what you need:

if (activity.Type == ActivityTypes.Message)
{

    var message = activity as IMessageActivity;
    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
    {
        var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
        var key = Address.FromActivity(message);

        ConversationReference r = new ConversationReference();
        var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);

        //you can get/set UserData, ConversationData, or PrivateConversationData like below
        //set state data
        userData.SetProperty("key 1", "value1");
        userData.SetProperty("key 2", "value2");
        //get state data
        userData.GetProperty<string>("key 1");
        userData.GetProperty<string>("key 2");

        await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
        await botDataStore.FlushAsync(key, CancellationToken.None);
    }
    await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}

这篇关于如何在消息控制器之类的对话框之外访问Bot Framework ConversationData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆