2016-07-27 12 views
0

Hier ist mein Code zu erhalten:Gibt es eine Möglichkeit Benutzereingabe von BotFramework innerhalb einer Schleife

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

Ich kann nicht mit einer besseren Art und Weise kommen. Das scheint sehr schlecht zu sein. Fehler -> Ich frage nach dem ersten Namen in der ersten if-Schleife, aber ich bekomme den Ausgang zurück in der zweiten else if-Schleife. Wenn ich diesem Gespräch für eine Weile folge, werde ich Code haben, der nicht zu verwalten ist. Gibt es einen besseren Weg, dies zu tun? Ich dachte daran, den Dialog zu benutzen, wollte aber nur Code da draußen veröffentlichen, um zu sehen, ob jemand das besser gemacht hat.

Antwort

0

Sie können eine Formflow verwenden:

Formflow Klasse:

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

Controller:

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

See: Introduction To FormFlow With The Microsoft Bot Framework

+0

Wie werden wir Zustand in diesem retten? Auch, wenn ich eine andere Logik einbinden muss, sagen wir, geben Sie Benutzereingaben von einer API, um Nachrichten auszuwählen, die sie sehen wollen, ich werde nicht in der Lage sein, das mit Formularfluss richtig zu machen? – NoviceMe

+0

Siehe das Ende des Artikels: Einführung in FormFlow mit dem Microsoft Bot Framework (http://aihelpwebsite.com/Blog/EntryId/8/Introduction-To-FormFlow-With-The-Microsoft-Bot-Framework) für ein Beispiel Speichern von Daten. –

Verwandte Themen