MS BotFramework 数据保存,BotState

Posted

技术标签:

【中文标题】MS BotFramework 数据保存,BotState【英文标题】:MS BotFramework Data Saving, BotState 【发布时间】:2017-05-05 13:12:38 【问题描述】:

下面是整个代码块,基本上是表单流,我正在尝试将数据保存到 botState。

public class ProfileForm

    // these are the fields that will hold the data
    // we will gather with the form
    [Prompt("What is your first name? ||")]
    public string FirstName;

    [Prompt("What is your last name? ||")]
    public string LastName;

    [Prompt("What is your gender? ||")]
    public Gender Gender;

    // This method 'builds' the form 
    // This method will be called by code we will place
    // in the MakeRootDialog method of the MessagesControlller.cs file

    public static IForm<ProfileForm> BuildForm()
    
        return new FormBuilder<ProfileForm>()

                .Message("Please complete your profile!")
                .OnCompletion(async (context, profileForm) =>

                
                    BotData bt = new BotData();

                    await context.PostAsync("Your profile is complete.\n\n"+profileForm.FirstName+profileForm.LastName+profileForm.Gender);

                    SessionInfo.botUserData.SetProperty<bool>("ProfileComplete", true);
                    SessionInfo.botUserData.SetProperty<string>("FirstName", profileForm.FirstName);
                    SessionInfo.botUserData.SetProperty<string>("LastName", profileForm.LastName);
                    SessionInfo.botUserData.SetProperty<string>("Gender", profileForm.Gender==Gender.Male? "Male" :"Female");

                    await context.PostAsync("Before Saving");

                    await SessionInfo.userStateClient.BotState.SetPrivateConversationDataWithHttpMessagesAsync(
                        SessionInfo.ChannelID, SessionInfo.ConversationID, SessionInfo.FromID, SessionInfo.botUserData);
                    // Tell the user that the form is complete
                    await context.PostAsync("Your profile is complete.");

                )
                .Build();
    

// This enum provides the possible values for the 
// Gender property in the ProfileForm class
// Notice we start the options at 1 
[Serializable]

public enum Gender



    Male = 1, Female = 2

;

我在SetPrivateConversationDataWithHttpMessagesAsync() 方法调用行遇到以下错误,请帮助。


对象引用未设置为对象的实例。

在 Microsoft.Bot.Builder.FormFlow.FormDialog1.d__14.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.d4.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Internals.Fibers.Wait2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d19.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Frame1.&lt;Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop&lt;C&gt;-PollAsync&gt;d__9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Fiber1.<Microsoft-Bot-Builder-Internals-Fibers-IFiberLoop<C>-PollAsync>d16.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Bot.Builder.Internals.Fibers.Wait2.Microsoft.Bot.Builder.Internals.Fibers.IAwaiter&lt;T&gt;.GetResult() at Microsoft.Bot.Builder.Dialogs.Chain.FromDialog1.<ResumeAsync>d3.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.&lt;Rest&gt;d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Wait2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d19.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Frame1.-PollAsync>d9.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Internals.Fibers.Fiber1.-PollAsync>d__16.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Microsoft.Bot.Builder.Internals.Fibers.Wait2.Microsoft.Bot.Builder.Internals.Fibers.IAwaiter.GetResult() 在 Microsoft.Bot.Builder.Dialogs.Chain.LoopDialog1.d__3.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.ThunkResume1.d4.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Internals.Fibers.Wait2.<Microsoft-Bot-Builder-Internals-Fibers-IWait<C>-PollAsync>d19.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Frame1.&lt;Microsoft-Bot-Builder-Internals-Fibers-IFrameLoop&lt;C&gt;-PollAsync&gt;d__9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Internals.Fibers.Fiber1.<Microsoft-Bot-Builder-Internals-Fibers-IFiberLoop<C>-PollAsync>d16.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IDialogStack-PollAsync>d20.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d221.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.ReactiveDialogTask.d31.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.ScoringDialogTask1.d31.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d31.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.&lt;Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync&gt;d__31.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Bot.Builder.Dialogs.Internals.SerializingDialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d41.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.ExceptionTranslationDialogTask.d21.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.LocalizedDialogTask.d__21.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUserTask.d5`1.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 - 在 Microsoft.Bot.Builder.Dialogs.Internals.PostUnhandledExceptionToUserTask.d51.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Internals.LogPostToBot.d__31.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 — 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Conversation.d4.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 - 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 Microsoft.Bot.Builder.Dialogs.Conversation.d2.MoveNext() — 从先前抛出异常的位置结束堆栈跟踪 - 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务 任务)在 LUISBotAppTesting.MessagesController.d__1.MoveNext()

【问题讨论】:

如果您不发布所涉及的整个代码,真的很难知道这里发生了什么。 我已经更新了整个代码块,基本上它在表单流中,我正在尝试将数据保存为机器人状态。更详细地说,我正在使用来自 Facebook Messenger 的代码。 【参考方案1】:

这个问题有点难以理解,但我想提一下,botUserData.SetProperty&lt;&gt;() 函数不保存属性,它只是编辑当前对象的属性。您应该使用 BotState.SetUserData() 并将对象传递给他以保存它。 这是一个保存机器人状态的工作示例:

public static bool saveData(Activity activity, string key, string value)

    StateClient stateClient = activity.GetStateClient();
    BotData userData = stateClient.BotState.GetUserData(activity.ChannelId, activity.From.Id);
    userData.SetProperty<string>(key, value);
    BotData updateResponse = stateClient.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);
    return value == updateResponse.GetProperty<string>(key);

【讨论】:

以上是关于MS BotFramework 数据保存,BotState的主要内容,如果未能解决你的问题,请参考以下文章

Slack 通道不适用于 MS Bot Framework

为啥我的 JSON 数组会变成一个对象?

Facebook Bots 按钮上的“有效负载”选项

如何离线测试 Messenger Bots?本地网络挂钩

BotFramework中的onMembersAdded、onMembersAddedActivity、onTeamsMembersAdded有啥区别?

BotFramework-WebChat - 自适应卡片