봇 프레임 워크를 사용하여 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;
}
}
}
}
작업은 .NET의 일부이므로 봇과 아무 관련이 없습니다. Google은 작업 취소, CancellationToken 등을 알고 있습니다. 원격 서비스의 응답을 기다리는 작업 취소는 특정 메시지를 해당 서비스로 전송하지 않습니다. 단순히 대기를 취소합니다. 대화 상자를 종료하도록 Bot 서비스에 지시하려면 적절한 메소드를 찾아 호출해야합니다. –