有没有办法从循环内的BotFramework获取用户输入

时间:2016-07-27 19:47:39

标签: c# botframework

这是我的代码:

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);
            }           
        }

我无法想出更好的方法。这似乎是非常糟糕的设计。 缺陷 - &gt;我在第一个if循环中请求名字,但是如果循环,我在第二个输出返回输出。 如果我按照这个对话一段时间,我会有无法管理的代码。有没有更好的方法呢?我正在考虑使用对话框,但只是想把代码放在那里,看看是否有人以更好的方式做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以使用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;            
}

请参阅: Introduction To FormFlow With The Microsoft Bot Framework