Microsoft BotFramework:将FormFlow和Dialog与身份验证集成

时间:2016-09-06 15:54:48

标签: c# botframework formflow

我使用Dialog开发了一个带有MS Bot Framework的机器人,并在Twitter上进行了身份验证过程。 (我跟着facebook github项目)

我添加了一个业务逻辑流程,并且阅读了MS文档,我遵循了FormFlow的最佳实践。

现在我有一个带有Twitter身份验证核心的Dialog和带有我BL的FormFlow。 目前,我很难合并Auth流程和BL流程。

我的目标是:

  • 用户连接到僵尸程序并启动身份验证(对话框)
  • 认证后我想启动BL(FormFlow)

您对合并的最佳方式有什么建议吗? 只有在完成身份验证并保持实现分离的情况下,才能运行我的FormFlow吗?

一些代码

这是我的授权对话

 public static readonly IDialog<string> dialog = Chain
            .PostToChain()
            .Switch(
             new Case<IMessageActivity, IDialog<string>>((msg) =>
                {
                    var regex = new Regex("^login", RegexOptions.IgnoreCase);
                    return regex.IsMatch(msg.Text);
                }, (ctx, msg) =>
                {
                    // User wants to login, send the message to Twitter Auth Dialog
                    return Chain.ContinueWith(new SimpleTwitterAuthDialog(msg),
                                async (context, res) =>
                                {
                                    // The Twitter Auth Dialog completed successfully and returend the access token in its results
                                    var token = await res;
                                    var name = await TwitterHelpers.GetTwitterProfileName(token);
                                    context.UserData.SetValue("name", name);



                                      context.PrivateConversationData.SetValue<bool>("isLogged", true);
                                        return Chain.Return($"Your are logged in as: {name}");
                                    });
                    }),

            new DefaultCase<IMessageActivity, IDialog<string>>((ctx, msg) =>
                {

                    string token;
                    string name = string.Empty;
                    if (ctx.PrivateConversationData.TryGetValue(AuthTokenKey, out token) && ctx.UserData.TryGetValue("name", out name))
                    {
                        var validationTask = TwitterHelpers.ValidateAccessToken(token);
                        validationTask.Wait();
                        if (validationTask.IsCompleted && validationTask.Result)
                        {

                            Chain.ContinueWith(new TwitterDialog(),
                                                                async (context2, res2) =>
                                                                {

                                                                    var result2 = await res2;
                                                                    return Chain.Return($"Done.");
                                                                });

                            return Chain.Return($"Your are logged in as: {name}");
                        }
                        else
                        {
                            return Chain.Return($"Your Token has expired! Say \"login\" to log you back in!");
                        }
                    }
                    else
                    {
                        return Chain.Return("Say \"login\" when you want to login to Twitter!");
                    }
                })
            ).Unwrap().PostToUser();

这是我的FormFlow with BL

        public static IForm<Tweet> BuildForm()
            {

                OnCompletionAsyncDelegate<Tweet> processTweet = async (context, state) =>
                {

                    await context.PostAsync($"We are currently processing your tweet . We will message you the status. );
                    //...some code here ... 


                };

                return new FormBuilder<Tweet>()
                        .Message("Tweet bot !")
                        .Field(nameof(TweetHashtags))
                        .Field(nameof(Tweet.DeliveryTime), "When do you want publish your tweet? {||}")`
                        .Confirm("Are you sure ?")
                        .AddRemainingFields()
                        .Message("Thanks, your tweet will be post!")
                        .OnCompletion(processTweet)
                        .Build();

这是控制器

public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
        {
               // QUESTION 1 : 
               // when user send the first message, How can I check if user is already logged in or not?

               //QUESTION 2:
               // Based on Question 1 answer,  How can switch between Auth Dialog and FormFlow  if user is not logged in ?

              //QUESTION 3:
              // is this the right place where I have to check the Question 1 and Question 2 or I have to do in another place ? 

}

1 个答案:

答案 0 :(得分:1)

我会在两个对话框中分别使用root对话框,这将确定用户是否经过身份验证。如果不是,您可以启动Auth Dialog;否则你可以打电话给你的FormFlow。

您不应该在Controller中执行检查。控制器应该只调用RootDialog。一般来说,当事情开始增长时,我更喜欢使用自定义对话而不是链。

您可以查看AuthBot,了解这种方法是如何完成的。在示例中,您将看到有一个ActionDialog(根对话框),它在需要时调用AzureAuthDialog。

如果您想检查更复杂的样本;还使用AuthBot和这种方法,检查AzureBot