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

Implement Multiple IDialog on C#

Open fercom opened this issue 10 years ago • 12 comments

I am facing a similar issue like this question but in C#, the bot stucks in the first IDialog that I call, it can be either one but after it enters one it doesn't leave. I choose the dialog with a simple condition:

    if (!IsCommonExpresion(activity.Text))
                    {

                        await Conversation.SendAsync(activity, () => new MemeBotDialog());
                    }
                    else
                    {
                        await Conversation.SendAsync(activity, () => new LocalDialog());
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }

I guess I need to use something like the Node.Js "endDialog()" method but I don't know if it does exist on C#

fercom avatar Oct 03 '16 03:10 fercom

I'm guessing that you could use IDialogContext done method

https://docs.botframework.com/en-us/csharp/builder/sdkreference/de/db4/interface_microsoft_1_1_bot_1_1_builder_1_1_dialogs_1_1_internals_1_1_i_dialog_stack.html#ac925ad5a83a7d931925c575d31eb9575

I just did a small test where I used Done instead of wait, and it seemingly returned to my default response without crashing.

I have not tested with multiple dialogs though.

erlendw avatar Oct 03 '16 11:10 erlendw

@erlendw i have tryied it, it didn't work

fercom avatar Oct 03 '16 12:10 fercom

I made it work by doing context.Reset();, after the point in which I know I wanted to finish the dialog. That resets the dialog stack. The main problem about this is that it resets the bots state, so if you were storing some state that was important to you, you will lose it. In my case, that wasn't needed so it worked fine for me.

However, this approach could be helpful for you : Terminate all dialogs in ms bot (Check the first answer, is very neat and I think it does what you need)

Let me know if that works for you!

piffarettig avatar Nov 01 '16 14:11 piffarettig

@piffarettig It doesn't work for my bot, it saves data that I cannot lose by resetting the context. Thank you

fercom avatar Nov 01 '16 15:11 fercom

@msft-shahins or @willportnoy can you comment on this with respect to the new stack manipulation stuff?

chrimc62 avatar Nov 26 '16 23:11 chrimc62

@fercom I would use a root dialog to dispatch between the MemebotDialog and LocalDialog dialogs instead of trying to do it outside the Conversation.SendAsync.

willportnoy avatar Nov 27 '16 01:11 willportnoy

@willportnoy I already tried this, but everytime it enters a dialog, even from the message controller, it doesn't allows to enter a diferent dialog, it keeps stuck in the first dialog it enters

fercom avatar Nov 27 '16 18:11 fercom

Yes, because you can only choose one root dialog, and that single root dialog will get restarted when it finishes automatically. Since the dialog is stateful, you cannot just switch root dialogs on every incoming message.

willportnoy avatar Nov 27 '16 19:11 willportnoy

@willportnoy Okay, so, how do I dispatch one dialog from another one?

fercom avatar Nov 27 '16 20:11 fercom

Use IDialogStack.Call off the context argument passed to the dialog methods.

willportnoy avatar Nov 27 '16 20:11 willportnoy

@fercom here is an example on how you can develop a multi dialog bot and use context.Call(...) to call into different child dialogs: https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-MultiDialogs.

As @willportnoy mentioned you cannot dynamically switch between root dialogs since dialog system is stateful. If you really want to switch to another root dialog, you can reset the dialog stack and send the message to the new root. Here is an example showing how you can achieve this:

async Task ResetStackAndSend(Activity activity, Func<IDialog<object>> makeRoot, CancellationToken token)
{
	using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
	{
		var botData = scope.Resolve<IBotData>();
		await botData.LoadAsync(token);
		scope.Resolve<IDialogStack>().Reset();
		DialogModule_MakeRoot.Register(scope, makeRoot);
		using (new LocalizedScope(activity.Locale))
		{
			await scope.Resolve<IPostToBot>().PostAsync(activity, token);
		}
	}
}

msft-shahins avatar Nov 29 '16 01:11 msft-shahins

@msft-shahins Ok, I will check it out, thank you!

fercom avatar Nov 29 '16 01:11 fercom