-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathWebexClientWrapper.cs
230 lines (193 loc) · 10.3 KB
/
WebexClientWrapper.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Thrzn41.WebexTeams;
using Thrzn41.WebexTeams.Version1;
namespace Bot.Builder.Community.Adapters.Webex
{
/// <summary>
/// A client for interacting with the Webex Teams API.
/// </summary>
public class WebexClientWrapper
{
private const string MessageUrl = "https://api.ciscospark.com/v1/messages";
private const string ActionsUrl = "https://api.ciscospark.com/v1/attachment/actions";
private const string SparkSignature = "x-spark-signature";
private readonly TeamsAPIClient _api;
/// <summary>
/// Initializes a new instance of the <see cref="WebexClientWrapper"/> class.
/// Creates a Webex Client Wrapper. See <see cref="WebexClientWrapperOptions"/> for a full definition of the allowed parameters.
/// </summary>
/// <param name="options">An object containing API credentials, a webhook verification token and other options.</param>
public WebexClientWrapper(WebexClientWrapperOptions options)
{
Options = options ?? throw new ArgumentNullException(nameof(options));
if (string.IsNullOrWhiteSpace(Options.WebexAccessToken))
{
throw new ArgumentException(nameof(options.WebexAccessToken));
}
if (Options.WebexPublicAddress == null)
{
throw new ArgumentException(nameof(options.WebexPublicAddress));
}
_api = TeamsAPI.CreateVersion1Client(Options.WebexAccessToken);
}
/// <summary>
/// Gets the options collection for the adapter.
/// </summary>
/// <value>A WebexClientWrapperOptions class exposing properties for each of the available options.</value>
public WebexClientWrapperOptions Options { get; }
/// <summary>
/// Validates the local secret against the one obtained from the request header.
/// </summary>
/// <param name="request">The <see cref="HttpRequest"/> with the signature.</param>
/// <param name="jsonPayload">The serialized payload to be use for comparison.</param>
/// <returns>The result of the comparison between the signature in the request and hashed json.</returns>
public virtual bool ValidateSignature(HttpRequest request, string jsonPayload)
{
var signature = request.Headers.ContainsKey(SparkSignature)
? request.Headers[SparkSignature].ToString().ToUpperInvariant()
: throw new InvalidOperationException($"HttpRequest is missing \"{SparkSignature}\"");
#pragma warning disable CA5350 // Webex API uses SHA1 as cryptographic algorithm.
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(Options.WebexSecret)))
{
var hashArray = hmac.ComputeHash(Encoding.UTF8.GetBytes(jsonPayload));
var hash = BitConverter.ToString(hashArray).Replace("-", string.Empty).ToUpperInvariant();
return signature == hash;
}
#pragma warning restore CA5350 // Webex API uses SHA1 as cryptographic algorithm.
}
/// <summary>
/// Wraps Webex API's CreateMessageAsync method.
/// </summary>
/// <param name="recipient">Target id of the message.</param>
/// <param name="text">Text of the message.</param>
/// <param name="files">List of files attached to the message.</param>
/// <param name="messageType">Type of message. It can be Text or Markdown.</param>
/// <param name="target">Target for the message.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>The created message id.</returns>
public virtual async Task<string> CreateMessageAsync(string recipient, string text, IList<Uri> files = null, MessageTextType messageType = MessageTextType.Markdown, MessageTarget target = MessageTarget.PersonId, CancellationToken cancellationToken = default)
{
var webexResponse = await _api.CreateMessageAsync(recipient, text, files, target, messageType, cancellationToken: cancellationToken).ConfigureAwait(false);
return webexResponse.Data.Id;
}
/// <summary>
/// Wraps Webex API's DeleteMessageAsync method.
/// </summary>
/// <param name="messageId">The id of the message to be deleted.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public virtual async Task DeleteMessageAsync(string messageId, CancellationToken cancellationToken)
{
await _api.DeleteMessageAsync(messageId, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Creates a message with attachments.
/// </summary>
/// <param name="recipient">PersonId, email or roomId of the message.</param>
/// <param name="textOrMarkdown">Text or markdown of the message.</param>
/// <param name="attachments">List of attachments attached to the message.</param>
/// <param name="messageType">Type of the message. It can be Text or Markdown.</param>
/// <param name="target">Target for the message.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>The created message id.</returns>
public virtual async Task<string> CreateMessageWithAttachmentsAsync(string recipient, string textOrMarkdown, IList<Attachment> attachments, MessageTextType messageType = MessageTextType.Markdown, MessageTarget target = MessageTarget.PersonId, CancellationToken cancellationToken = default)
{
Message result;
var attachmentsContent = new List<object>();
foreach (var attach in attachments)
{
attachmentsContent.Add(attach.Content);
}
var text = textOrMarkdown ?? string.Empty;
string markdown = null;
if (!string.IsNullOrEmpty(textOrMarkdown) && messageType == MessageTextType.Markdown)
{
markdown = textOrMarkdown;
text = Shared.MarkdownToPlaintextRenderer.Render(textOrMarkdown);
}
var request = new WebexMessageRequest
{
RoomId = target == MessageTarget.SpaceId ? recipient : null,
ToPersonId = target == MessageTarget.SpaceId ? null : recipient,
Text = text,
Markdown = markdown,
Attachments = attachmentsContent.Count > 0 ? attachmentsContent : null,
};
var http = (HttpWebRequest)WebRequest.Create(new Uri(MessageUrl));
http.PreAuthenticate = true;
http.Headers.Add("Authorization", "Bearer " + Options.WebexAccessToken);
http.Accept = "application/json";
http.ContentType = "application/json" +
"; charset=utf-8";
http.Method = "POST";
var parsedContent = JsonConvert.SerializeObject(request);
var encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(parsedContent);
var newStream = await http.GetRequestStreamAsync().ConfigureAwait(false);
await newStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);
newStream.Close();
var response = await http.GetResponseAsync().ConfigureAwait(false);
var stream = response.GetResponseStream();
using (var sr = new StreamReader(stream))
{
var content = await sr.ReadToEndAsync().ConfigureAwait(false);
result = JsonConvert.DeserializeObject<Message>(content);
}
return result.Id;
}
/// <summary>
/// Shows details for an attachment action, by ID.
/// </summary>
/// <param name="actionId">An unique identifier for the attachment action.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>The attachment action details.</returns>
public virtual async Task<Message> GetAttachmentActionAsync(string actionId, CancellationToken cancellationToken)
{
Message result;
var url = $"{ActionsUrl}/{actionId}";
var http = (HttpWebRequest)WebRequest.Create(new Uri(url));
http.PreAuthenticate = true;
http.Headers.Add("Authorization", "Bearer " + Options.WebexAccessToken);
http.Method = "GET";
var response = await http.GetResponseAsync().ConfigureAwait(false);
var stream = response.GetResponseStream();
using (var sr = new StreamReader(stream))
{
var content = await sr.ReadToEndAsync().ConfigureAwait(false);
result = JsonConvert.DeserializeObject<Message>(content);
}
return result;
}
/// <summary>
/// Wraps Webex API's GetMeAsync method.
/// </summary>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>The <see cref="Person"/> object associated with the bot.</returns>
public virtual async Task<Person> GetMeAsync(CancellationToken cancellationToken)
{
var resultPerson = await _api.GetMeAsync(cancellationToken).ConfigureAwait(false);
return resultPerson.GetData(false);
}
/// <summary>
/// Wraps Webex API's GetMessageAsync method.
/// </summary>
/// <param name="messageId">Id of the message to be recovered.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>The message's data.</returns>
public virtual async Task<Message> GetMessageAsync(string messageId, CancellationToken cancellationToken)
{
var message = await _api.GetMessageAsync(messageId, cancellationToken).ConfigureAwait(false);
return message.GetData(false);
}
}
}