gpt4 book ai didi

c# - 如何在对话框之外(如消息 Controller 中)访问 Bot Framework ConversationData?

转载 作者:行者123 更新时间:2023-12-02 01:48:13 24 4
gpt4 key购买 nike

在我的机器人内的对话框中,我将标志值存储在 ConversationData 中,如下所示:

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

稍后,我需要在将消息分派(dispatch)到对话框之前检查 MessagesController 中的该标志。根据this previous question我尝试通过 StateClient 检索 ConversationData,如下所示:

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,并且我无法通过 检索任何存储的标志获取属性。我在 relevant documentation 中没有看到任何内容这有助于阐明这个问题——我在这里可能做错了什么?难道我有什么误解吗?

最佳答案

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

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

关于c# - 如何在对话框之外(如消息 Controller 中)访问 Bot Framework ConversationData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46085614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com