How to Chain multiple Dialogs?
Im working on a bot with the C# Bot Builder.
Now, I know that there are quite a few example of how to deal with chaining dialogs. Like the FacebookAuthDialog or the ChainedEchoDialog.
What i want to do: A user has to go through an Authorization Dialog and when thats done, i want to immediately put the user into the "UserDialog" where he can use all the functions which needed his authentication.
Here is my Code:
`public static readonly IDialog
if (userInfo != null && userInfo.Activated)
return false;
else
return true;
}, (ctx, msg) =>
{
return Chain.ContinueWith(new AuthenticationDialog(),
async (context, res) =>
{
var result = await res;
return Chain.Return($"You successfully activated your account.");
});
}),
new Case<Message, IDialog<string>>((msg) =>
{
var userInfo = new StorageClient().GetUser(msg.From.Id);
if (userInfo != null && userInfo.Activated)
return true;
else
return false;
}, (ctx, msg) =>
{
var service = new LuisService();
return Chain.ContinueWith(new UserDialog(msg, service),
async (context, res) =>
{
return Chain.Return($"");
});
}),
new DefaultCase<Message, IDialog<string>>((ctx, msg) =>
{
return Chain.Return("Something went wrong.");
})
).Unwrap().PostToUser();`
This kind of works. I call this dialog from the MessageController with
await Conversation.SendAsync(message, () => ManagingDialog.dialog);
But this doesnt feel right. I also have to call this dialoge twice everytime a dialog finished, because when the user enters something nothing happens since that only starts the dialog.
I tried to put another ContinueWith after the Execution of the AuthenticationDialog Case, but i couldnt get it to work.
I'd really appreciate some help with maybe some code snippets. Im completely clueless.
Greetings
I'm not quite sure you're using the ContinueWith extension method correctly - generally, you chain it to an existing dialog like this
https://github.com/Microsoft/BotBuilder/blob/f2808cd6c752e9f48594a58b0566bd6e4080127b/CSharp/Samples/EchoBot/EchoChainDialog.cs#L23
with manually instantiating a dialog to pass to the ContinueWith method.
there are two chain samples here that might be helpful:
https://github.com/Microsoft/BotBuilder/blob/develop/CSharp/Tests/Microsoft.Bot.Builder.Tests/ChainTests.cs#L380
https://github.com/Microsoft/BotBuilder/blob/develop/CSharp/Tests/Microsoft.Bot.Builder.Tests/ChainTests.cs#L343
Thanks for your Reply. I dont have the code in front of me so i cant fiddle around with it right now. Are you saying i should do something like this?
return Chain.ContinueWith(new AuthenticationDialog(),
async (context, res) =>
{
var result = await res; // Res is of Type Message
return Chain.Return(result);
}).ContinueWith<Message, string>(async (ctx, res2) =>
{
Message result2 = await res2;
// Call UserDialog. But How?
return Chain.Return("Dialog ended successfully!");
});
I think when i tried something like that, i didnt work. But im not sure. Do you think that would work and furthermore, do you know the proper way to call the UserDialog ?
I'd really appreciate your help :)
Greetings
I don't completely understand your dialogs, but this is an example of ContinueWith:
new AuthenticationDialog() .ContinueWith(async (context, authResult) => new UserDialog(await authResult))
If you want to call a series of dialogs, you can use the linq query syntax as well:
https://github.com/Microsoft/BotBuilder/blob/develop/CSharp/Tests/Microsoft.Bot.Builder.Tests/ChainTests.cs#L294
Basically i want the user to be in an authentication dialog, where he does some stuff. Once thats done, i want to pass kind of a dummy message to the "Main" Dialog, the user dialog where he can interact with a system using LUIS. I wanna pass a dummy message, because for some reason the user would have to send a message to initiate the "UserDialog" and nothing happens. Only the next message is forwarded to the actual MessageReceived Method of the dialog. And i want to make the transition between the 2 dialogs without the user having to type 2 messages. My solution might seem a bit silly and hacked, but i dont really know how to solve it properly.
I'll try out your suggestion tomorrow and tell you, whether it works or not.
Thanks for your help!
You shouldn't need to have the user send another message to transition to another dialog - you can simply context.Call that child dialog and let it do what it wants (for example, Posting a message to the user).
So I've tried a couple of your suggestions but i can't get it to work: Approach 1:
public static readonly IDialog<string> dialog = Chain
.PostToChain()
.Switch(
new Case<Message, IDialog<string>>((msg) =>
{
var userInfo = new StorageClient().GetUser(msg.From.Id);
if (userInfo != null && userInfo.Activated)
return false;
else
return true;
}, (ctx, msg) =>
{
return Chain.ContinueWith<object,string>(new AuthenticationDialog(),async (context, authResult) => new UserDialog(await authResult));
}),
new DefaultCase<Message, IDialog<string>>((ctx, msg) =>
{
return Chain.Return("Something went wrong.");
})
).Unwrap().PostToUser();
With that approach i get an Exception:
"exceptionMessage": "Value cannot be null.\r\nParameter name: wait",
"exceptionType": "System.ArgumentNullException",
after i call Context.Dont
Do you have any idea what would be the cause of this? The code doesnt even reach the constructor of the UserDialog.
Approach 2: I dont really know where you want me to call context.Call. The "context" in the ContinueWith block is of Type IBotContext, which doesnt offer the call function. Only the IDialogContext implements that function. I tried a way around with returning the DialogContext from the AuthenticationDialog and then use call:
return Chain.ContinueWith(new AuthenticationDialog(),
async (context, res) =>
{
var result = (IDialogContext)await res;
result.Call(new UserDialog(), (context2, res2) =>
{
return resumeTest();
});
return Chain.Return($"You successfully activated your account.");
});
public async static Task<string> resumeTest()
{
return "Complete";
}
Doing that, the code at least reaches the constructor of the UserDialog but for some reason instantly jumps into the resumeTest() function and then i get the Exception:
"exceptionMessage": "invalid next: Wait: Poll Object for ThunkStart.Rest have Object ",
That might be, because i return it with context.Done
Approach 3: I tried using the Linq stuff:
//In the MessageController:
return await Conversation.SendAsync(message, () => ManagingDialog.asdf());
public static IDialog<string> asdf()
{
var query = from x in new AuthenticationDialog()
from y in new UserDialog()
select y.ToString();
return query = query.PostToUser();
}
Then i get the Exception: "exceptionMessage": "Type 'Microsoft.Bot.Builder.Dialogs.Internals.DialogContext' in Assembly 'Microsoft.Bot.Builder, Version=1.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.",
Please dont lose hope with me =/ I really need to get that working.
Im pretty sure a big problem is that i dont understand all the Lambda-Functions etc.
Hello! I finally got it to work:
return Chain.ContinueWith(new AuthenticationDialog(),
async (context, res) =>
{
await context.PostAsync("Successfull authentication!");
var result = (Message)await res;
return Chain.ContinueWith(new UserDialog(result),
async (context2, res2) =>
{
var result2 = await res2;
return Chain.Return($"You successfully did your things.");
});
});
That works. If you see a huge mistake somewhere or you know a much better approach, please let me know.
But huge thanksf or your kind help!
I'm happy to hear you got it working - I'm going to close this issue, but feel free to open a new one.
Here are some answers to your questions for future searchers:
approach 1: may have been caused by storing IDialogContext and using outside of the method call it's provided to approach 2: IBotContext is a limited form of IDialogContext, because in ContinueWith methods you can't directly manipulate the stack approach 3: it seems likely that IDialogContext was stored somewhere - it shouldn't be kept in class member variables.
Can you please explain with some code in proper way? I'm trying to achieve starting a new dialog (form based) from earlier dialog choices.
https://github.com/Microsoft/BotBuilder/blob/develop/CSharp/Tests/Microsoft.Bot.Builder.Tests/ChainTests.cs#L380 has an example of calling different dialogs based on user input.
I'm going to close this issue, but feel free to open a new issue for more discussion.