2016-12-04 86 views
2

我正在用bot框架在C#中開始一個ChatBot項目。如何退出C#bot-framework中的對話框?

我選擇ContosoFlowers的例子來學習使用bot框架。在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的一部分,它們與機器人無關。谷歌取消任務,CancellationToken等。請注意,取消等待遠程服務響應的任務不會*發送任何特定消息到該服務。它只是取消等待。如果你想告訴Bot服務終止一個對話框,你需要找到並調用正確的方法。 –

回答

3

您只需要處理MessageReceivedAsync方法頂部的退出條件。

在您可以添加類似

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

然後僅僅是一個認識問題,如果該消息由用戶匹配發送任何取消條款。在MessageReceivedAsync方法做這樣的事情:

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

你也可以去一個更通用一些,並創建一個類似於什麼在CancelablePromptChoice做了CancelableIDialog。

+0

感謝您的回答,但'context.Done(null);'對調試器有問題。 「類型參數不能從使用中推斷出來。」 @Ezequiel Jadib –

+0

嘗試'context.Done (null);' –

+0

這很有幫助,謝謝。 –

相關問題