[Question] How can I add multiple dialogs to a bot?
How can I add multiple dialogs to a bot? Like 2 LUIS dialogs or LUIS dialog with command dialog. In my bot I wanna use a LUIS pre-built application with another one I made, or with a command dialog.
Is this for C# or Node.js?
I'm using Node.Js
Cool... So let me ask this... Are you wanting to chain the dialogs together such that if one model doesn't recognize the intent the other model will be checked? I don't have official support for that yet (it's on my to-do list) but there is a hacky way you could do it now. Fill me in a little more on what you're trying to accomplish.
Yes that's what I'm trying to do. If the pre-built LUIS model doesn't recognize the intent, the other model I made will be checked
Ok... What you need to do is add 2 dialogs, one for each model. In the onDefault() handler for the first dialog you're going to need to call beginDialog() on the second model. That's the easy part. The not so obvious part is that for every handler in the second dialog you're going need to finish up with a call to endDialog() to avoid the user being stuck in that dialog. I'll cook up a quick example.
bot.add('/', new builder.LuisDialog('pre-built model')
.onDefault('/extensions'));
bot.add('/extensions', new builder.LuisDialog('extension model')
.on('MyExtension', function (session) {
session.endDialog('The extension was triggered!');
})
.onDefault(builder.DialogAction.endDialog()));
Worked perfectly, thank you :) I wanna ask, in the inner dialogs do I have always to use session.endDialog() ?
If you don't the user will get stuck meaning that only your extension model will be evaluated. You're free to setup a waterfall or even call other dialogs from the inner handlers you just need to make sure they always finish with a call to endDialog().
Thank you so much :)
I should also add in case its not obvious... If you have a default "I didn't understand" message you send you'll need to do that from the extension dialog. Just make sure you pass the message in a call to endDialog().
Ok, so it'll be like myDialog.onDefault(builder.DialogAction.endDialog('I didnt understand'));
Unfortunately DialogAction.endDialog() doesn't take a message. A bit of an oversight on my part. Also going on the to-do list. You'll have to do:
myDialog.onDefault(function (session) {
session.endDialog("I didn't understand");
});
Ok 👍
@Stevenic is there any way to do this in C#? I need to do the same thing.
Re-opening and assigning to shahin
Any update on this?
You can call PostMessage on the IDialogContext as many times as you like. The last message will be sent inline with the http request, and all other messages will be sent directly with the IConnectorClient.
@willportnoy I'm not sure how that is a solution to the discussed question? We're trying to transition from one dialog to another, with the new dialog processing the original message sent by the user. For example, LuisDialog A picks up a message, and then decides to transfer the message to LuisDialog B. So far, the only way I've found to do this in C# is by doing something like this, but I'm not sure if this is advisable:
public class FirstLuisDialog : LuisDialog<IDialog>
{
[LuisIntent("some.first.intent")]
public async Task SomeFirstIntent(IDialogContext context, LuisResult result)
{
var next = new SecondLuisDialog();
var loop = next.Loop();
context.Call(loop, async (ctx, arg) => ctx.Wait(MessageReceived));
}
}
It sounds like you're trying to delegate handling of the original Message from the LuisDialog to a child dialog.
You can override LuisDialog.MessageReceived to save the original Message, then call the base implementation of MessageReceived, and if you choose to delegate handling of that Message to a child dialog, you can instantiate your child dialog with a reference to that saved Message.
I'll try to think of a simpler way - for example, I could possibly imagine a mechanism to forward Messages more directly to child dialogs.
@willportnoy Yes, that's exactly the intent - to forward the original Message to a child Dialog and have it handle that Message as if it were the original recipient.
I tried your suggestion in several ways. First, I tried to send the actual IAwaitable<Message> object to the child Dialog, and then call LuisDialog.MessageReceived(IDialogContext context, IAwaitable<Message> item) with it. However, this caused a runtime exception in Wait.cs, line 331 (it seems that INotifyCompletion.OnCompleted() is not implemented and just throws a NotImplementedException.
I then just went ahead and sent over the actual Message to the child Dialog. This works, however I have to essentially recreate LuisDialog's entire MessageReceived and EnumerateHandlers methods, since MessageReceived can only take an IAwaitable<Message>, and not a plain Message.
However, like I said, when I recreated these methods in my child Dialog, it seems that everything works fine, so I think that it may be a good quick win to have within the base framework.