-
Notifications
You must be signed in to change notification settings - Fork 0
/
WHFunction_ContentModeration.cs
226 lines (192 loc) · 8.11 KB
/
WHFunction_ContentModeration.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
using Microsoft.Azure.CognitiveServices.ContentModerator;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using WHFunction;
namespace SPWebHook_AzureFunction_ContentModeration
{
public static class WHFunction_ContentModeration
{
[FunctionName("WHFunction_ContentModeration")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous,
"post", Route = null)]HttpRequestMessage req,
TraceWriter log)
{
log.Info($"WebHook Function called at: { DateTime.Now }");
// WebHook Registration
string validationToken = WHHelpRoutines.GetValidationToken(req, log);
if (validationToken != null)
return WHHelpRoutines.RegisterWebHook(req, validationToken, log);
else
log.Info($"---- Registration: Registering WebHook already done");
// Changes
var myContent = await req.Content.ReadAsStringAsync();
var allNotifications = JsonConvert.DeserializeObject<ResponseModel<NotificationModel>>(myContent).Value;
if (allNotifications.Count > 0)
{
foreach (var oneNotification in allNotifications)
{
// Login in SharePoint
string baseUrl = ConfigurationManager.AppSettings["whSpBaseUrl"];
ClientContext SPClientContext = WHHelpRoutines.LoginSharePoint(baseUrl, log);
// Get the Changes
GetChanges(SPClientContext, oneNotification.Resource, log);
}
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
static void GetChanges(ClientContext SPClientContext, string ListId, TraceWriter log)
{
// Get the List where the WebHook is working
List myListWebHook = WHHelpRoutines.GetListWebHook(SPClientContext, log);
// Get the Change Query
ChangeQuery myChangeQuery = WHHelpRoutines.GetChangeQueryNew(ListId, log); // Only new items and for the last one minute
// Get all the Changes
ChangeCollection allChanges = WHHelpRoutines.GetAllChanges(myListWebHook, myChangeQuery, SPClientContext, log);
foreach (Change oneChange in allChanges)
{
if (oneChange is ChangeItem)
{
// Get what is changed
ListItem itemChanged = WHHelpRoutines.GetItemChanged(myListWebHook, oneChange, SPClientContext, log);
log.Info($"itemChangedID - " + itemChanged.Id.ToString());
// Do something with the Item Changed
DoSomething(SPClientContext, itemChanged, log);
}
}
}
static void DoSomething(ClientContext SPClientContext, ListItem ItemChanged, TraceWriter log)
{
string disBody = string.Empty;
// Read the post Body
disBody = ItemChanged["Body"].ToString();
log.Info($"---- Body: " + disBody);
// Make a stream of the body text
byte[] disBodyByteArray = System.Text.Encoding.UTF8.GetBytes(disBody);
MemoryStream disBodyStream = new MemoryStream(disBodyByteArray);
log.Info($"---- Body For Moderation: " + disBody);
// Get the Moderation from Azure and modify the Body
AzureContentModerationResults myModeration = CheckContentForModeration(disBodyStream);
string myModifiedBody = ModifyText(myModeration.AutoCorrectedText, myModeration);
log.Info($"---- Body Moderated: " + myModifiedBody);
// Send back the modified Body to SharePoint
ItemChanged["Body"] = myModifiedBody;
ItemChanged.Update();
SPClientContext.ExecuteQuery();
log.Info($"---- Item Moderated");
}
static AzureContentModerationResults CheckContentForModeration(MemoryStream TextToModerate)
{
string AzureContModBaseURL = "https://westeurope.api.cognitive.microsoft.com";
string ContModSubscriptionKey = "dabb15da19a14baea06a71bfxxxxxxxx";
ContentModeratorClient contModClient = new ContentModeratorClient(new ApiKeyServiceClientCredentials(ContModSubscriptionKey));
contModClient.Endpoint = AzureContModBaseURL;
var objResult = contModClient.TextModeration.ScreenText("text/plain", TextToModerate, "eng", true, true, null, true);
string jsonResult = JsonConvert.SerializeObject(objResult, Formatting.Indented);
return JsonConvert.DeserializeObject<AzureContentModerationResults>(jsonResult);
}
static string ModifyText(string InputString, AzureContentModerationResults ModerationResult)
{
foreach (Term oneWord in ModerationResult.Terms)
{
string termModified = oneWord.term.Remove(1);
for (int charCount = 1; charCount < oneWord.term.Length; charCount++)
{
termModified += "*";
}
InputString = InputString.Replace(oneWord.term, termModified);
}
return InputString;
}
}
public class AzureContentModerationResults
{
public string OriginalText { get; set; }
public string NormalizedText { get; set; }
public string AutoCorrectedText { get; set; }
public object Misrepresentation { get; set; }
public Classification Classification { get; set; }
public Status Status { get; set; }
public PII PII { get; set; }
public string Language { get; set; }
public Term[] Terms { get; set; }
public string TrackingId { get; set; }
}
public class Classification
{
public Category1 Category1 { get; set; }
public Category2 Category2 { get; set; }
public Category3 Category3 { get; set; }
public bool ReviewRecommended { get; set; }
}
public class Category1
{
public float Score { get; set; }
}
public class Category2
{
public float Score { get; set; }
}
public class Category3
{
public float Score { get; set; }
}
public class Status
{
public int Code { get; set; }
public string Description { get; set; }
public object Exception { get; set; }
}
public class PII
{
public Email[] Email { get; set; }
public SSN[] SSN { get; set; }
public IPA[] IPA { get; set; }
public Phone[] Phone { get; set; }
public Address[] Address { get; set; }
}
public class Email
{
public string Detected { get; set; }
public string SubType { get; set; }
public string Text { get; set; }
public int Index { get; set; }
}
public class SSN
{
public string Text { get; set; }
public int Index { get; set; }
}
public class IPA
{
public string SubType { get; set; }
public string Text { get; set; }
public int Index { get; set; }
}
public class Phone
{
public string CountryCode { get; set; }
public string Text { get; set; }
public int Index { get; set; }
}
public class Address
{
public string Text { get; set; }
public int Index { get; set; }
}
public class Term
{
public int Index { get; set; }
public int OriginalIndex { get; set; }
public int ListId { get; set; }
public string term { get; set; }
}
}