2016-12-04 6 views
2

봇 프레임 워크를 사용하여 C#의 ChatBot 프로젝트를 시작합니다.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 방법의 상단에서 종료 조건 만 처리하면됩니다. 당신이

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

시도 'context.Done (null);' –

+0

도움이됩니다. 고맙습니다. –

관련 문제