-
Notifications
You must be signed in to change notification settings - Fork 487
/
Copy pathCloudAdapterBase.cs
339 lines (284 loc) · 17.9 KB
/
CloudAdapterBase.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.Bot.Builder
{
/// <summary>
/// An adapter that implements the Bot Framework Protocol and can be hosted in different cloud environmens both public and private.
/// </summary>
public abstract class CloudAdapterBase : BotAdapter
{
/// <summary>
/// Initializes a new instance of the <see cref="CloudAdapterBase"/> class.
/// </summary>
/// <param name="botFrameworkAuthentication">The cloud environment used for validating and creating tokens.</param>
/// <param name="logger">The ILogger implementation this adapter should use.</param>
protected CloudAdapterBase(
BotFrameworkAuthentication botFrameworkAuthentication,
ILogger logger = null)
{
BotFrameworkAuthentication = botFrameworkAuthentication ?? throw new ArgumentNullException(nameof(botFrameworkAuthentication));
Logger = logger ?? NullLogger.Instance;
}
/// <summary>
/// Gets the <see cref="BotFrameworkAuthentication" /> instance for this adapter.
/// </summary>
/// <value>
/// The <see cref="BotFrameworkAuthentication" /> instance for this adapter.
/// </value>
protected BotFrameworkAuthentication BotFrameworkAuthentication { get; private set; }
/// <summary>
/// Gets a <see cref="ILogger" /> to use within this adapter and its subclasses.
/// </summary>
/// <value>
/// The <see cref="ILogger" /> instance for this adapter.
/// </value>
protected ILogger Logger { get; private set; }
/// <inheritdoc/>
public override async Task<ResourceResponse[]> SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
{
_ = turnContext ?? throw new ArgumentNullException(nameof(turnContext));
_ = activities ?? throw new ArgumentNullException(nameof(activities));
if (activities.Length == 0)
{
throw new ArgumentException("Expecting one or more activities, but the array was empty.", nameof(activities));
}
Logger.LogInformation($"SendActivitiesAsync for {activities.Length} activities.");
var responses = new ResourceResponse[activities.Length];
for (var index = 0; index < activities.Length; index++)
{
var activity = activities[index];
activity.Id = null;
var response = default(ResourceResponse);
Logger.LogInformation($"Sending activity. ReplyToId: {activity.ReplyToId}");
if (activity.Type == ActivityTypesEx.Delay)
{
var delayMs = (int)activity.Value;
await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
}
else if (activity.Type == ActivityTypesEx.InvokeResponse)
{
turnContext.TurnState.Add(InvokeResponseKey, activity);
}
else if (activity.Type == ActivityTypes.Trace && activity.ChannelId != Channels.Emulator)
{
// no-op
}
else
{
if (!string.IsNullOrWhiteSpace(activity.ReplyToId))
{
var connectorClient = turnContext.TurnState.Get<IConnectorClient>();
response = await connectorClient.Conversations.ReplyToActivityAsync(activity, cancellationToken).ConfigureAwait(false);
}
else
{
var connectorClient = turnContext.TurnState.Get<IConnectorClient>();
response = await connectorClient.Conversations.SendToConversationAsync(activity, cancellationToken).ConfigureAwait(false);
}
}
if (response == null)
{
response = new ResourceResponse(activity.Id ?? string.Empty);
}
responses[index] = response;
}
return responses;
}
/// <inheritdoc/>
public override async Task<ResourceResponse> UpdateActivityAsync(ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
{
_ = turnContext ?? throw new ArgumentNullException(nameof(turnContext));
_ = activity ?? throw new ArgumentNullException(nameof(activity));
Logger.LogInformation($"UpdateActivityAsync ActivityId: {activity.Id}");
var connectorClient = turnContext.TurnState.Get<IConnectorClient>();
return await connectorClient.Conversations.UpdateActivityAsync(activity, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public override async Task DeleteActivityAsync(ITurnContext turnContext, ConversationReference reference, CancellationToken cancellationToken)
{
_ = turnContext ?? throw new ArgumentNullException(nameof(turnContext));
_ = reference ?? throw new ArgumentNullException(nameof(reference));
Logger.LogInformation($"DeleteActivityAsync Conversation Id: {reference.Conversation.Id}, ActivityId: {reference.ActivityId}");
var connectorClient = turnContext.TurnState.Get<IConnectorClient>();
await connectorClient.Conversations.DeleteActivityAsync(reference.Conversation.Id, reference.ActivityId, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(string botAppId, ConversationReference reference, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = reference ?? throw new ArgumentNullException(nameof(reference));
return ProcessProactiveAsync(CreateClaimsIdentity(botAppId), reference.GetContinuationActivity(), null, callback, cancellationToken);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, ConversationReference reference, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = reference ?? throw new ArgumentNullException(nameof(reference));
return ProcessProactiveAsync(claimsIdentity, reference.GetContinuationActivity(), null, callback, cancellationToken);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, ConversationReference reference, string audience, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = claimsIdentity ?? throw new ArgumentNullException(nameof(claimsIdentity));
_ = reference ?? throw new ArgumentNullException(nameof(reference));
_ = callback ?? throw new ArgumentNullException(nameof(callback));
return ProcessProactiveAsync(claimsIdentity, reference.GetContinuationActivity(), audience, callback, cancellationToken);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(string botAppId, Activity continuationActivity, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = callback ?? throw new ArgumentNullException(nameof(callback));
ValidateContinuationActivity(continuationActivity);
return ProcessProactiveAsync(CreateClaimsIdentity(botAppId), continuationActivity, null, callback, cancellationToken);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, Activity continuationActivity, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = claimsIdentity ?? throw new ArgumentNullException(nameof(claimsIdentity));
_ = callback ?? throw new ArgumentNullException(nameof(callback));
ValidateContinuationActivity(continuationActivity);
return ProcessProactiveAsync(claimsIdentity, continuationActivity, null, callback, cancellationToken);
}
/// <inheritdoc/>
public override Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, Activity continuationActivity, string audience, BotCallbackHandler callback, CancellationToken cancellationToken)
{
_ = claimsIdentity ?? throw new ArgumentNullException(nameof(claimsIdentity));
_ = callback ?? throw new ArgumentNullException(nameof(callback));
ValidateContinuationActivity(continuationActivity);
return ProcessProactiveAsync(claimsIdentity, continuationActivity, audience, callback, cancellationToken);
}
/// <summary>
/// The implementation for continue conversation.
/// </summary>
/// <param name="claimsIdentity">A <see cref="ClaimsIdentity"/> for the conversation.</param>
/// <param name="continuationActivity">The continuation <see cref="Activity"/> used to create the <see cref="ITurnContext" />.</param>
/// <param name="audience">The audience for the call.</param>
/// <param name="callback">The method to call for the resulting bot turn.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task that represents the work queued to execute.</returns>
protected async Task ProcessProactiveAsync(ClaimsIdentity claimsIdentity, Activity continuationActivity, string audience, BotCallbackHandler callback, CancellationToken cancellationToken)
{
Logger.LogInformation($"ProcessProactiveAsync for Conversation Id: {continuationActivity.Conversation.Id}");
// Create the connector factory and the inbound request, extracting parameters and then create a connector for outbound requests.
var connectorFactory = BotFrameworkAuthentication.CreateConnectorFactory(claimsIdentity);
// Create the connector client to use for outbound requests.
using (var connectorClient = await connectorFactory.CreateAsync(continuationActivity.ServiceUrl, audience, cancellationToken).ConfigureAwait(false))
// Create a UserTokenClient instance for the application to use. (For example, in the OAuthPrompt.)
using (var userTokenClient = await BotFrameworkAuthentication.CreateUserTokenClientAsync(claimsIdentity, cancellationToken).ConfigureAwait(false))
// Create a turn context and run the pipeline.
using (var context = CreateTurnContext(continuationActivity, claimsIdentity, audience, connectorClient, userTokenClient, callback, connectorFactory))
{
// Run the pipeline.
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// The implementation for processing an Activity sent to this bot.
/// </summary>
/// <param name="authHeader">The authorization header from the http request.</param>
/// <param name="activity">The <see cref="Activity"/> to process.</param>
/// <param name="callback">The method to call for the resulting bot turn.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task that represents the work queued to execute. Containing the InvokeResponse if there is one.</returns>
protected async Task<InvokeResponse> ProcessActivityAsync(string authHeader, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
{
Logger.LogInformation($"ProcessActivityAsync");
// Authenticate the inbound request, extracting parameters and create a ConnectorFactory for creating a Connector for outbound requests.
var authenticateRequestResult = await BotFrameworkAuthentication.AuthenticateRequestAsync(activity, authHeader, cancellationToken).ConfigureAwait(false);
// Delegate the creation and execution of the turn, so the implementation can be shared with streaming requests
return await ProcessActivityAsync(authenticateRequestResult, activity, callback, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// The implementation for processing an Activity sent to this bot.
/// </summary>
/// <param name="authenticateRequestResult">The authentication results for this turn.</param>
/// <param name="activity">The <see cref="Activity"/> to process.</param>
/// <param name="callback">The method to call for the resulting bot turn.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task that represents the work queued to execute. Containing the InvokeResponse if there is one.</returns>
protected async Task<InvokeResponse> ProcessActivityAsync(AuthenticateRequestResult authenticateRequestResult, Activity activity, BotCallbackHandler callback, CancellationToken cancellationToken)
{
// Set the callerId on the activity.
activity.CallerId = authenticateRequestResult.CallerId;
// Create the connector client to use for outbound requests.
using (var connectorClient = await authenticateRequestResult.ConnectorFactory.CreateAsync(activity.ServiceUrl, authenticateRequestResult.Audience, cancellationToken).ConfigureAwait(false))
// Create a UserTokenClient instance for the application to use. (For example, it would be used in a sign-in prompt.)
using (var userTokenClient = await BotFrameworkAuthentication.CreateUserTokenClientAsync(authenticateRequestResult.ClaimsIdentity, cancellationToken).ConfigureAwait(false))
// Create a turn context and run the pipeline.
using (var context = CreateTurnContext(activity, authenticateRequestResult.ClaimsIdentity, authenticateRequestResult.Audience, connectorClient, userTokenClient, callback, authenticateRequestResult.ConnectorFactory))
{
// Run the pipeline.
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
// If there are any results they will have been left on the TurnContext.
return ProcessTurnResults(context);
}
}
/// <summary>
/// This is a helper to create the ClaimsIdentity structure from an appId that will be added to the TurnContext.
/// It is intended for use in proactive and named-pipe scenarios.
/// </summary>
/// <param name="botAppId">The bot's application id.</param>
/// <returns>A <see cref="ClaimsIdentity"/> with the audience and appId claims set to the appId.</returns>
protected ClaimsIdentity CreateClaimsIdentity(string botAppId)
{
if (botAppId == null)
{
botAppId = string.Empty;
}
// Hand craft Claims Identity.
return new ClaimsIdentity(new List<Claim>
{
// Adding claims for both Emulator and Channel.
new Claim(AuthenticationConstants.AudienceClaim, botAppId),
new Claim(AuthenticationConstants.AppIdClaim, botAppId),
});
}
private TurnContext CreateTurnContext(Activity activity, ClaimsIdentity claimsIdentity, string oauthScope, IConnectorClient connectorClient, UserTokenClient userTokenClient, BotCallbackHandler callback, ConnectorFactory connectorFactory)
{
var turnContext = new TurnContext(this, activity);
turnContext.TurnState.Add<IIdentity>(BotIdentityKey, claimsIdentity);
turnContext.TurnState.Add(connectorClient);
turnContext.TurnState.Add(userTokenClient);
turnContext.TurnState.Add(callback);
turnContext.TurnState.Add(connectorFactory);
turnContext.TurnState.Set(OAuthScopeKey, oauthScope); // in non-skills scenarios the oauth scope value here will be null, so use Set
return turnContext;
}
private void ValidateContinuationActivity(Activity continuationActivity)
{
_ = continuationActivity ?? throw new ArgumentNullException(nameof(continuationActivity));
_ = continuationActivity.Conversation ?? throw new ArgumentException("The continuation Activity should contain a Conversation value.");
_ = continuationActivity.ServiceUrl ?? throw new ArgumentException("The continuation Activity should contain a ServiceUrl value.");
}
private InvokeResponse ProcessTurnResults(TurnContext turnContext)
{
// Handle ExpectedReplies scenarios where the all the activities have been buffered and sent back at once in an invoke response.
if (turnContext.Activity.DeliveryMode == DeliveryModes.ExpectReplies)
{
return new InvokeResponse { Status = (int)HttpStatusCode.OK, Body = new ExpectedReplies(turnContext.BufferedReplyActivities) };
}
// Handle Invoke scenarios where the Bot will return a specific body and return code.
if (turnContext.Activity.Type == ActivityTypes.Invoke)
{
var activityInvokeResponse = turnContext.TurnState.Get<Activity>(InvokeResponseKey);
if (activityInvokeResponse == null)
{
return new InvokeResponse { Status = (int)HttpStatusCode.NotImplemented };
}
return (InvokeResponse)activityInvokeResponse.Value;
}
// No body to return.
return null;
}
}
}