2016-12-08 8 views
2

私はボットフレームワークの例を試していて、ユーザーに敬意を表したい簡単なダイアログを作っています。私が抱えている問題は、ユーザー名の入力を求められた後、再開メソッドが実行されないことです。常にConverstationStartedAsyncメソッドに戻ります。どんな考え? ボットフレームワークは常にダイアログのsamesメソッドを起動します

この

はダイアログです:

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

以下のコードは '' HelloDialog'としてStartConversationDialog'同じですか?見ますか – stuartd

+0

はい、そうです。申し訳ありませんが、コードを修正します。 – Maxolidean

答えて

2

私は本当に何が起こったのかわからないが、私はマイクロソフトのパッケージをアンインストールすることによってそれを解決できます.Bot.Builder(v3.0)を開き、次にv3.3.3にアップグレードします。

1

これは私がコードを間違えてコードを修正して再デプロイしたときに起こったことです。しかし、Botフレームワークは状態を保存するので、古いロジックを使用して立ち往生することができます。

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); 
     } 
     } 
    } 
関連する問題