2016-12-08 38 views
2

我正在玩Bot框架的例子,並製作了一個簡單的對話框,用來向用戶致敬。我遇到的問題是,在提示輸入用戶名後,恢復方法從不會觸發。它總是返回到ConverstationStartedAsync方法。任何想法爲什麼?Bot框架總是激活對話框的方法

這是對話框:

public class HelloDialog : IDialog<string> 
{ 

    public async Task StartAsync(IDialogContext context) 
    { 
     context.Wait(ConversationStartedAsync); 
    } 

    public async Task ConversationStartedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument) 
    { 
     var message = await argument; 
     PromptDialog.Text(
      context: context, 
      resume: AfterNameInput, 
      prompt: "Hi! what's your name?", 
      retry: "Sorry, I didn't get that."); 

    } 

    public async Task AfterNameInput(IDialogContext context, IAwaitable<string> result) 
    { 
     var name = await result; 
     PromptDialog.Text(context, AfterAskNeed, "Hi {name}. How can I help you?", "Sorry, I didn't get that.", 3); 
    } 

,這是在MessagesController行動:

 public async Task<HttpResponseMessage> Post([FromBody]Activity activity) 
    { 
     if (activity != null) 
     { 
      // one of these will have an interface and process it 
      switch (activity.GetActivityType()) 
      { 
       case ActivityTypes.Message: 
        try 
        { 
         await Conversation.SendAsync(activity,() => new HelloDialog()); 
        } 
        catch(Exception ex) 
        { 

        } 
        break; 

       case ActivityTypes.ConversationUpdate: 
       case ActivityTypes.ContactRelationUpdate: 
       case ActivityTypes.Typing: 
       case ActivityTypes.DeleteUserData: 
       default: 
        break; 
      } 
     } 
     return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted); 
    } 
+0

是'StartConversationDialog'一樣'HelloDialog'? – stuartd

+0

是的。對不起,我會更正代碼。 – Maxolidean

回答

2

我真的不知道發生了什麼,但我可以通過卸載包微軟解決問題.Bot.Builder(v3.0)然後升級到v3.3.3。

1

當我在代碼中犯了一個錯誤,然後修復並重新部署代碼時,發生了這種情況。但是,由於Bot Framework保存狀態,您可以使用舊邏輯卡住。

在Facebook上,您可以通過鍵入/ deleteprofile重新開始清除保存的狀態,並通過創建新對話或關閉/重新打開模擬器來在模擬器中重新啓動。

0

enter image description here正如MSDN表示,(https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-manage-conversation-flow):

對話框生命週期

當調用一個對話時,它將採取 會話控制流。每個新消息都將被對話框 處理,直到它關閉或重定向到另一個對話框。在 C#中,您可以使用context.Wait()指定回調,以在用戶下次發送消息時調用 。要關閉一個對話框並從堆棧中刪除它(從而將用戶發送回棧中的先前對話框 ),請使用context.Done()。您必須使用context.Wait(),context.Fail(),context.Done()或某些重定向(如context.Forward()或context.Call())指令來結束每個對話方法。不會以其中之一結束的對話方法 將導致錯誤(因爲 框架不知道下次用戶 發送消息時要執行的操作)。

的解決方案是正常退出對話框, 看到下面的代碼爲一個完整的例子

[Serializable] 
    public class RootDialog : IDialog<object> 
    { 
      public async Task StartAsync(IDialogContext context) 
      { 
       context.Wait(ConversationStartedAsync); 
      } 

      public async Task ConversationStartedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument) 
      { 
       var message = await argument; 
       PromptDialog.Text(
        context: context, 
        resume: AfterNameInput, 
        prompt: "Hi! what's your name?", 
        retry: "Sorry, I didn't get that."); 

      } 

      public async Task AfterNameInput(IDialogContext context, IAwaitable<string> result) 
      { 
       var name = await result; 
      //Set value in the context, like holding value in ASP.NET Session 
      context.PrivateConversationData.SetValue<string>("Name", name); 

       PromptDialog.Choice(context, this.ResumeAfterTakingGender, new[] { "Male", "Female" }, "Please enter your gender", "I am sorry I didn't get that, try selecting one of the options below", 3); 
      } 

     private async Task ResumeAfterTakingGender(IDialogContext context, IAwaitable<string> result) 
     { 
      string gender = await result; 

      string name = string.Empty; 

      //Get the data from the context 
      context.PrivateConversationData.TryGetValue<string>("Name", out name); 

      await context.PostAsync($"Hi {name} you are a {gender}"); 

      //Gracefully exit the dialog, because its implementing the IDialog<object>, so use 
      context.Done<object>(null); 
     } 
     } 
    }