public async Task<HttpResponseMessage> OAuthCallback([FromUri] string userId, [FromUri] string conversationId, [FromUri] string channelId, [FromUri] string serviceUrl, [FromUri] string locale, [FromUri] string code, [FromUri] string state, CancellationToken token)
        {
            // Get the resumption cookie
            var resumptionCookie = new ResumptionCookie(userId, botId.Value, conversationId, channelId, HttpUtility.UrlDecode(serviceUrl), locale);

            // Exchange the Facebook Auth code with Access token
            var accessToken = await FacebookHelpers.ExchangeCodeForAccessToken(resumptionCookie, code, SimpleFacebookAuthDialog.FacebookOauthCallback.ToString());

            // Create the message that is send to conversation to resume the login flow
            var msg = resumptionCookie.GetMessage();
            msg.Text = $"token:{accessToken.AccessToken}";

            // Resume the conversation to SimpleFacebookAuthDialog
            await Conversation.ResumeAsync(resumptionCookie, msg);

            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, msg))
            {
                var dataBag = scope.Resolve<IBotData>();
                await dataBag.LoadAsync(token);
                ResumptionCookie pending;
                if (dataBag.PrivateConversationData.TryGetValue("persistedCookie", out pending))
                {
                    // remove persisted cookie
                    dataBag.PrivateConversationData.RemoveValue("persistedCookie");
                    await dataBag.FlushAsync(token);
                    return Request.CreateResponse("You are now logged in! Continue talking to the bot.");
                }
                else
                {
                    // Callback is called with no pending message as a result the login flow cannot be resumed.
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Cannot resume!"));
                }
            }
        }
Example #2
0
        public static async Task ResumeAsync(ResumptionCookie resumptionCookie, IActivity toBot, CancellationToken token = default(CancellationToken))
        {
            var conversationRef = resumptionCookie.ToConversationReference();

            await ResumeAsync(conversationRef, toBot, token);
        }
        public async Task SendResumeAsyncTest()
        {
            var chain = Chain.PostToChain().Select(m => m.Text).Switch(
                new RegexCase<IDialog<string>>(new Regex("^resume"), (context, data) => { context.UserData.SetValue("resume", true); return Chain.Return("resumed!"); }),
                new DefaultCase<string, IDialog<string>>((context, data) => { return Chain.Return(data); })).Unwrap().PostToUser();

            using (new FiberTestBase.ResolveMoqAssembly(chain))
            using (var container = Build(Options.InMemoryBotDataStore, new MockConnectorFactory(new BotIdResolver("testBot")), chain))
            {
                var msg = DialogTestBase.MakeTestMessage();
                msg.Text = "testMsg";
                
                using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                {
                    Func<IDialog<object>> MakeRoot = () => chain;
                    scope.Resolve<Func<IDialog<object>>>(TypedParameter.From(MakeRoot));

                    await Conversation.SendAsync(scope, msg);
                    var reply = scope.Resolve<Queue<IMessageActivity>>().Dequeue();
                }

                var resumptionCookie = new ResumptionCookie(msg);
                var continuationMessage = resumptionCookie.GetMessage(); 
                using (var scope = DialogModule.BeginLifetimeScope(container, continuationMessage))
                {
                    Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); };
                    scope.Resolve<Func<IDialog<object>>>(TypedParameter.From(MakeRoot));

                    await Conversation.ResumeAsync(scope, continuationMessage, new Activity { Text = "resume" });
                    var reply = scope.Resolve<Queue<IMessageActivity>>().Dequeue();
                    Assert.AreEqual("resumed!", reply.Text);

                    var botData = scope.Resolve<IBotData>();
                    await botData.LoadAsync(default(CancellationToken));
                    Assert.IsTrue(botData.UserData.Get<bool>("resume"));
                }
            }
        }