forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SurveyTriggerer.cs
71 lines (59 loc) · 2.51 KB
/
SurveyTriggerer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace CreateNewConversationBot
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using Microsoft.Rest;
public static class SurveyTriggerer
{
public static async Task StartSurvey(ResumptionCookie cookie, CancellationToken token)
{
var container = WebApiApplication.FindContainer();
// the ResumptionCookie has the "key" necessary to resume the conversation
var message = cookie.GetMessage();
ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));
try
{
var conversation = await client.Conversations.CreateDirectConversationAsync(message.Recipient, message.From);
message.Conversation.Id = conversation.Id;
}
catch (HttpOperationException ex)
{
var reply = message.CreateReply();
reply.Text = ex.Message;
await client.Conversations.SendToConversationAsync(reply);
return;
}
// we instantiate our dependencies based on an IMessageActivity implementation
using (var scope = DialogModule.BeginLifetimeScope(container, message))
{
// find the bot data interface and load up the conversation dialog state
var botData = scope.Resolve<IBotData>();
await botData.LoadAsync(token);
// resolve the dialog task
IDialogTask task = scope.Resolve<IDialogTask>();
// make a dialog to push on the top of the stack
var child = scope.Resolve<SurveyDialog>();
// wrap it with an additional dialog that will restart the wait for
// messages from the user once the child dialog has finished
var interruption = child.Void<object, IMessageActivity>();
try
{
// put the interrupting dialog on the stack
task.Call(interruption, null);
// start running the interrupting dialog
await task.PollAsync(token);
}
finally
{
// save out the conversation dialog state
await botData.FlushAsync(token);
}
}
}
}
}