有没有一种方法可以在循环中从BotFramework获得用户输入
本文关键字:循环 BotFramework 输入 用户 一种 方法 有没有 | 更新日期: 2023-09-27 17:59:01
这是我的代码:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
// Global variables
var boolAskedForFName = false;
var boolAskedForLName = false;
var userLName = string.Empty;
var userFName = string.Empty;
if (activity.Type == ActivityTypes.Message)
{
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
// Get any saved values
var sc = activity.GetStateClient();
var userData = sc.BotState.GetPrivateConversationData(
activity.ChannelId, activity.Conversation.Id, activity.From.Id);
boolAskedForCity = userData.GetProperty<bool>("AskedForUserFName");
boolAskedForName = userData.GetProperty<bool>("AskedForUserLName");
userLName = userData.GetProperty<string>("LastName") ?? "";
userFName = userData.GetProperty<string>("FirstName") ?? "";
if (boolAskedForFName == false)
{
Activity replyToConversation = activity.CreateReply("May i have your first name?");
replyToConversation.Recipient = activity.From;
replyToConversation.Type = "message";
userData.SetProperty<bool>("AskedForUserFName", true);
var reply = await connector.Conversations.ReplyToActivityAsync(replyToConversation);
}
else if (boolAskedForLName == false)
{
userName = activity.Text;
userData.SetProperty<string>("Name", userFName);
var replyToConversation = activity.CreateReply("what about last name?");
replyToConversation.Recipient = activity.From;
replyToConversation.Type = "message";
replyToConversation.Attachments = new List<Attachment>();
userData.SetProperty<bool>("AskedForUserLName", true);
var reply = await connector.Conversations.ReplyToActivityAsync(replyToConversation);
}
else
{
}
// Save BotUserData
sc.BotState.SetPrivateConversationData(
activity.ChannelId, activity.Conversation.Id, activity.From.Id, userData);
}
}
我想不出更好的办法了。这似乎是一个非常糟糕的设计。缺陷->我在第一个if循环中请求名字,但在第二个else-if循环中得到了输出。如果我关注这个对话一段时间,我会有无法管理的代码。有更好的方法吗?我想使用对话框,但只是想把代码放在那里,看看是否有人用更好的方式做到了这一点。
您可以使用FormFlow:
FormFlow类别:
[Serializable]
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("Welcome to the profile bot!")
.OnCompletion(async (context, profileForm) =>
{
// 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
};
控制器:
internal static IDialog<ProfileForm> MakeRootDialog()
{
return Chain.From(() => FormDialog.FromForm(ProfileForm.BuildForm));
}
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
// Detect if this is a Message activity
if (activity.Type == ActivityTypes.Message)
{
// Call our FormFlow by calling MakeRootDialog
await Conversation.SendAsync(activity, MakeRootDialog);
}
else
{
// This was not a Message activity
HandleSystemMessage(activity);
}
// Return response
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
请参阅:使用Microsoft Bot Framework 的FormFlow简介