问题描述
在下面的一段代码中,TurnContext 对象需要从 Cosmos 中获取底层的 Bot State 并将其保存回来 -
In following piece of code, TurnContext object is required to fetch the underlying Bot State from Cosmos and save it back -
//Get the TurnContext from the Dictionary
TurnContextReferences.TryGetValue(sessionStateChangedEventData.SessionId, out ITurnContext turnContext);
if (turnContext != null)
{
var conversationData = await BotStateAccessors
.ConversationStateAccessor
.GetAsync(turnContext, () => new ConversationStateDataModel());
if (!conversationData.LiveAgentChatClosed)
{
conversationData.LiveAgentChatClosed = true;
await BotStateAccessors.ConversationStateAccessor.SetAsync(turnContext, conversationData);
await BotConversationState.SaveChangesAsync(turnContext);
}
}
是否有任何可能的方法可以在不直接使用 TurnContext 的情况下实现相同的目标?
Is there any possible way to achieve same without using TurnContext directly?
推荐答案
访问机器人状态和向用户发送消息所需的所有信息都在对话参考中.您可以使用 ContinueConversationAsync
方法从保存的对话参考中构建转向上下文.您可以在 主动消息示例:
All the information you need to access bot state and send messages to a user is in a conversation reference. You can build a turn context out of a saved conversation reference using the ContinueConversationAsync
method. You can see how to do this in the proactive messages sample:
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
回合上下文并不意味着存在于其相关回合之外.您应该保存对话引用而不是转换上下文.
A turn context is not meant to exist outside of its associated turn. You should be saving conversation references instead of turn contexts.
这篇关于如何在不使用 TurnContext 对象的情况下管理和存储 TurnState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!