简体   繁体   English

如何从上下文传递数据。转发以及如何在子对话框中获取项目

[英]How to pass data from context.Forward and how to get the item in child dialog

I have one main dialog: MainDialog() and I want to send a message to child dialog: ChildDialog() using context.Forward() . 我有一个主对话框: MainDialog() ,我想使用context.Forward()将消息发送给子对话框: ChildDialog() context.Forward()

I want to access the message I sent from MainDialog in StartAsync method of ChildDialog . 我要访问我发送该邮件MainDialogStartAsync的方法ChildDialog The StartAsync method of ChildDialog has Context.Wait(MessageReceivedAsync) . StartAsync的方法ChildDialog具有Context.Wait(MessageReceivedAsync) Based on the message I got from MainDialog (I want to send YES or NO string to childDialog ), I want to take action. 根据我从得到的消息MainDialog (我想YESNO串发送到childDialog ),我要采取行动。

If it is YES , I don't want to wait in ChildDialog and directly pass the message to MessageReceivedAsync handler. 如果为YES ,我不想在ChildDialog等待,直接将消息传递给MessageReceivedAsync处理程序。

How can I do this? 我怎样才能做到这一点? I've looked at After call context.Forward, how to get the item in child dialog , but it is related to LuisDialog . 我看过After call context.Forward,如何在子对话框中获取该项目 ,但它与LuisDialog有关。

While you can't pass data to StartAsync as an argument, there are a few ways you can pass data to your child dialog. 虽然您不能将数据作为参数传递给StartAsync ,但是有几种方法可以将数据传递给子对话框。 Since you have control over your dialog's constructor, you can pass data as an argument to the constructor like this: 由于您可以控制对话框的构造函数,因此可以将数据作为参数传递给构造函数,如下所示:

context.Forward(new ChildDialog(data), MessageReceivedAsync, item, token);

If you store the data in a property or field of your ChildDialog class, you'll be able to access that data in StartAsync . 如果您存储在您的属性或字段中的数据ChildDialog类,您就可以访问该数据StartAsync

You may have noticed context.Forward 's item parameter. 您可能已经注意到context.Forwarditem参数。 That's another way you can pass data to your child dialog, but it won't be available until after StartAsync has run. 这是将数据传递到子对话框的另一种方法,但是直到StartAsync运行之后,它才可用。 That said, you might want to reevaluate the design of your program flow. 也就是说,您可能需要重新评估程序流程的设计。 You say: 你说:

If it is YES , I don't want to wait in ChildDialog and directly pass the message to MessageReceivedAsync handler. 如果为YES ,我不想在ChildDialog等待,直接将消息传递给MessageReceivedAsync处理程序。

If you have context.Wait(MessageReceivedAsync) in your StartAsync method then you already won't wait in ChildDialog . 如果你有context.Wait(MessageReceivedAsync)StartAsync方法,那么你已经不会等待ChildDialog This may be confusing, but even though you're calling context.Wait the message will immediately be passed along to MessageReceivedAsync because context.Forward sends the message to the dialog immediately after it calls StartAsync . 这可能会造成混淆,但是即使您正在调用context.Wait ,消息也会立即传递到MessageReceivedAsync因为context.Forward在调用StartAsync之后立即将消息发送到对话框。

I'm guessing you want your bot to wait until the user sends another message if you pass "NO" to your child dialog. 我猜想如果您将“否”传递给子对话框,则您希望您的机器人等待用户发送另一条消息。 I don't know what you're trying to do with your bot, but I would consider not even starting your child dialog until the next turn in that case. 我不知道您打算如何使用您的漫游器,但在这种情况下,我将考虑直到下一轮才启动子对话框。 If you really want to start the dialog and immediately wait, you might consider having two different ResumeAfter delegates: 如果您确实要启动对话框并立即等待,则可以考虑使用两个不同的ResumeAfter委托:

[Serializable]
public class ChildDialog : IDialog<object>
{
    private string _data;

    public ChildDialog(string data)
    {
        _data = data
    }

    public Task StartAsync(IDialogContext context)
    {
        if (_data.Equals("YES"))
        {
            context.Wait(MessageReceivedAsync);
        }
        else
        {
            context.Wait(WaitAsync);
        }

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        // Respond to a message from the user
    }

    private async Task WaitAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        context.Wait(MessageReceivedAsync);
    }
}

Please have a look at the documentation for more information about dialogs: https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-dialogs?view=azure-bot-service-3.0 请查看文档以获取有关对话框的更多信息: https : //docs.microsoft.com/zh-cn/azure/bot-service/dotnet/bot-builder-dotnet-dialogs? view = azure-bot- service -3.0

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

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