Skip to content

Commit

Permalink
Merge pull request microsoft#123 from southworkscom/csharp-proactive-…
Browse files Browse the repository at this point in the history
…messages-update

[C#-ProactiveMessages] Update to use ConversationReference and Json serialization
  • Loading branch information
msft-shahins authored Jun 1, 2017
2 parents f5179c3 + a70682a commit 45d0f87
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http;

namespace startNewDialog
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -19,7 +17,7 @@ public async Task<HttpResponseMessage> 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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;

namespace startNewDialog
{
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>(conversationReference).GetPostToBotMessage();
var client = new ConnectorClient(new Uri(message.ServiceUrl));

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
Expand Down
12 changes: 1 addition & 11 deletions CSharp/core-proactiveMessages/startNewDialog/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -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);


}
}
}
26 changes: 9 additions & 17 deletions CSharp/core-proactiveMessages/startNewDialog/RootDialog.cs
Original file line number Diff line number Diff line change
@@ -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<object>
{
Expand All @@ -27,10 +19,12 @@ public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> 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

Expand All @@ -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
}


}
}
17 changes: 4 additions & 13 deletions CSharp/core-proactiveMessages/startNewDialog/SurveyDialog.cs
Original file line number Diff line number Diff line change
@@ -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<object>
{
public async Task StartAsync(IDialogContext context)
Expand All @@ -24,6 +14,7 @@ public async Task StartAsync(IDialogContext context)

context.Wait(this.MessageReceivedAsync);
}

public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
if ((await result).Text == "done")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http;

namespace startNewDialogWithPrompt
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +16,7 @@ public async Task<HttpResponseMessage> 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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using Newtonsoft.Json;

namespace startNewDialogWithPrompt
{
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>(conversationReference).GetPostToBotMessage();
var client = new ConnectorClient(new Uri(message.ServiceUrl));

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,7 +14,6 @@ public class RootDialog : IDialog<object>
{
[NonSerialized]
Timer t;


public async Task StartAsync(IDialogContext context)
{
Expand All @@ -30,7 +23,8 @@ public async Task StartAsync(IDialogContext context)
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> 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));
Expand All @@ -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
}


}
}
Original file line number Diff line number Diff line change
@@ -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<object>
{
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<string> result)

{

if ((await result) == "Get back to where I was")
Expand All @@ -37,10 +24,7 @@ private async Task AfterSelectOption(IDialogContext context, IAwaitable<string>
{
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");

}

}

}
}

0 comments on commit 45d0f87

Please sign in to comment.