2016-12-04 13 views
2

私はC#でチャットボットのプロジェクトをボットフレームワークで開始しています。C#bot-frameworkのダイアログからどうやって終了できますか?

私はContosoFlowersの例を選択して、ボットフレームワークの使用について学習します。 AddressDialogでは、ユーザーはアドレスを入力せずにダイアログを入力した後でダイアログを終了できません。

ユーザーが「キャンセル」、「中止」、「B」または「戻る」と応答したときにダイアログを終了するようにコードを更新するにはどうすればよいですか?

namespace ContosoFlowers.BotAssets.Dialogs 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Threading.Tasks; 
    using Extensions; 
    using Microsoft.Bot.Builder.Dialogs; 
    using Microsoft.Bot.Connector; 
    using Properties; 
    using Services; 

    [Serializable] 
    public class AddressDialog : IDialog<string> 
    { 
     private readonly string prompt; 
     private readonly ILocationService locationService; 

     private string currentAddress; 

     public AddressDialog(string prompt, ILocationService locationService) 
     { 
      this.prompt = prompt; 
      this.locationService = locationService; 
     } 

     public async Task StartAsync(IDialogContext context) 
     { 
      await context.PostAsync(this.prompt); 
      context.Wait(this.MessageReceivedAsync); 
     } 

     public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result) 
     { 
      var message = await result; 

      var addresses = await this.locationService.ParseAddressAsync(message.Text); 
      if (addresses.Count() == 0) 
      { 

       await context.PostAsync(Resources.AddressDialog_EnterAddressAgain); 
       context.Wait(this.MessageReceivedAsync); 
      } 
      else if (addresses.Count() == 1) 
      { 
       this.currentAddress = addresses.First(); 
       PromptDialog.Choice(context, this.AfterAddressChoice, new[] { Resources.AddressDialog_Confirm, Resources.AddressDialog_Edit }, this.currentAddress); 
      } 
      else 
      { 
       var reply = context.MakeMessage(); 
       reply.AttachmentLayout = AttachmentLayoutTypes.Carousel; 

       foreach (var address in addresses) 
       { 
        reply.AddHeroCard(Resources.AddressDialog_DidYouMean, address, new[] { new KeyValuePair<string, string>(Resources.AddressDialog_UseThisAddress, address) }); 
       } 

       await context.PostAsync(reply); 
       context.Wait(this.MessageReceivedAsync); 
      } 
     } 

     private async Task AfterAddressChoice(IDialogContext context, IAwaitable<string> result) 
     { 
      try 
      { 
       var choice = await result; 

       if (choice == Resources.AddressDialog_Edit) 
       { 
        await this.StartAsync(context); 
       } 
       else 
       { 
        context.Done(this.currentAddress); 
       } 
      } 
      catch (TooManyAttemptsException) 
      { 
       throw; 
      } 
     } 
    } 

} 
+2

タスクは.NETの一部であり、ボットとは関係ありません。 Googleはタスクのキャンセル、CancellationTokenなどを認識します。ただし、リモートサービスからの応答を待つタスクをキャンセルすると、そのサービスに特定のメッセージは送信されません。それは単に待機をキャンセルします。ダイアログを終了するようにBotサービスに指示するには、適切なメソッドを見つけて呼び出す必要があります。 –

答えて

3

あなただけMessageReceivedAsyncメソッドの先頭でquit条件を処理する必要があります。あなたは

private static IEnumerable<string> cancelTerms = new[] { "Cancel", "Back", "B", "Abort" }; 

のようなものを追加しても、このメソッドを追加することができ、ダイアログの上部に

public static bool IsCancel(string text) 
{ 
    return cancelTerms.Any(t => string.Equals(t, text, StringComparison.CurrentCultureIgnoreCase)); 
} 

メッセージがユーザー一致により送信された場合は、理解するだけですキャンセル条項のいずれか。あなたも少しはより一般的に行くとCancelablePromptChoiceで行われたものと同様のCancelableIDialogを作成することができます

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result) 
{ 
    var message = await result; 

    if (IsCancel(message.Text)) 
    { 
    context.Done<string>(null); 
    } 

    // rest of the code of this method.. 
} 

MessageReceivedAsync方法でのような何かをします。

+0

回答ありがとうございますが、 'context.Done(null);'デバッガに問題があります。 "型引数は使用法から推論することはできません。" @Ezequiel Jadib –

+0

try 'context.Done (null);' –

+0

お手数ですが、ありがとうございます。 –

関連する問題