Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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<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(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();
                // User wants to login, send the message to Facebook Auth Dialog
                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

like image 791
Eiren Avatar asked Jun 27 '16 13:06

Eiren


1 Answers

Here is an example from the BotBuilder:

 public async Task SampleChain_Quiz()
    {
        var quiz = Chain
            .PostToChain()
            .Select(_ => "how many questions?")
            .PostToUser()
            .WaitToBot()
            .Select(m => int.Parse(m.Text))
            .Select(count => Enumerable.Range(0, count).Select(index => Chain.Return($"question {index + 1}?").PostToUser().WaitToBot().Select(m => m.Text)))
            .Fold((l, r) => l + "," + r)
            .Select(answers => "your answers were: " + answers)
            .PostToUser();

        using (var container = Build(Options.ResolveDialogFromContainer))
        {
            var builder = new ContainerBuilder();
            builder
                .RegisterInstance(quiz)
                .As<IDialog<object>>();
            builder.Update(container);

            await AssertScriptAsync(container,
                "hello",
                "how many questions?",
                "3",
                "question 1?",
                "A",
                "question 2?",
                "B",
                "question 3?",
                "C",
                "your answers were: A,B,C"
                );
        }
    }

There is a good discussion on this topic on BotBuilder's github site

like image 63
Lars Avatar answered Nov 12 '22 01:11

Lars