-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
441 lines (406 loc) · 13.3 KB
/
app.js
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
'use strict';
const restify = require('restify');
const builder = require('botbuilder');
const oxford = require('project-oxford');
const fs = require('fs');
const request = require('request');
const azure = require('azure-storage');
const stream = require('stream');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3979, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
server.get('/', restify.serveStatic({
'directory': __dirname,
'default': 'index.html'
}));
var client = new oxford.Client(process.env.MICROSOFT_FACEAPI_KEY);
server.use(restify.queryParser());
server.use(restify.bodyParser());
// Seed images
var faceUrls = [];
faceUrls.push({url: "https://raw.githubusercontent.com/ritazh/facedetect-bot/master/images/face.jpeg", contact: "@BillGates", faceid: ""});
faceUrls.push({url: "https://raw.githubusercontent.com/ritazh/facedetect-bot/master/images/face3.jpeg", contact: "@ritazzhang", faceid: ""});
faceUrls.push({url: "https://raw.githubusercontent.com/ritazh/facedetect-bot/master/images/face7.jpeg", contact: "@bhargav", faceid: ""});
faceUrls.push({url: "https://raw.githubusercontent.com/ritazh/facedetect-bot/master/images/face4.jpeg", contact: "@sedouard", faceid: ""});
var faceListId = "facedetectbot";
var faceList = null;
//storage setup
var blobClient = azure.createBlobService();
var containerName = "facedetectbot"
var hostName = 'https://' + process.env.AZURE_STORAGE_ACCOUNT + '.blob.core.windows.net';
var uploadOptions = {
blockIdPrefix: "block",
useTransactionalMD5: true,
storeBlobContentMD5: true,
contentSettings: {contentType: 'image/jpeg'}
};
//=========================================================
// Bots Dialogs
//=========================================================
// bot.on('conversationUpdate', function (message) {
// if (message.membersAdded) {
// message.membersAdded.forEach(function (identity) {
// if (identity.id === message.address.bot.id) {
// bot.beginDialog(message.address, '/');
// }
// });
// }
// });
bot.dialog('/', [
(session) => {
session.send('Welcome! I am Face Detection Bot! Upload a user picture and I will find a match for you.');
session.send('Here are some users in our database:');
session.userData.faces =[];
session.userData.faceids = [];
getFaceList(function(err){
if(!err){
if (faceList && faceList.persistedFaces.length > 0){
displayFaces(session, function(msg){
builder.Prompts.confirm(session, "Would you like to upload a user picture to find a match?");
});
}else{
builder.Prompts.confirm(session, "Something went wrong. Would you like to try again?");
}
}else{
session.send("Something went wrong. " + err);
builder.Prompts.confirm(session, "Something went wrong. Would you like to try again?");
}
});
},
(session, results, next) => {
if (results.response){
session.beginDialog('/findmatch');
}else{
next();
}
},
(session) => {
var msg = new builder.Message(session)
.attachments([{
contentType: "image/jpeg",
contentUrl: "http://www.theoldrobots.com/images62/Bender-18.JPG"
}]);
session.send(msg);
session.endConversation("Goodbye until next time...");
}
]);
bot.dialog('/findmatch', [
(session) => {
builder.Prompts.attachment(session, "Please upload a user picture.");
},
(session, results, next) => {
session.sendTyping();
console.log(results);
session.userData.newUserImageUrl = null;
session.userData.newUserFaceId = null;
results.response.forEach(function (attachment) {
getFile(attachment, function(error, body){
if(!error){
findMatch(session, body, function(msg, userfaceid, found){
session.send(msg);
if (!found){
saveFile(body, function(error, fileurl){
if (!error){
session.userData.newUserImageUrl = fileurl;
session.userData.newUserFaceId = userfaceid;
session.beginDialog('/adduser');
}else{
session.send("Error encountered while saving the picture: " + error);
next();
}
});
}else{
next();
}
});
}else{
session.send("Error encountered while getting the picture: " + error);
next();
}
});
});
},
(session, results) => {
builder.Prompts.confirm(session, "Would you like to try another picture?");
},
(session, results) => {
if (results.response){
session.replaceDialog('/findmatch');
}else{
session.endDialog();
}
}
]);
bot.dialog('/adduser', [
(session) => {
builder.Prompts.confirm(session, "Would you like to add this user?");
},
(session, results, next) => {
if (results.response){
session.beginDialog('/addcontact');
}else{
next();
}
},
(session, results) => {
session.endDialog();
}
]);
bot.dialog('/addcontact', [
(session) => {
builder.Prompts.text(session, "Please enter the user's twitter handle starting with '@'");
},
(session, results, next) => {
console.log(results.response);
if (results.response){
if(validateTwitter(results.response)){
var contact = results.response;
var newFace = {url: session.userData.newUserImageUrl, contact: contact, faceid: session.userData.newUserFaceId};
//add to facelist
addFaceToList(newFace, function(err){
if(!err){
console.log("Added new user:" + contact);
session.send("New user has been added successfully!");
session.userData.newUserFaceId = null;
session.userData.newUserImageUrl = null;
//refresh list and display latest
getFaceList(function(err){
if (!err){
displayFaces(session, function(msg){
next();
});
}else{
session.send("Error detected while trying to add this user. " + err);
session.userData.newUserFaceId = null;
session.userData.newUserImageUrl = null;
next();
}
});
}else{
session.send("Error detected while trying to add this user. " + err);
session.userData.newUserFaceId = null;
session.userData.newUserImageUrl = null;
next();
}
});
}else{
session.send("Invalid twitter handle");
session.replaceDialog('/addcontact');
}
}else{
//this shouldnt happen
session.replaceDialog('/addcontact');
}
},
(session, results) => {
session.endDialog();
}
]);
function displayFaces(session, callback){
var msg = "";
if (faceList.persistedFaces.length > 0){
var attachments = [];
var processed = 0;
faceList.persistedFaces.forEach(function (persistedFace) {
//breaking this into batches as skype only supports up to 10 items in carousel
var userData = JSON.parse(persistedFace.userData);
attachments.push(createAttachment(session, userData.contact, '', userData.url));
processed++;
if(processed == faceList.persistedFaces.length || attachments.length > 4){
msg = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(attachments.reverse());
session.send(msg);
attachments = [];
}
if(processed == faceList.persistedFaces.length){
callback(msg);
}
});
}
}
// get facelist
function getFaceList(callback){
client.face.faceList.get(faceListId).then(function (response) {
if(response && response.faceListId === faceListId){
faceList = response;
callback(null);
}else{
createFaceList(callback);
}
}).catch(function (error) {
console.log(JSON.stringify(error));
if(error.code === "FaceListNotFound"){
createFaceList(callback);
}
});
}
function createFaceList(callback){
client.face.faceList.create(faceListId, {
name: faceListId
}).then(function (response) {
//Add seed images to faceList
var addedImages = 0;
faceUrls.forEach(function(faceUrl){
addFaceToList(faceUrl, function(err){
if(!err){
addedImages++;
if(addedImages == faceUrls.length){
client.face.faceList.get(faceListId).then(function (response) {
if(response && response.faceListId === faceListId){
faceList = response;
callback(null);
}
}).catch(function (error) {
console.log(JSON.stringify(error));
callback(error);
});
}
}else{
callback(err);
}
});
});
});
}
function getFaceFromList(persistedFaceId){
var face = null;
faceList.persistedFaces.forEach(function (persistedFace) {
if (persistedFace.persistedFaceId === persistedFaceId){
var userData = JSON.parse(persistedFace.userData);
face = {url: userData.url, contact: userData.contact, faceid: persistedFace.persistedFaceId};
console.log(face);
}
});
return face;
}
function addFaceToList(faceUrl, callback){
var userData = JSON.stringify({contact: faceUrl.contact, url: faceUrl.url});
console.log(userData);
client.face.faceList.addFace(faceListId, {
url: faceUrl.url,
userData: userData})
.then(function (response) {
faceUrl.faceid = response.persistedFaceId;
callback(null);
})
.catch(function (error) {
console.log(JSON.stringify(error));
callback(error);
})
}
// check if attachment is skype attachment
function isSkypeAttachment(url){
return url.startsWith("https://apis.skype.com/v2/attachments");
}
// create new attachment
function createAttachment(session, title, subtitle, url){
var attachment = new builder.HeroCard(session)
.title(title)
.subtitle(subtitle)
.images([
builder.CardImage.create(session, url)
]);
return attachment;
}
function detectFace(url, callback){
client.face.detect({
url: url,
returnFaceId: true,
analyzesAge: false,
analyzesGender: false
}).then(function (response) {
console.log('The faceid is: ' + response[0].faceId);
callback(response);
});
}
function saveFile(body, callback){
var bufferStream = new stream.PassThrough();
bufferStream.end(body);
var fileName = new Date().getTime() + '.jpeg';
blobClient.createAppendBlobFromStream(containerName, fileName, bufferStream, body.length, uploadOptions, function(error, blob){
if(error){
console.log(error);
callback(error, null);
}else{
var fileurl = blobClient.getUrl(containerName, fileName, null, hostName);
callback(null, fileurl);
}
});
}
function getFile(attachment, callback){
var fileurl = attachment.contentUrl;
var headers = {};
if(isSkypeAttachment(fileurl)){
connector.getAccessToken(
function(error, token) {
headers['Authorization'] = 'Bearer ' + token;
headers['Content-Type'] = 'application/octet-stream';
}
);
}
else{
headers['Content-Type'] = attachment.contentType;
}
request({
url: fileurl,
method: 'get',
encoding: null,
headers: headers
},
function(error, response, body){
callback(error, body);
}
);
}
function validateEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return newRegExp(re).test(email);
}
function validateTwitter(twitter){
var re = /(^|[^@\w])@(\w{1,15})\b/;
return re.test(twitter);
}
function findMatch(session, body, callback){
client.face.detect({
data: body,
returnFaceId: true,
analyzesAge: false,
analyzesGender: false
}).then(function (response) {
console.log('The faceid is: ' + response[0].faceId);
var userfaceid = response[0].faceId;
var matchfound = false;
var matchcontact;
var msg = '';
client.face.similar(userfaceid, {
candidateFaceListId: faceListId
}).then(function(response) {
if(response.length > 0){
matchcontact = getFaceFromList(response[0].persistedFaceId);
if(matchcontact){
matchfound = true;
msg = "We've found a matching user with " + Math.floor(response[0].confidence * 100) + '% confidence.';
msg = msg + " Here's the contact info: " + matchcontact.contact;
callback(msg, userfaceid, true);
}
}
if (!matchfound){
msg = 'Sorry no match found for this user.';
callback(msg, userfaceid, false);
}
});
});
}