如何在不使用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的情况下实现同样的目的?

访问bot状态和向用户发送消息所需的所有信息都在会话引用中。您可以使用ContinueConversationAsync方法从保存的会话引用中构建turn上下文。您可以在主动消息示例中看到如何做到这一点:

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

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

最新更新