大佬教程收集整理的这篇文章主要介绍了BotState 对象保留其他对话的价值,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在 BotState 中保存一个对象,如下所示
protected Readonly BotState ConversationState;
protected Readonly BotState UserState;
public DialogBot(ConversationState conversationState,UserState userStatE)
{
ConversationState = conversationState;
UserState = userState;
}
public overrIDe async Task OnTurnAsync(ITurnContext turnContext,CancellationToken cancellationToken = default)
{
await base.onTurnAsync(turnContext,cancellationToken);
await ConversationState.SaveChangesAsync(turnContext,false,cancellationToken);
await UserState.SaveChangesAsync(turnContext,cancellationToken);
}
protected overrIDe async Task OnmessageActivityAsync(ITurnContext<ImessageActivity> turnContext,CancellationToken cancellationToken)
{
Logger.Loginformation("Running dialog with message Activity.");
var userStateAccessor = _userState.CreateProperty<UserProfile>(nameof(UserProfilE));
var userProfile= await userStateAccessor .GetAsync(turnContext,() => new UserProfile(),cancellationToken);
if(String.IsNullOrEmp(userProfile.UserID))
{
userProfile.ConversationID = turnContext.Activity.Conversation.ID;
userProfile.UserID = turnContext. turnContext.Activity.From.ID;
}
await Dialog.RunAsync(turnContext,ConversationState.CreateProperty<DialogState>(nameof(DialogStatE)),cancellationToken);
}
自部署后首次访问后,上述代码中的 If 条件始终为“假”。即 BotState 对象 UserProfile 保留先前对话的价值。 如果有什么问题,请帮助我
这是设计好的。 UserState 将在所有对话中保留用户的数据。另一方面,ConversationState 仅保留当前对话的数据。 Here is some more information about managing states in Bot Framework.
所以在这里,您的代码是说“如果用户 ID 为空,则保存当前对话 ID 和用户 ID”。一旦你设置了它,除非你删除用户状态对象,否则你永远不会在那个块中结束。我没有看到下游您在哪里使用这些值,但按理说,如果您没有点击 if 块,那么您已经保存了这些值并且不需要需要点击如果阻止。
我不确定您使用这些值的目的,但我会小心在用户状态下保存对话 ID。尤其是此代码的设置方式,当您进入新对话时,对话 ID 不会更新,因此您最终会得到一个过时的值。您可以将其保存在“else”语句中,但是我会质疑为什么您根本需要 if 块(即每轮都重置对话和用户 ID)。
以上是大佬教程为你收集整理的BotState 对象保留其他对话的价值全部内容,希望文章能够帮你解决BotState 对象保留其他对话的价值所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。