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!")); } } }
/// <summary> /// Resume a conversation and post the data to the dialog waiting. /// </summary> /// <typeparam name="T"> Type of the data. </typeparam> /// <param name="resumptionCookie"> The id of the bot.</param> /// <param name="toBot"> The data sent to bot.</param> /// <param name="token"> The cancellation token.</param> /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns> public static async Task <Message> ResumeAsync <T>(ResumptionCookie resumptionCookie, T toBot, CancellationToken token = default(CancellationToken)) { var continuationMessage = resumptionCookie.GetMessage(); using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage)) { return(await ResumeAsync(scope, continuationMessage, toBot, token)); } }
/// <summary> /// Resume a conversation and post the data to the dialog waiting. /// </summary> /// <typeparam name="T"> Type of the data. </typeparam> /// <param name="resumptionCookie"> The id of the bot.</param> /// <param name="toBot"> The data sent to bot.</param> /// <param name="token"> The cancellation token.</param> /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns> public static async Task ResumeAsync<T>(ResumptionCookie resumptionCookie, T toBot, CancellationToken token = default(CancellationToken)) { var continuationMessage = resumptionCookie.GetMessage(); using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage)) { Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); }; DialogModule_MakeRoot.Register(scope, MakeRoot); await ResumeAsync(scope, continuationMessage, toBot, token); } }
/// <summary> /// Resume a conversation and post the data to the dialog waiting. /// </summary> /// <param name="resumptionCookie"> The id of the bot.</param> /// <param name="toBot"> The data sent to bot.</param> /// <param name="token"> The cancellation token.</param> /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns> public static async Task ResumeAsync(ResumptionCookie resumptionCookie, IActivity toBot, CancellationToken token = default(CancellationToken)) { if (resumptionCookie.IsTrustedServiceUrl) { MicrosoftAppCredentials.TrustServiceUrl(resumptionCookie.Address.ServiceUrl); } var continuationMessage = resumptionCookie.GetMessage(); using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage)) { Func <IDialog <object> > MakeRoot = () => { throw new InvalidOperationException(); }; DialogModule_MakeRoot.Register(scope, MakeRoot); await ResumeAsync(scope, continuationMessage, 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")); } } }