-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathZoomAdapter.cs
166 lines (138 loc) · 6.39 KB
/
ZoomAdapter.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Bot.Builder.Community.Adapters.Zoom.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using HttpResponse = Microsoft.AspNetCore.Http.HttpResponse;
namespace Bot.Builder.Community.Adapters.Zoom
{
public class ZoomAdapter : BotAdapter, IBotFrameworkHttpAdapter
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
private readonly ZoomAdapterOptions _options;
private readonly ILogger _logger;
private readonly ZoomRequestMapper _requestMapper;
private readonly ZoomClient _zoomClient;
public ZoomAdapter(ZoomAdapterOptions options = null, ILogger logger = null)
{
_options = options ?? new ZoomAdapterOptions();
_logger = logger ?? NullLogger.Instance;
_zoomClient = new ZoomClient(_options.ClientId, _options.ClientSecret);
_requestMapper = new ZoomRequestMapper(new ZoomRequestMapperOptions()
{
RobotJid = _options.BotJid
}, null);
}
public async Task ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken = default)
{
if (httpRequest == null)
{
throw new ArgumentNullException(nameof(httpRequest));
}
if (httpResponse == null)
{
throw new ArgumentNullException(nameof(httpResponse));
}
if (bot == null)
{
throw new ArgumentNullException(nameof(bot));
}
if (_options.ValidateIncomingZoomRequests &&
httpRequest.Headers.TryGetValue("HeaderAuthorization", out StringValues headerAuthorization)
&& headerAuthorization.FirstOrDefault() != _options.VerificationToken)
{
throw new AuthenticationException("Failed to validate incoming request. Mismatched verification token.");
}
string body;
using (var sr = new StreamReader(httpRequest.Body))
{
body = await sr.ReadToEndAsync();
}
var zoomRequest = JsonConvert.DeserializeObject<ZoomRequest>(body, JsonSerializerSettings);
var activity = RequestToActivity(zoomRequest);
using (var context = new TurnContext(this, activity))
{
await RunPipelineAsync(context, bot.OnTurnAsync, cancellationToken).ConfigureAwait(false);
var statusCode = Convert.ToInt32(context.TurnState.Get<string>("httpStatus"), CultureInfo.InvariantCulture);
var text = context.TurnState.Get<object>("httpBody") != null ? context.TurnState.Get<object>("httpBody").ToString() : string.Empty;
await WriteAsync(httpResponse, statusCode, text, Encoding.UTF8, cancellationToken).ConfigureAwait(false);
}
}
public override Task<ResourceResponse> UpdateActivityAsync(ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
{
return Task.FromException<ResourceResponse>(new NotImplementedException("Zoom adapter does not support updateActivity."));
}
public override Task DeleteActivityAsync(ITurnContext turnContext, ConversationReference reference, CancellationToken cancellationToken)
{
return Task.FromException(new NotImplementedException("Zoom adapter does not support deleteActivity."));
}
public virtual Activity RequestToActivity(ZoomRequest request)
{
return _requestMapper.RequestToActivity(request);
}
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
{
var message = _requestMapper.ActivityToZoom(activity);
var clientResponse = await _zoomClient.SendMessageAsync(message, cancellationToken).ConfigureAwait(false);
if (clientResponse.IsSuccessful)
{
responses.Add(new ResourceResponse() { Id = JObject.Parse(clientResponse.Content)["message_id"].ToString() });
}
else
{
_logger.LogError(clientResponse.ErrorException, $"Error sending message to Zoom. {clientResponse.ErrorMessage}");
}
}
}
return responses.ToArray();
}
public static async Task WriteAsync(HttpResponse response, int code, string text, Encoding encoding, CancellationToken cancellationToken)
{
if (response == null)
{
throw new ArgumentNullException(nameof(response));
}
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}
if (encoding == null)
{
throw new ArgumentNullException(nameof(encoding));
}
response.ContentType = "text/plain";
response.StatusCode = code;
var data = encoding.GetBytes(text);
await response.Body.WriteAsync(data, 0, data.Length, cancellationToken).ConfigureAwait(false);
}
}
}