-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathConversation.cs
185 lines (172 loc) · 7.54 KB
/
Conversation.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Builder SDK GitHub:
// https://github.com/Microsoft/BotBuilder
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Bot.Builder.ConnectorEx;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
namespace Microsoft.Bot.Builder.Dialogs
{
/// <summary>
/// The top level composition root for the SDK.
/// </summary>
public static partial class Conversation
{
private static readonly object gate = new object();
private static IContainer container;
static Conversation()
{
UpdateContainer(builder =>
{
});
}
public static IContainer Container
{
get
{
lock (gate)
{
return container;
}
}
}
/// <summary>
/// Update the Autofac container.
/// </summary>
/// <param name="update">The delegate that represents the update to apply.</param>
public static void UpdateContainer(Action<ContainerBuilder> update)
{
lock (gate)
{
var builder = new ContainerBuilder();
builder.RegisterModule(new DialogModule_MakeRoot());
update(builder);
container = builder.Build();
}
}
/// <summary>
/// Process an incoming message within the conversation.
/// </summary>
/// <remarks>
/// This method:
/// 1. Instantiates and composes the required components.
/// 2. Deserializes the dialog state (the dialog stack and each dialog's state) from the <paramref name="toBot"/> <see cref="IMessageActivity"/>.
/// 3. Resumes the conversation processes where the dialog suspended to wait for a <see cref="IMessageActivity"/>.
/// 4. Queues <see cref="IMessageActivity"/>s to be sent to the user.
/// 5. Serializes the updated dialog state in the messages to be sent to the user.
///
/// The <paramref name="MakeRoot"/> factory method is invoked for new conversations only,
/// because existing conversations have the dialog stack and state serialized in the <see cref="IMessageActivity"/> data.
/// </remarks>
/// <param name="toBot">The message sent to the bot.</param>
/// <param name="MakeRoot">The factory method to make the root dialog.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>A task that represents the message to send inline back to the user.</returns>
public static async Task SendAsync(IMessageActivity toBot, Func<IDialog<object>> MakeRoot, CancellationToken token = default(CancellationToken))
{
using (var scope = DialogModule.BeginLifetimeScope(Container, toBot))
{
DialogModule_MakeRoot.Register(scope, MakeRoot);
await SendAsync(scope, toBot, token);
}
}
/// <summary>
/// Resume a conversation and post the data to the dialog waiting.
/// </summary>
/// <param name="resumptionCookie"> The resumption cookie.</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>
[Obsolete("Use the overload that uses ConversationReference instead of ResumptionCookie")]
public static async Task ResumeAsync(ResumptionCookie resumptionCookie, IActivity toBot, CancellationToken token = default(CancellationToken))
{
var conversationRef = resumptionCookie.ToConversationReference();
await ResumeAsync(conversationRef, toBot, token);
}
/// <summary>
/// Resume a conversation and post the data to the dialog waiting.
/// </summary>
/// <param name="conversationReference"> The resumption cookie.</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(ConversationReference conversationReference, IActivity toBot, CancellationToken token = default(CancellationToken))
{
var continuationMessage = conversationReference.GetPostToBotMessage();
using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage))
{
Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); };
DialogModule_MakeRoot.Register(scope, MakeRoot);
await SendAsync(scope, toBot, token);
}
}
/// <summary>
/// Disable a specific service type by replacing it with a pass through implementation.
/// </summary>
/// <param name="type">The service type.</param>
/// <param name="builder">The container builder.</param>
public static void Disable(Type type, ContainerBuilder builder)
{
if (typeof(IBotToUser).IsAssignableFrom(type))
{
builder
.RegisterType<PassBotToUser>()
.Keyed<IBotToUser>(type);
}
if (typeof(IPostToBot).IsAssignableFrom(type))
{
builder
.RegisterType<PassPostToBot>()
.Keyed<IPostToBot>(type);
}
}
/// <summary>
/// Disable a specific service type by replacing it with a pass through implementation.
/// </summary>
/// <param name="type">The service type.</param>
public static void Disable(Type type)
{
UpdateContainer(builder =>
{
Disable(type, builder);
});
}
internal static async Task SendAsync(ILifetimeScope scope, IActivity toBot, CancellationToken token = default(CancellationToken))
{
var task = scope.Resolve<IPostToBot>();
await task.PostAsync(toBot, token);
}
}
}