【问题标题】:How can you quit from dialog in C# bot-framework?如何退出 C# bot-framework 中的对话框?
【发布时间】:2016-12-06 23:39:02
【问题描述】:

我正在使用机器人框架在 C# 中启动一个 ChatBot 项目。

我选择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 的一部分,它们与机器人无关。谷歌用于任务取消、CancellationToken 等。请注意,尽管取消等待远程服务响应的任务确实不会向该服务发送任何特定消息。它只是取消等待。如果您想告诉 Bot 服务终止对话,您需要找到并调用正确的方法。

标签: c# botframework


【解决方案1】:

您只需要在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。

【讨论】:

  • 尝试'context.Done(null);'
猜你喜欢
  • 2017-04-27
  • 2016-09-04
  • 2022-11-14
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2016-10-30
  • 2017-03-03
  • 1970-01-01
相关资源
最近更新 更多