简体   繁体   English

我无法编写context.Forward在我的bot应用程序的async方法中

[英]I couldn't write context.Forward within my async method in my bot application

I create a bot application using C# . 我使用C#创建一个bot应用程序。

I want to design my bot using multiple dialogs. 我想使用多个对话框来设计我的机器人。 In RootDialog.cs , I have written code to navigate different dialogs according to the user's input. RootDialog.cs ,我编写了代码来根据用户的输入导航不同的对话框。

I am using context.Forward for navigating to next dialog. 我正在使用context.Forward导航到下一个对话框。 But it is showing error when using it.Here I have attached my RootDialog.cs . 但是使用它时显示错误。这里我附加了RootDialog.cs

Can anyone help me in solving the problem? 谁能帮助我解决问题?

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Threading;

namespace WebService.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

       public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var message = await result;
            if (message.Text.ToLower().Contains("name")) {
            await context.Forward(new NameDialog(), this.DetailDialog);
                context.Wait(this.MessageReceivedAsync);
            }

            // calculate something for us to return


            // return our reply to the user

            context.Wait(MessageReceivedAsync);
        }
        private async Task DetailDialog(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var message = await result;
            if (message.Text.ToLower().Contains("price"))
            {

             context.Forward(new PriceDialog(), this.QuantityDialog, message, CancellationToken.None);
                context.Wait(this.MessageReceivedAsync);

            }
        }
            private async Task QuantityDialog(IDialogContext context, IAwaitable<IMessageActivity> result) {
            var mess = await result;
            if (mess.Text.ToLower().Contains("Discount"))
            {
                await context.PostAsync("50% discount per each");

            }
        }
        }
    }

The error is to call a child dialog and add it to the top of the stack showed in "context.Forward(new PriceDialog(), this.QuantityDialog, message, CancellationToken.None)" line. 错误是调用一个子对话框并将其添加到“ context.Forward(new PriceDialog(),this.QuantityDialog,message,CancellationToken.None)”行中显示的堆栈的顶部。

The context.Forward is a task like this: context.Forward是一个像这样的任务:

Task Forward<R, T>(IDialog<R> child, ResumeAfter<R> resume, T item, CancellationToken token);

The first parameter child means the child dialog you want to call, not a task. 第一个参数child表示您要调用的子对话框 ,而不是任务。 The second one indicates the method to resume when the child dialog has completed, this method can be implemented inside of the parent dialog. 第二个指示子对话框完成后恢复的方法,该方法可以在父对话框内部实现。 The item parameter should be the message you want to send to the child dialog. item参数应该是您要发送到子对话框的消息。 And the last one is the cancellation token. 最后一个是取消令牌。 And type parameter R represents the type of result expected from the child dialog and in the meanwhile T represents the type of the item sent to child dialog. 类型参数R代表子对话框期望的结果类型,同时T代表发送给子对话框的项目的类型。

So basically, you should be able to call this Forward task exactly according to its constructor. 因此,基本上,您应该能够完全根据其构造函数调用此Forward任务。 Your code await context.Forward(new NameDialog(), this.DetailDialog); 您的代码await context.Forward(new NameDialog(), this.DetailDialog); is not right. 是不正确的。 You may try to modify it like this: 您可以尝试像这样修改它:

context.Forward(new NameDialog(), this.QuantityDialog, message, CancellationToken.None);

By the way, if you want to call your task DetailDialog inside of MessageReceivedAsync , you can directly call it like this: 顺便说一句,如果要在MessageReceivedAsync内部调用任务DetailDialog ,则可以像这样直接调用它:

await DetailDialog(context, result);  

For more information, you can refer to Invoke the 'New Order' dialog . 有关更多信息,您可以参考调用“新订单”对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM