Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#bot框架中退出对话框?_C#_Botframework - Fatal编程技术网

如何在C#bot框架中退出对话框?

如何在C#bot框架中退出对话框?,c#,botframework,C#,Botframework,我正在用C#启动一个聊天机器人项目,该项目使用机器人框架 我选择ContosoFlowers示例来学习bot框架的使用。在AddressDialog中,用户在进入对话框后,如果不提供地址,则无法退出该对话框 如何更新代码,以便用户在回答“取消”或“中止”或“B”或“返回”时退出对话框 namespace ContosoFlowers.BotAssets.Dialogs { using System; using System.Collections.Generic; us

我正在用C#启动一个聊天机器人项目,该项目使用机器人框架

我选择
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;
            }
        }
    }

}
namespace ContosoFlowers.BotAssets.Dialogs
{
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用扩展;
使用Microsoft.Bot.Builder.Dialogs;
使用Microsoft.Bot.Connector;
使用性能;
使用服务;
[可序列化]
公共类地址对话框:IDialog
{
私有只读字符串提示符;
专用只读ILocationService定位服务;
私有字符串地址;
公共地址对话框(字符串提示,ILocationService locationService)
{
this.prompt=prompt;
this.locationService=locationService;
}
公共异步任务StartAsync(IDialogContext上下文)
{
wait context.PostAsync(this.prompt);
context.Wait(this.MessageReceivedAsync);
}
公共虚拟异步任务消息ReceivedAsync(IDialogContext上下文,IAwaitable结果)
{
var消息=等待结果;
var addresses=wait this.locationService.ParseAddressAsync(message.Text);
if(addresses.Count()==0)
{
wait context.PostAsync(Resources.AddressDialog\u enterAddress再次);
context.Wait(this.MessageReceivedAsync);
}
else if(addresses.Count()==1)
{
this.currentAddress=地址.First();
PromptDialog.Choice(上下文,this.AfterAddressChoice,新建[]{Resources.AddressDialog\u Confirm,Resources.AddressDialog\u Edit},this.currentAddress);
}
其他的
{
var reply=context.MakeMessage();
reply.AttachmentLayout=attachmentlayoututypes.Carousel;
foreach(地址中的变量地址)
{
reply.AddHeroCard(Resources.AddressDialog_didYou的意思是,地址,new[]{new KeyValuePair(Resources.AddressDialog_UseThisAddress,address)});
}
等待上下文。PostAsync(回复);
context.Wait(this.MessageReceivedAsync);
}
}
私有异步任务AfterAddressChoice(IDialogContext上下文,IAwaitable结果)
{
尝试
{
var选择=等待结果;
if(choice==Resources.AddressDialog\u Edit)
{
等待这个。StartAsync(上下文);
}
其他的
{
context.Done(此.currentAddress);
}
}
捕获(ToomanyTestSexception)
{
投
}
}
}
}

您只需要处理
MessageReceivedAsync
方法顶部的退出条件

在对话框顶部,您可以添加如下内容

private static IEnumerable<string> cancelTerms = new[] { "Cancel", "Back", "B", "Abort" };
那么,这只是理解用户发送的消息是否与任何取消条款匹配的问题。在
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..
}
公共虚拟异步任务消息ReceivedAsync(IDialogContext上下文,IAwaitable结果) { var消息=等待结果; if(IsCancel(message.Text)) { context.Done(null); } //此方法的其余代码。。 }
您还可以更一般地创建一个类似于中所做的CancelableIDialog。

任务是.NET的一部分,它们与机器人程序无关。请注意,取消等待远程服务响应的任务不会向该服务发送任何特定消息。它只是取消了等待。如果要通知Bot服务终止对话框,需要找到并调用适当的方法。请尝试“context.Done(null);”
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..
}