botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

recieving item in child Dialog from context.Forward(T item)

Open fingejo opened this issue 9 years ago • 4 comments

var dialog = new EchoDialog();
await context.Forward(dialog, DialogDone, item, CancellationToken.None);

the forward method uses the IPostToBot.PostAsync(T item) method to post an item to the child dialog. But i'm not able to find the item in my new dialog:

async Task IDialog<object>.StartAsync(IDialogContext context)
        {            
            context.Wait(next);
        }

fingejo avatar Jan 20 '17 14:01 fingejo

You can Wait() to get the message. Forward() puts the message on the BotContext and eventually calls StartAsync on the dialog you passed, so technically you could read the message directly off of the IDialogContext, but I recommend doing context.Wait() and then reading the message from the method you pass into the Wait().

public async Task StartAsync(IDialogContext context)
{
    //Since a message is passed along when using IDialogContext.Forward(), read it first
    context.Wait(MessageReceivedAsync);
}

async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> message)
{
    var msg = await message;
    //Do stuff with msg here
}

BenjaBobs avatar Jan 20 '17 15:01 BenjaBobs

that sounds logic, and i also get into MessageReceivedAsync. But when I try to await the message, i get a Exception: Stack is empty [File of type 'text/plain']

I tried to access my Dialog with different types of Dialogs and i also changed the generic Type of IAwaitable, but it is always the same, i cannot acces the message.

fingejo avatar Jan 20 '17 16:01 fingejo

The primary purpose of Forward is to reuse a child dialog that is "reactive", where the child dialog (in StartAsync) waits for a T with some ResumeAfter handler. In particular, if you have a child dialog that waits for a message (e.g. the LuisDialog), you can decide to forward the incoming message already received by some parent dialog by calling LuisDialog.

So it's much more common that T is an IMessageActivity, because almost nobody writes dialogs that wait for other types.

So @fingejo you would "find the item" in the first ResumeAfter handler (e.g. MessageReceived) that is scheduled by the StartAsync.

(most people just use Call directly)

willportnoy avatar Jan 20 '17 17:01 willportnoy

@willportnoy thankyou for your detailed explanation. I was able to figure out my problem with your explanation That is my solution now:

public async Task Bildersuche(IDialogContext context, IAwaitable<IMessageActivity> argument, LuisResult result)
        {
            //BilderDialog.lresult = result;
            var a = await argument;
            a.Text = result.Entities[0].Entity;
            var dialog = new BilderDialog();
            await context.Forward(dialog, DialogDone, a, CancellationToken.None);
            
        }
public async Task bilder(IDialogContext context, IAwaitable<IMessageActivity> argument)
        {
            string b = "";
            var c = await argument;

I am now able to recieve my item. It had to be an IMessageActivity. So i just cloned the information from LuisDialog i needed into it.

fingejo avatar Jan 23 '17 08:01 fingejo