如何链接多个对话框?

时间:2016-06-27 13:56:48

标签: c# botframework botbuilder

我正在使用C#Bot Builder处理机器人。

现在,我知道有很多关于如何处理链接对话框的例子。像FacebookAuthDialog或ChainedEchoDialog。

我想做什么:用户必须通过授权对话框,当这样做时,我想立即将用户放入“UserDialog”,在那里他可以使用所有需要他认证的功能。

这是我的代码:

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();

这种作品。我使用

从MessageController调用此对话框
await Conversation.SendAsync(message, () => ManagingDialog.dialog);

但这感觉不对。每次对话结束时我也必须调用两次这样的拨号,因为当用户输入的内容没有任何反应时,因为它只会启动对话框。

我在执行AuthenticationDialog案例后尝试放置另一个ContinueWith,但我无法让它工作。

我真的很感谢一些代码片段的帮助。我完全无能为力。

问候

1 个答案:

答案 0 :(得分:3)

以下是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"
                );
        }
    }

BotBuilder's github site

上就此主题进行了很好的讨论