forked from libardolara/fb-watson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
589 lines (523 loc) · 17.7 KB
/
main.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License'); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
'use strict';
const AssistantV1 = require('watson-developer-cloud/assistant/v1');
const DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
const VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
const cloudant = require('@cloudant/cloudant');
const request = require('request');
function errorResponse(reason) {
return {
text: 400,
message: reason || 'An unexpected error occurred. Please try again later.'
}
}
// Using some globals for now
let assistant;
let db;
let cloudantClient
let context;
let dbName = 'watson';
let doc;
let deleteContext;
/**
* Inicializa las principales variables para que se ejecute la aplicación
* Conexion con Watson Assistant y Conexion con la Base de Datos
*
* @args {JSON} Argumentos de inicio de la función
*/
function initClients(args) {
// Connect a client to Watson Assistant
if ( args.wa_api_key && args.wa_api_key !== "") {
assistant = new AssistantV1({
version: '2018-09-20',
iam_apikey: args.wa_api_key,
url: 'https://gateway.watsonplatform.net/assistant/api'
});
} else {
assistant = new AssistantV1({
version: '2018-09-20',
username: args.wa_username,
password: args.wa_password,
url: 'https://gateway.watsonplatform.net/assistant/api'
});
}
console.log('Connected to Watson Assistant');
// Connect a client to Cloudant
if (args.cloudant_url) {
cloudantClient = cloudant(args.cloudant_url);
} else {
console.error('Could not Connect to de Cloudant, please provide the url');
}
return new Promise(function(resolve, reject){
// check if DB exists if not create
cloudantClient.db.create(dbName, function(err, res) {
if (err) {
console.log('Could not create new db: ' + dbName + ', it might already exist.');
}
var a_db = cloudantClient.use(dbName);
resolve(a_db);
});
});
}
/**
* Realiza una consulta a Watson Discovery
*
* @wds_query {JSON} Consulta completa para ser enviada al WDS
* @params {Json} Parametros de Conexion al WDS
*
* @return - Json con la respuesta del WDS
*/
function callDiscovery(wds_query, params){
console.log("llamando a Discovery");
console.log(wds_query);
return new Promise(function(resolve, reject){
var discovery = new DiscoveryV1({
username: params.wds_username,
password: params.wds_password,
version_date: '2017-11-07'
});
discovery.query(wds_query,
function(error, data) {
if (error) {
console.error(error);
reject(error);
}else {
console.log(JSON.stringify(data, null, 2));
resolve(data);
}
});
})
}
/**
* Realiza una evaluacion de una Imagen contra el Visual Recognition
* Hace una clasificación de la imagen contra el modelo especificado en la variale vr_model_id del archivo de parametros
*
* @params {JSON} Resultado con la clasificacion de la imagen
*
* @return - status of post request sent to Facebook POST API
*/
function callVisualRecognition(params){
return new Promise(function(resolve, reject){
if (params.entry[0].messaging[0].message.attachments && params.entry[0].messaging[0].message.attachments[0].type == 'image') {
var postUrl = params.entry[0].messaging[0].message.attachments[0].payload.url;
var visualRecognition = new VisualRecognitionV3({
iam_apikey: params.vr_api_key,
version_date: '2018-03-19'
});
var classifier_ids = [params.vr_model_id];
var threshold = 0.5;
var args = {
url: postUrl,
classifier_ids: classifier_ids
};
visualRecognition.classify(args, function(err, response) {
if (err)
console.log(err);
else
console.log(JSON.stringify(response, null, 2));
context.images = response;
resolve("image");
});
}else {
var request = params.entry[0].messaging[0].message.text;
resolve(request);
}
})
}
/**
* Realiza el llamado al Watson Assistant
* Hace un llamado al WA con el texto de entrada
*
* @request {JSON} Resultado con la clasificacion de la imagen
* @workspaceId {string} Id del Workspace con la conversacion
*
* @return - Respuesta del Watson Assistant
*/
function assistantMessage(request, workspaceId) {
return new Promise(function(resolve, reject) {
const input = request ? request : '';
console.log('WORKSPACE_ID: ' + workspaceId);
console.log('Input text: ' + input);
assistant.message(
{
input: { text: input },
workspace_id: workspaceId,
context: context
},
function(err, watsonResponse) {
if (err) {
console.error(err);
reject('Error talking to Watson.');
} else {
console.log(watsonResponse);
context = watsonResponse.context; // Update global context
resolve(watsonResponse);
}
}
);
});
}
/**
* Realiza una evaluación de que accion viene desde el Watson Assistant
* Las acciones son procedimientos del back-end que vienen ordenados desde el WA
*
* @args {JSON} Parametros para conexiones
* @watsonResponse {JSON} Respuesta que vino desde WA
*
* @return - status of post request sent to Facebook POST API
*/
function actionHandler(args, watsonResponse) {
console.log('Begin actionHandler');
//console.log(args);
//console.log(watsonResponse);
return new Promise((resolve, reject) => {
switch (watsonResponse.output.action) {
// Hace una busqueda en el Discovery para buscar los mejores talleres
case 'wds_opiniones':
console.log("Calling action 'wds_opiniones'");
var wds_query = {
environment_id: args.wds_enviroment,
collection_id: args.wds_collection_reviews,
filter:'sentiment_label::"positive"',
aggregation:"term(taller,count:3)",
passages:false
};
return callDiscovery(wds_query, args)
.then(result => {
console.log("Tiene respuesta");
const talleres = result.aggregations[0].results.map( t => t.key + "(" + t.matching_results + ")" ).join(", ");
watsonResponse.output.text.push(talleres);
resolve(watsonResponse);
})
.catch( err => {
reject({
text: 400,
message: 'Error invocando wds_opiniones' + err
})}
);
// Realiza una consulta de Long-Tail sobre la cobertura de la poliza
case 'wds_cobertura':
console.log("Calling action 'wds_cobertura'")
//// TODO: Codigo para hacer una consulta en lenguage natural
// Indica que al finalizar no se debe guardar la conversacion y debe borrarse de la DB
case 'fn_chao':
deleteContext = true;
/* Other actions could be implemented with this switch or using watsonResponse values.
case "addMoreActionsHere":
return resolve(watsonResponse);
*/
default:
// No action. Resolve with watsonResponse as-is.
console.log('No action');
return resolve(watsonResponse);
}
});
}
/**
* Poste la respuesta de la convesacion al messenger usando el Facebook API https://graph.facebook.com/v2.6/me/messages
*
* @params {JSON} Parametros para el post de Facebook
* @postUrl {string} Url para postear la respuesta
* @accessToken {string} Token de autenticacion de la pagina en Facebook
*
* @return - Status del request al POST API
*/
function postFacebook(response, params, postUrl, accessToken) {
console.log('Entro a enviar a facebook');
const facebookParams = {
recipient: {
id: params.entry[0].messaging[0].sender.id
},
// Get payload for regular text message or interactive message
message: getMessageType(response)
};
return new Promise(function(resolve, reject){
request(
{
url: postUrl,
qs: { access_token: accessToken },
method: 'POST',
json: facebookParams
},
function(error, response) {
if (error) {
return reject(error.message);
}
if (response) {
if (response.statusCode === 200) {
// Facebook expects a "200" string/text response instead of a JSON.
// With Cloud Functions if we have to return a string/text, then we'd have to specify
// the field "text" and assign it a value that we'd like to return. In this case,
// the value to be returned is a statusCode.
return resolve({
text: response.statusCode,
params,
url: postUrl
});
}
return reject(
`Action returned with status code ${response.statusCode}, message: ${response.statusMessage}`
);
}
reject(`An unexpected error occurred when sending POST to ${postUrl}.`);
}
);
});
}
/**
* Cada vez que facebook usa el Webhook espera un texto "200"
*
* @return - Status del request al POST API
*/
function sendResponse(resolve, msg_s) {
console.log('Begin sendResponse');
console.log(msg_s)
// Everytime facebook pings the "receive" endpoint/webhook, it expects a
// "200" string/text response in return. In Cloud Functions, if we'd want to return
// a string response, then it's necessary that we add a field "text" and the
// response "200" as the value. The field "text" tells Cloud Functions that this
// endpoint must return a "text" response.
// Response code 200 only tells us that receive was able to execute it's code
// successfully but it doesn't really tell us if the sub-pipeline or the
// batched-messages pipeline that are invoked as a part of it returned a successful
// response or not. Hence, we return the activation id of the appropriate action so
// that the user can retrieve it's details for debugging purposes.
resolve({
text: 200,
message: `Response code 200 above only tells you that receive action was invoked successfully.
However, it does not really say if the Facebook API was invoked successfully. `
});
}
/**
* Evalua los mensajes para extraer el payload interactivo o el mensaje de texto
*
* @params {JSON} Parametros de la accion
* @return {JSON} - El archivo adjunto o el mensaje de texto
*/
function getMessageType(params) {
const interactiveMessage = params.output.facebook;
const textMessage = params.output.text.join(' ');
// If dialog node sends back output.facebook (used for interactive messages such as
// buttons and templates)
if (interactiveMessage) {
// An acceptable interactive JSON could either be of form -> output.facebook or
// output.facebook.message. Facebook's Send API accepts the "message" payload. So,
// if you already wrap your interactive message inside "message" object, then we
// accept it as-is. And if you don't wrap your interactive message inside "message"
// object, then the code wraps it for you.
if (interactiveMessage.message) {
console.log('Output interactive: ' + interactiveMessage.message);
return interactiveMessage.message;
}
console.log('Output interactive: ' + interactiveMessage);
return interactiveMessage;
}
console.log('Output text: ' + textMessage);
// if regular text message is received
return { text: textMessage };
}
/**
* Busca el historial de una conversación usando el ID de Session de Facebook
*
* @sessionID {String} ID de Session de Facebook
*
* @return - Json con la conversacion
*/
function getSessionContext(a_db, sessionId) {
console.log('sessionId: ' + sessionId);
db = a_db;
return new Promise(function(resolve, reject) {
db.find({selector:{_id:sessionId}}, function(err, result) {
if (err) {
console.error(err);
reject('Error getting context from Cloudant.');
}
context = result.docs[0] ? result.docs[0].context : {};
doc = result.docs[0] ? result.docs[0]: {};
console.log('context:');
console.log(context);
resolve();
});
});
}
/**
* Guarda la conversacion utilizando el ID de Session o la borra si la accion fue activada
*
* @sessionId {JSON} Parametros de la accion
*/
function saveSessionContext(sessionId) {
console.log('Begin saveSessionContext');
console.log('Session ID: ' + sessionId);
console.log('Delete Context: ' + deleteContext);
return new Promise(function(resolve, reject) {
if(deleteContext){
console.log('Begin action delete context ');
return deleteContextDB(sessionId)
.then(a_msg => {
return resolve(a_msg)
});
}else {
console.log('Begin insertUpdateContext');
return insertUpdateContext(sessionId)
.then(a_msg => {
return resolve(a_msg)
});
}
});
}
/**
* Borra la conversacion utilizando el ID de Session
*
* @sessionId {JSON} Parametros de la accion
*/
function deleteContextDB(sessionId){
return new Promise(function(resolve, reject) {
if(doc && doc._rev){
db.destroy(sessionId,doc._rev,function(err){
if(err){
console.error(err);
}else {
console.log('Deleted context in Cloudant');
}
return resolve("Borrado");
});
}else {
return resolve("No hay doc para borrar");
}
});
}
/**
* Guarda la conversacion utilizando el ID de Session
*
* @sessionId {JSON} Parametros de la accion
*/
function insertUpdateContext(sessionId){
return new Promise(function(resolve, reject) {
// Save the context in Cloudant. Can do this after resolve(response).
if (context) {
if(doc && doc._rev){
//Realiza un Update en Cloudant
db.insert({
_id: sessionId ,
_rev: doc._rev,
context: context
}, function(err, doc) {
if (err) {
console.error(err);
return resolve(err);
} else {
console.log('Updated context in Cloudant');
console.log(sessionId);
console.log(context);
return resolve("Actualizado");
}
});
}else{
//Realiza un Insert en Cloudant
db.insert({
_id: sessionId ,
context: context
}, function(err, doc) {
if (err) {
console.error(err);
return resolve(err);
} else{
console.log('Saved context in Cloudant');
console.log(sessionId);
console.log(context);
return resolve("Insertado");
}
});
}
}
});
}
/**
* Verifica si el llamado viene desde una URL de Verificación
*
* @args {JSON} Parametros de la accion
* @return {boolean} - true o false
*/
function isURLVerificationEvent(args) {
if (
args['hub.mode'] !== 'subscribe' ||
args['hub.verify_token'] !== args.fb_verification_token
) {
return false;
}
return true;
}
/**
* Verifica si object es de tipo "Page"
*
* @params {JSON} Parametros pasados a la accion
* @return {boolean} - true o false
*/
function isPageObject(params) {
if (!(params.object === 'page')) {
return false;
}
return true;
}
/**
* Receives a either a url-verification-type message or a regular page request from Facebook
* and returns the appropriate response depending on the type of event that is detected.
*
* @param {JSON} params - Facebook Callback API parameters as outlined by
* https://developers.facebook.com/docs/graph-api/webhooks#callback
* @return {Promise} - Result of the Facebook callback API
*/
function main(args) {
console.log('Begin action');
//console.log(args);
deleteContext = false;
return new Promise(function(resolve, reject) {
try {
if (isURLVerificationEvent(args)) {
// Challege value is returned
return resolve( { text: args['hub.challenge'] } );
} else if (isPageObject(args)) {
// Every time facebook makes a POST request to the webhook endpoint, it sends along
// x-hub-signature header which basically contains SHA1 key. In order to make sure, that
// the request is coming from facebook, it is important to calculate the HMAC key using
// app-secret and the request payload and compare it against the x-hub-signature header.
// TODO
const sessionId = args.entry[0].messaging[0].sender.id;
const postUrl = 'https://graph.facebook.com/v2.6/me/messages';
initClients(args)
.then(a_db => getSessionContext(a_db, sessionId))
.then(()=>callVisualRecognition(args))
.then(request => assistantMessage(request, args.wa_workspace_id))
.then(watsonResponse => actionHandler(args, watsonResponse))
.then(actionResponse => postFacebook(actionResponse, args, postUrl, args.fb_page_access_token))
.then(() => saveSessionContext(sessionId))
.then((msg_s) => sendResponse( resolve, msg_s))
.catch(err => { reject(errorResponse(err)) })
} else {
reject({
text: 400,
message: 'Neither a page type request nor a verfication type request detected'
});
}
} catch (err) {
console.error('Caught error: ');
console.log(err);
reject(errorResponse(err));
}
});
}
exports.main = main;