首页 > 解决方案 > 如何在不使用 TurnContext 对象的情况下管理和存储 TurnState

问题描述

在以下代码中,需要 TurnContext 对象从 Cosmos 获取底层 Bot State 并将其保存回来 -

//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 的情况下实现相同的方法?

标签: c#.net-corebotframework

解决方案


访问机器人状态和向用户发送消息所需的所有信息都在对话参考中。ContinueConversationAsync您可以使用该方法从保存的对话参考中构建一个回合上下文。您可以在主动消息示例中查看如何执行此操作:

await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));

转弯上下文并不意味着存在于其关联转弯之外。您应该保存对话参考而不是转向上下文。


推荐阅读