forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebexAdapter.cs
308 lines (275 loc) · 15.6 KB
/
WebexAdapter.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Authentication;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Thrzn41.WebexTeams.Version1;
namespace Bot.Builder.Community.Adapters.Webex
{
/// <summary>
/// BotAdapter to allow for handling Webex Teams app payloads and responses via the Webex Teams API.
/// </summary>
public class WebexAdapter : BotAdapter, IBotFrameworkHttpAdapter
{
private const string WebexAccessTokenKey = "WebexAccessToken";
private const string WebexPublicAddressKey = "WebexPublicAddress";
private const string WebexSecretKey = "WebexSecret";
private const string WebexWebhookNameKey = "WebexWebhookName";
private readonly WebexClientWrapper _webexClient;
private readonly ILogger _logger;
private readonly WebexAdapterOptions _options;
/// <summary>
/// Initializes a new instance of the <see cref="WebexAdapter"/> class using configuration settings.
/// </summary>
/// <param name="configuration">An <see cref="IConfiguration"/> instance.</param>
/// <remarks>
/// The configuration keys are:
/// WebexAccessToken: An access token for the bot.
/// WebexPublicAddress: The root URL of the bot application.
/// WebexSecret: The secret used to validate incoming webhooks.
/// WebexWebhookName: A name for the webhook subscription.
/// </remarks>
/// <param name="options">An instance of <see cref="WebexAdapterOptions"/>.</param>
/// <param name="logger">The ILogger implementation this adapter should use.</param>
public WebexAdapter(IConfiguration configuration, WebexAdapterOptions options = null, ILogger logger = null)
: this(new WebexClientWrapper(new WebexClientWrapperOptions(configuration[WebexAccessTokenKey], new Uri(configuration[WebexPublicAddressKey]), configuration[WebexSecretKey], configuration[WebexWebhookNameKey])), options, logger)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WebexAdapter"/> class.
/// Creates a Webex adapter.
/// </summary>
/// <param name="webexClient">A Webex API interface.</param>
/// <param name="options">An instance of <see cref="WebexAdapterOptions"/>.</param>
/// <param name="logger">The ILogger implementation this adapter should use.</param>
public WebexAdapter(WebexClientWrapper webexClient, WebexAdapterOptions options, ILogger logger = null)
{
_webexClient = webexClient ?? throw new ArgumentNullException(nameof(webexClient));
_options = options ?? new WebexAdapterOptions();
_logger = logger ?? NullLogger.Instance;
}
/// <summary>
/// Standard BotBuilder adapter method to send a message from the bot to the messaging API.
/// </summary>
/// <param name="turnContext">A TurnContext representing the current incoming message and environment.</param>
/// <param name="activities">An array of outgoing activities to be sent back to the messaging API.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public override async Task<ResourceResponse[]> SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
{
var responses = new List<ResourceResponse>();
foreach (var activity in activities)
{
if (activity.Type != ActivityTypes.Message)
{
_logger.LogTrace($"Unsupported Activity Type: '{activity.Type}'. Only Activities of type 'Message' are supported.");
}
else
{
// transform activity into the webex message format
string recipientId;
var target = MessageTarget.PersonId;
if (activity.Conversation?.Id != null)
{
recipientId = activity.Conversation.Id;
target = MessageTarget.SpaceId;
}
else if (activity.Conversation == null && activity.Recipient?.Id != null)
{
recipientId = activity.Recipient.Id;
}
else if (activity.GetChannelData<WebhookEventData>()?.MessageData.PersonEmail != null)
{
recipientId = activity.GetChannelData<WebhookEventData>()?.MessageData.PersonEmail;
}
else
{
throw new InvalidOperationException("No Person, Email or Room to send the message");
}
string responseId;
if (activity.Attachments != null && activity.Attachments.Count > 0)
{
if (activity.Attachments[0].ContentType == "application/vnd.microsoft.card.adaptive")
{
responseId = await _webexClient.CreateMessageWithAttachmentsAsync(recipientId, activity.Text, activity.Attachments, MessageTextType.Text, target, cancellationToken).ConfigureAwait(false);
}
else
{
var files = new List<Uri>();
foreach (var attachment in activity.Attachments)
{
var file = new Uri(attachment.ContentUrl);
files.Add(file);
}
responseId = await _webexClient.CreateMessageAsync(recipientId, activity.Text, files.Count > 0 ? files : null, MessageTextType.Text, target, cancellationToken).ConfigureAwait(false);
}
}
else
{
responseId = await _webexClient
.CreateMessageAsync(recipientId, activity.Text, target: target, cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
responses.Add(new ResourceResponse(responseId));
}
}
return responses.ToArray();
}
/// <summary>
/// Standard BotBuilder adapter method to update a previous message.
/// </summary>
/// <param name="turnContext">A TurnContext representing the current incoming message and environment.</param>
/// <param name="activity">An activity to be sent back to the messaging API.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public override Task<ResourceResponse> UpdateActivityAsync(ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
{
return Task.FromException<ResourceResponse>(new NotSupportedException("Webex adapter does not support updateActivity."));
}
/// <summary>
/// Standard BotBuilder adapter method to delete a previous message.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> representing the current incoming message and environment.</param>
/// <param name="reference">A <see cref="ConversationReference"/> object.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public override async Task DeleteActivityAsync(ITurnContext turnContext, ConversationReference reference, CancellationToken cancellationToken)
{
if (!string.IsNullOrWhiteSpace(reference.ActivityId))
{
await _webexClient.DeleteMessageAsync(reference.ActivityId, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Standard BotBuilder adapter method for continuing an existing conversation based on a conversation reference.
/// </summary>
/// <param name="reference">A <see cref="ConversationReference"/> to be applied to future messages.</param>
/// <param name="logic">A bot logic function that will perform continuing action.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task ContinueConversationAsync(ConversationReference reference, BotCallbackHandler logic, CancellationToken cancellationToken)
{
if (reference == null)
{
throw new ArgumentNullException(nameof(reference));
}
if (logic == null)
{
throw new ArgumentNullException(nameof(logic));
}
var request = reference.GetContinuationActivity().ApplyConversationReference(reference, true);
using (var context = new TurnContext(this, request))
{
await RunPipelineAsync(context, logic, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Sends a proactive message from the bot to a conversation.
/// </summary>
/// <param name="claimsIdentity">A <see cref="ClaimsIdentity"/> for the conversation.</param>
/// <param name="reference">A reference to the conversation to continue.</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>
/// <remarks>Call this method to proactively send a message to a conversation.
/// Most _channels require a user to initialize a conversation with a bot
/// before the bot can send activities to the user.
/// <para>This method registers the following services for the turn.<list type="bullet">
/// <item><description><see cref="IIdentity"/> (key = "BotIdentity"), a claims claimsIdentity for the bot.
/// </description></item>
/// </list></para>
/// </remarks>
/// <seealso cref="BotAdapter.RunPipelineAsync(ITurnContext, BotCallbackHandler, CancellationToken)"/>
public override async Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, ConversationReference reference, BotCallbackHandler callback, CancellationToken cancellationToken)
{
using (var context = new TurnContext(this, reference.GetContinuationActivity()))
{
context.TurnState.Add<IIdentity>(BotIdentityKey, claimsIdentity);
context.TurnState.Add<BotCallbackHandler>(callback);
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Accept an incoming webhook <see cref="HttpRequest"/> and convert it into a <see cref="TurnContext"/> which can be processed by the bot's logic.
/// </summary>
/// <param name="request">The incoming <see cref="HttpRequest"/>.</param>
/// <param name="response">When this method completes, the <see cref="HttpResponse"/> to send.</param>
/// <param name="bot">The bot that will handle the incoming activity.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task ProcessAsync(HttpRequest request, HttpResponse response, IBot bot, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
if (bot == null)
{
throw new ArgumentNullException(nameof(bot));
}
var identity = await _webexClient.GetMeAsync(cancellationToken).ConfigureAwait(false);
WebhookEventData payload;
string json;
using (var bodyStream = new StreamReader(request.Body))
{
json = await bodyStream.ReadToEndAsync().ConfigureAwait(false);
payload = JsonConvert.DeserializeObject<WebhookEventData>(json);
}
if (_options.ValidateIncomingRequests && !_webexClient.ValidateSignature(request, json))
{
throw new AuthenticationException("Webhook received message with invalid signature. Potential malicious behavior!");
}
Activity activity;
if (payload.Resource == EventResource.Message && payload.EventType == EventType.Created)
{
var decryptedMessage = await WebexHelper.GetDecryptedMessageAsync(payload, _webexClient.GetMessageAsync, cancellationToken).ConfigureAwait(false);
activity = WebexHelper.DecryptedMessageToActivity(decryptedMessage, identity);
}
else if (payload.Resource.Name == "attachmentActions" && payload.EventType == EventType.Created)
{
var extraData = payload.GetResourceData<TeamsData>();
var data = JsonConvert.SerializeObject(extraData);
var jsonData = JsonConvert.DeserializeObject<AttachmentActionData>(data);
var decryptedMessage = await _webexClient.GetAttachmentActionAsync(jsonData.Id, cancellationToken).ConfigureAwait(false);
activity = WebexHelper.AttachmentActionToActivity(decryptedMessage, identity);
}
else
{
activity = WebexHelper.PayloadToActivity(payload, identity);
}
using (var context = new TurnContext(this, activity))
{
await RunPipelineAsync(context, bot.OnTurnAsync, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Determines whether the provided <see cref="IConfiguration"/> has the settings needed to
/// configure a <see cref="WebexAdapter"/>.
/// </summary>
/// <param name="configuration"><see cref="IConfiguration"/> to verify for settings.</param>
/// <returns>A value indicating whether the configuration has the necessary settings required to create a <see cref="WebexAdapter"/>.</returns>
internal static bool HasConfiguration(IConfiguration configuration)
{
// Do we have the config needed to create an adapter?
return !string.IsNullOrEmpty(configuration.GetValue<string>(WebexAccessTokenKey))
&& !string.IsNullOrEmpty(configuration.GetValue<string>(WebexPublicAddressKey))
&& !string.IsNullOrEmpty(configuration.GetValue<string>(WebexSecretKey))
&& !string.IsNullOrEmpty(configuration.GetValue<string>(WebexWebhookNameKey));
}
}
}