本文介绍了如何在不使用TurnContext对象的情况下管理和存储TurnState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,需要TurnContext对象才能从Cosmos获取基础的Bot状态并将其保存回--

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?

推荐答案

访问bot状态并将消息发送给用户所需的所有信息都在对话参考中.您可以使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:41