Botdata.SetProperty 不保存其值
Botdata.SetProperty does not save its value
我正在使用 formflow,我试图设置一个 属性 来了解用户是否已经收到问候,它设置数据没有任何问题 (userData.SetProperty( "Greet", 真);),
我通常在设置后检查 属性,但是当你试图在下一个 运行(userData.GetProperty("Greet")), 似乎没有保存。我需要这个,以便在用户受到欢迎后,它将退出表单流程并已经尝试 qnadialog。
MessageController
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
//ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
//await Conversation.SendAsync(activity, () => new QnADialog());
#region Formflow
// Get any saved values
StateClient sc = activity.GetStateClient();
BotData userData = sc.BotState.GetPrivateConversationData(
activity.ChannelId, activity.Conversation.Id, activity.From.Id);
var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete");
if (!boolProfileComplete)
{
// Call our FormFlow by calling MakeRootDialog
await Conversation.SendAsync(activity, MakeRootDialog);
}
else
{
//Check if Personalized Greeting is done
if (userData.GetProperty<bool>("Greet"))
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
await Conversation.SendAsync(activity, () => new QnADialog());
}
else
{
// Get the saved profile values
var Name = userData.GetProperty<string>("Name");
userData.SetProperty<bool>("Greet", true);
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity replyMessage = activity.CreateReply(string.Format("Hi {0}!", Name));
await connector.Conversations.ReplyToActivityAsync(replyMessage);
}
}
#endregion
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
FormFlow
[Serializable]
public class ProfileForm
{
// these are the fields that will hold the data
// we will gather with the form
[Prompt("What is your name? {||}")]
public string Name;
// 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("Welcome to the profile bot!")
.OnCompletion(async (context, profileForm) =>
{
// Set BotUserData
context.PrivateConversationData.SetValue<bool>("ProfileComplete", true);
context.PrivateConversationData.SetValue<string>("Name", profileForm.Name);
// Tell the user that the form is complete
await context.PostAsync("Your profile is complete.");
})
.Build();
}
}
在您的控制器中,您首先获取私人对话数据存储,然后将其设置为 Userdata 数据存储。这样,您对私人对话存储的更改永远不会保存在私人对话数据存储中。
改变
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);
至
sc.BotState.SetPrivateConversationData(activity.ChannelId, activity.Conversation.Id, activity.From.Id, userData);
我正在使用 formflow,我试图设置一个 属性 来了解用户是否已经收到问候,它设置数据没有任何问题 (userData.SetProperty( "Greet", 真);), 我通常在设置后检查 属性,但是当你试图在下一个 运行(userData.GetProperty("Greet")), 似乎没有保存。我需要这个,以便在用户受到欢迎后,它将退出表单流程并已经尝试 qnadialog。
MessageController
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
//ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
//await Conversation.SendAsync(activity, () => new QnADialog());
#region Formflow
// Get any saved values
StateClient sc = activity.GetStateClient();
BotData userData = sc.BotState.GetPrivateConversationData(
activity.ChannelId, activity.Conversation.Id, activity.From.Id);
var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete");
if (!boolProfileComplete)
{
// Call our FormFlow by calling MakeRootDialog
await Conversation.SendAsync(activity, MakeRootDialog);
}
else
{
//Check if Personalized Greeting is done
if (userData.GetProperty<bool>("Greet"))
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
await Conversation.SendAsync(activity, () => new QnADialog());
}
else
{
// Get the saved profile values
var Name = userData.GetProperty<string>("Name");
userData.SetProperty<bool>("Greet", true);
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity replyMessage = activity.CreateReply(string.Format("Hi {0}!", Name));
await connector.Conversations.ReplyToActivityAsync(replyMessage);
}
}
#endregion
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
FormFlow
[Serializable]
public class ProfileForm
{
// these are the fields that will hold the data
// we will gather with the form
[Prompt("What is your name? {||}")]
public string Name;
// 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("Welcome to the profile bot!")
.OnCompletion(async (context, profileForm) =>
{
// Set BotUserData
context.PrivateConversationData.SetValue<bool>("ProfileComplete", true);
context.PrivateConversationData.SetValue<string>("Name", profileForm.Name);
// Tell the user that the form is complete
await context.PostAsync("Your profile is complete.");
})
.Build();
}
}
在您的控制器中,您首先获取私人对话数据存储,然后将其设置为 Userdata 数据存储。这样,您对私人对话存储的更改永远不会保存在私人对话数据存储中。
改变
sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData);
至
sc.BotState.SetPrivateConversationData(activity.ChannelId, activity.Conversation.Id, activity.From.Id, userData);