From a70682a6c6ea7dd66df66b0db2c5e9e3bfbaff48 Mon Sep 17 00:00:00 2001 From: Ezequiel Jadib Date: Fri, 12 May 2017 12:37:20 -0700 Subject: [PATCH] Update sample to use ConversationReference and Json serialization --- .../startNewDialog/App_Start/WebApiConfig.cs | 5 +--- .../Controllers/CustomWebAPIController.cs | 4 +-- .../Controllers/MessagesController.cs | 9 +++---- .../startNewDialog/ConversationStarter.cs | 5 ++-- .../startNewDialog/Global.asax.cs | 12 +-------- .../startNewDialog/RootDialog.cs | 26 +++++++------------ .../startNewDialog/SurveyDialog.cs | 17 +++--------- .../App_Start/WebApiConfig.cs | 5 +--- .../Controllers/CustomWebAPIController.cs | 5 +--- .../Controllers/MessagesController.cs | 9 +++---- .../ConversationStarter.cs | 5 ++-- .../startNewDialogWithPrompt/Global.asax.cs | 14 ++-------- .../startNewDialogWithPrompt/RootDialog.cs | 22 +++++----------- .../startNewDialogWithPrompt/SurveyDialog.cs | 22 +++------------- 14 files changed, 42 insertions(+), 118 deletions(-) diff --git a/CSharp/core-proactiveMessages/startNewDialog/App_Start/WebApiConfig.cs b/CSharp/core-proactiveMessages/startNewDialog/App_Start/WebApiConfig.cs index 2818befc19..b36fb65a2c 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/App_Start/WebApiConfig.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/App_Start/WebApiConfig.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Http; +using System.Web.Http; namespace startNewDialog { diff --git a/CSharp/core-proactiveMessages/startNewDialog/Controllers/CustomWebAPIController.cs b/CSharp/core-proactiveMessages/startNewDialog/Controllers/CustomWebAPIController.cs index bef90e8886..1abc463220 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/Controllers/CustomWebAPIController.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/Controllers/CustomWebAPIController.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -19,7 +17,7 @@ public async Task SendMessage() { try { - if (!string.IsNullOrEmpty(ConversationStarter.resumptionCookie)) + if (!string.IsNullOrEmpty(ConversationStarter.conversationReference)) { await ConversationStarter.Resume(); //We don't need to wait for this, just want to start the interruption here diff --git a/CSharp/core-proactiveMessages/startNewDialog/Controllers/MessagesController.cs b/CSharp/core-proactiveMessages/startNewDialog/Controllers/MessagesController.cs index 5f02bed36f..b177f0585c 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/Controllers/MessagesController.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/Controllers/MessagesController.cs @@ -1,13 +1,10 @@ -using Autofac; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Connector; -using System.Diagnostics; -using System.Linq; +using System.Diagnostics; using System.Net.Http; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Description; +using Microsoft.Bot.Builder.Dialogs; +using Microsoft.Bot.Connector; namespace startNewDialog { diff --git a/CSharp/core-proactiveMessages/startNewDialog/ConversationStarter.cs b/CSharp/core-proactiveMessages/startNewDialog/ConversationStarter.cs index a0faff9302..e6d86a05d8 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/ConversationStarter.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/ConversationStarter.cs @@ -5,6 +5,7 @@ using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Builder.Dialogs.Internals; using Microsoft.Bot.Connector; +using Newtonsoft.Json; namespace startNewDialog { @@ -12,12 +13,12 @@ public class ConversationStarter { //Note: Of course you don't want this here. Eventually you will need to save this in some table //Having this here as static variable means we can only remember one user :) - public static string resumptionCookie; + public static string conversationReference; //This will interrupt the conversation and send the user to SurveyDialog, then wait until that's done public static async Task Resume() { - var message = ResumptionCookie.GZipDeserialize(resumptionCookie).GetMessage(); + var message = JsonConvert.DeserializeObject(conversationReference).GetPostToBotMessage(); var client = new ConnectorClient(new Uri(message.ServiceUrl)); using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message)) diff --git a/CSharp/core-proactiveMessages/startNewDialog/Global.asax.cs b/CSharp/core-proactiveMessages/startNewDialog/Global.asax.cs index b1820b3f78..e7329bddba 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/Global.asax.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/Global.asax.cs @@ -1,22 +1,12 @@ -using System.Reflection; -using System.Web.Http; -using Autofac; -using Autofac.Integration.WebApi; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Internals.Fibers; +using System.Web.Http; namespace startNewDialog { public class WebApiApplication : System.Web.HttpApplication { - - protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); - - } } } diff --git a/CSharp/core-proactiveMessages/startNewDialog/RootDialog.cs b/CSharp/core-proactiveMessages/startNewDialog/RootDialog.cs index 2f8220d0df..3b06f45774 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/RootDialog.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/RootDialog.cs @@ -1,22 +1,14 @@ -using Autofac; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Luis; -using Microsoft.Bot.Builder.Luis.Models; -using Microsoft.Bot.Connector; -using System; -using System.Collections.Generic; -using System.Linq; +using System; +using System.Threading; using System.Threading.Tasks; using System.Web; -using System.Xml.Linq; -using System.Threading; +using Microsoft.Bot.Builder.ConnectorEx; +using Microsoft.Bot.Builder.Dialogs; +using Microsoft.Bot.Connector; using Newtonsoft.Json; namespace startNewDialog { - - [Serializable] public class RootDialog : IDialog { @@ -27,10 +19,12 @@ public async Task StartAsync(IDialogContext context) { context.Wait(this.MessageReceivedAsync); } + public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable result) { var message = await result; - ConversationStarter.resumptionCookie = new ResumptionCookie(message).GZipSerialize(); + var conversationReference = message.ToConversationReference(); + ConversationStarter.conversationReference = JsonConvert.SerializeObject(conversationReference); //We will start a timer to fake a background service that will trigger the proactive message @@ -42,13 +36,11 @@ await context.PostAsync("Hey there, I'm going to interrupt our conversation and url.Scheme + "://" + url.Host + ":" + url.Port + "/api/CustomWebApi"); context.Wait(MessageReceivedAsync); } + public void timerEvent(object target) { - t.Dispose(); ConversationStarter.Resume(); //We don't need to wait for this, just want to start the interruption here } - - } } \ No newline at end of file diff --git a/CSharp/core-proactiveMessages/startNewDialog/SurveyDialog.cs b/CSharp/core-proactiveMessages/startNewDialog/SurveyDialog.cs index 976ff49282..d6bcddf2ba 100644 --- a/CSharp/core-proactiveMessages/startNewDialog/SurveyDialog.cs +++ b/CSharp/core-proactiveMessages/startNewDialog/SurveyDialog.cs @@ -1,21 +1,11 @@ -using Autofac; +using System; +using System.Threading.Tasks; using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Luis; -using Microsoft.Bot.Builder.Luis.Models; using Microsoft.Bot.Connector; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Web; -using System.Xml.Linq; -using System.Threading; -using Newtonsoft.Json; namespace startNewDialog { - [Serializable] + [Serializable] public class SurveyDialog : IDialog { public async Task StartAsync(IDialogContext context) @@ -24,6 +14,7 @@ public async Task StartAsync(IDialogContext context) context.Wait(this.MessageReceivedAsync); } + public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable result) { if ((await result).Text == "done") diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/App_Start/WebApiConfig.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/App_Start/WebApiConfig.cs index 07c4b3933e..7e1e34176f 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/App_Start/WebApiConfig.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/App_Start/WebApiConfig.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Http; +using System.Web.Http; namespace startNewDialogWithPrompt { diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/CustomWebAPIController.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/CustomWebAPIController.cs index 415166e23a..f2666e68d6 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/CustomWebAPIController.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/CustomWebAPIController.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; -using System.Web; using System.Web.Http; namespace startNewDialogWithPrompt.Controllers @@ -19,7 +16,7 @@ public async Task SendMessage() { try { - if (!string.IsNullOrEmpty(ConversationStarter.resumptionCookie)) + if (!string.IsNullOrEmpty(ConversationStarter.conversationReference)) { await ConversationStarter.Resume(); //We don't need to wait for this, just want to start the interruption here diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/MessagesController.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/MessagesController.cs index bb6804eab8..87409681a3 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/MessagesController.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Controllers/MessagesController.cs @@ -1,13 +1,10 @@ -using Autofac; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Connector; -using System.Diagnostics; -using System.Linq; +using System.Diagnostics; using System.Net.Http; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Description; +using Microsoft.Bot.Builder.Dialogs; +using Microsoft.Bot.Connector; namespace startNewDialogWithPrompt { diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/ConversationStarter.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/ConversationStarter.cs index 200dc7d776..f34dfee7b2 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/ConversationStarter.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/ConversationStarter.cs @@ -5,6 +5,7 @@ using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Builder.Dialogs.Internals; using Microsoft.Bot.Connector; +using Newtonsoft.Json; namespace startNewDialogWithPrompt { @@ -12,12 +13,12 @@ public class ConversationStarter { //Note: Of course you don't want this here. Eventually you will need to save this in some table //Having this here as static variable means we can only remember one user :) - public static string resumptionCookie; + public static string conversationReference; //This will interrupt the conversation and send the user to SurveyDialog, then wait until that's done public static async Task Resume() { - var message = ResumptionCookie.GZipDeserialize(resumptionCookie).GetMessage(); + var message = JsonConvert.DeserializeObject(conversationReference).GetPostToBotMessage(); var client = new ConnectorClient(new Uri(message.ServiceUrl)); using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message)) diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Global.asax.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Global.asax.cs index 2d5c5bb562..38f309ec63 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Global.asax.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/Global.asax.cs @@ -1,22 +1,12 @@ -using System.Reflection; -using System.Web.Http; -using Autofac; -using Autofac.Integration.WebApi; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Internals.Fibers; +using System.Web.Http; namespace startNewDialogWithPrompt { public class WebApiApplication : System.Web.HttpApplication { - - protected void Application_Start() { - GlobalConfiguration.Configure(WebApiConfig.Register); - - + GlobalConfiguration.Configure(WebApiConfig.Register); } } } diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/RootDialog.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/RootDialog.cs index 0e01b7d225..d3b39db4a2 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/RootDialog.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/RootDialog.cs @@ -1,16 +1,10 @@ -using Autofac; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Luis; -using Microsoft.Bot.Builder.Luis.Models; -using Microsoft.Bot.Connector; -using System; -using System.Collections.Generic; -using System.Linq; +using System; +using System.Threading; using System.Threading.Tasks; using System.Web; -using System.Xml.Linq; -using System.Threading; +using Microsoft.Bot.Builder.ConnectorEx; +using Microsoft.Bot.Builder.Dialogs; +using Microsoft.Bot.Connector; using Newtonsoft.Json; namespace startNewDialogWithPrompt @@ -20,7 +14,6 @@ public class RootDialog : IDialog { [NonSerialized] Timer t; - public async Task StartAsync(IDialogContext context) { @@ -30,7 +23,8 @@ public async Task StartAsync(IDialogContext context) public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable result) { var message = await result; - ConversationStarter.resumptionCookie = new ResumptionCookie(message).GZipSerialize(); + var conversationReference = message.ToConversationReference(); + ConversationStarter.conversationReference = JsonConvert.SerializeObject(conversationReference); //Prepare the timer to simulate a background/asynchonous process t = new Timer(new TimerCallback(timerEvent)); @@ -48,7 +42,5 @@ public void timerEvent(object target) t.Dispose(); ConversationStarter.Resume(); //We don't need to wait for this, just want to start the interruption here } - - } } \ No newline at end of file diff --git a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/SurveyDialog.cs b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/SurveyDialog.cs index 7d784ca129..ec6f9cbe74 100644 --- a/CSharp/core-proactiveMessages/startNewDialogWithPrompt/SurveyDialog.cs +++ b/CSharp/core-proactiveMessages/startNewDialogWithPrompt/SurveyDialog.cs @@ -1,31 +1,18 @@ -using Autofac; -using Microsoft.Bot.Builder.Dialogs; -using Microsoft.Bot.Builder.Dialogs.Internals; -using Microsoft.Bot.Builder.Luis; -using Microsoft.Bot.Builder.Luis.Models; -using Microsoft.Bot.Connector; -using System; -using System.Collections.Generic; -using System.Linq; +using System; using System.Threading.Tasks; -using System.Web; -using System.Xml.Linq; -using System.Threading; -using Newtonsoft.Json; +using Microsoft.Bot.Builder.Dialogs; namespace startNewDialogWithPrompt { - [Serializable] + [Serializable] public class SurveyDialog : IDialog { public async Task StartAsync(IDialogContext context) { PromptDialog.Choice(context, this.AfterSelectOption, new string[] { "Stay in this survey", "Get back to where I was" }, "Hello, you're in the survey dialog. Please pick one of these options"); - } private async Task AfterSelectOption(IDialogContext context, IAwaitable result) - { if ((await result) == "Get back to where I was") @@ -37,10 +24,7 @@ private async Task AfterSelectOption(IDialogContext context, IAwaitable { await context.PostAsync("I'm still on the survey until you tell me to stop"); PromptDialog.Choice(context, this.AfterSelectOption, new string[] { "Stay in this survey", "Get back to where I was" }, "Hello, you're in the survey dialog. Please pick one of these options"); - } - } - } } \ No newline at end of file