forked from IBM/watson-banking-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
741 lines (672 loc) · 28.1 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/**
* 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';
require('dotenv').config({
silent: true
});
const express = require('express'); // app server
const bodyParser = require('body-parser'); // parser for post requests
const watson = require('watson-developer-cloud'); // watson sdk
const fs = require('fs'); // file system for loading JSON
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
// const cfenv = require('cfenv');
// const url = require('url');
// const http = require('http');
// const https = require('https');
const numeral = require('numeral');
const vcapServices = require('vcap_services');
const bankingServices = require('./banking_services');
const WatsonDiscoverySetup = require('./lib/watson-discovery-setup');
const WatsonConversationSetup = require('./lib/watson-conversation-setup');
const DEFAULT_NAME = 'watson-banking-chatbot';
const DISCOVERY_ACTION = 'rnr'; // Replaced RnR w/ Discovery but Assistant action is still 'rnr'.
const DISCOVERY_DOCS = [
'./data/discovery/docs/BankFaqRnR-DB-Failure-General.docx',
'./data/discovery/docs/BankFaqRnR-DB-Terms-General.docx',
'./data/discovery/docs/BankFaqRnR-e2eAO-Terms.docx',
'./data/discovery/docs/BankFaqRnR-e2ePL-Terms.docx',
'./data/discovery/docs/BankRnR-OMP-General.docx'
];
const LOOKUP_BALANCE = 'balance';
const LOOKUP_TRANSACTIONS = 'transactions';
const LOOKUP_5TRANSACTIONS = '5transactions';
const app = express();
// Bootstrap application settings
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());
require('cf-deployment-tracker-client').track();
require('metrics-tracker-client').track();
// setupError will be set to an error message if we cannot recover from service setup or init error.
let setupError = '';
let discoveryCredentials = vcapServices.getCredentials('discovery');
let discoveryUrl = discoveryCredentials.url;
let discoveryUsername = discoveryCredentials.username;
let discoveryPassword = discoveryCredentials.password;
if (process.env.service_watson_discovery !== undefined) {
discoveryCredentials = JSON.parse(process.env.service_watson_discovery);
discoveryUrl = discoveryCredentials['url'];
discoveryUsername = discoveryCredentials['username'];
discoveryPassword = discoveryCredentials['password'];
}
const discovery = watson.discovery({
url: discoveryUrl,
username: discoveryUsername,
password: discoveryPassword,
version_date: '2017-10-16',
version: 'v1'
});
let discoveryParams; // discoveryParams will be set after Discovery is validated and setup.
const discoverySetup = new WatsonDiscoverySetup(discovery);
const discoverySetupParams = { default_name: DEFAULT_NAME, documents: DISCOVERY_DOCS };
discoverySetup.setupDiscovery(discoverySetupParams, (err, data) => {
if (err) {
handleSetupError(err);
} else {
console.log('Discovery is ready!');
discoveryParams = data;
}
});
// Create the service wrapper
let conversationCredentials = vcapServices.getCredentials('conversation');
let conversationUrl = conversationCredentials.url;
let conversationUsername = conversationCredentials.username;
let conversationPassword = conversationCredentials.password;
if (process.env.service_watson_discovery !== undefined) {
conversationCredentials = JSON.parse(process.env.service_watson_conversation);
conversationUrl = conversationCredentials['url'];
conversationUsername = conversationCredentials['username'];
conversationPassword = conversationCredentials['password'];
}
const conversation = watson.conversation({
url: conversationUrl,
username: conversationUsername,
password: conversationPassword,
version_date: '2016-07-11',
version: 'v1'
});
let workspaceID; // workspaceID will be set when the workspace is created or validated.
const conversationSetup = new WatsonConversationSetup(conversation);
const workspaceJson = JSON.parse(fs.readFileSync('data/conversation/workspaces/banking.json'));
const conversationSetupParams = { default_name: DEFAULT_NAME, workspace_json: workspaceJson };
conversationSetup.setupConversationWorkspace(conversationSetupParams, (err, data) => {
if (err) {
handleSetupError(err);
} else {
console.log('Watson Assistant is ready!');
workspaceID = data;
}
});
let toneAnalyzerCredentials = vcapServices.getCredentials('tone_analyzer');
let toneAnalyzerUrl = toneAnalyzerCredentials.url;
let toneAnalyzerUsername = toneAnalyzerCredentials.username;
let toneAnalyzerPassword = toneAnalyzerCredentials.password;
if (process.env.service_watson_discovery !== undefined) {
toneAnalyzerCredentials = JSON.parse(process.env.service_watson_tone_analyzer);
toneAnalyzerUrl = toneAnalyzerCredentials['url'];
toneAnalyzerUsername = toneAnalyzerCredentials['username'];
toneAnalyzerPassword = toneAnalyzerCredentials['password'];
}
const toneAnalyzer = watson.tone_analyzer({
url: toneAnalyzerUrl,
username: toneAnalyzerUsername,
password: toneAnalyzerPassword,
version: 'v3',
version_date: '2016-05-19'
});
/* ******** NLU ************ */
let naturalLanguageUnderstandingCredentials = vcapServices.getCredentials('natural-language-understanding');
let naturalLanguageUnderstandingUrl = naturalLanguageUnderstandingCredentials.url;
let naturalLanguageUnderstandingUsername = naturalLanguageUnderstandingCredentials.username;
let naturalLanguageUnderstandingPassword = naturalLanguageUnderstandingCredentials.password;
if (process.env.service_watson_discovery !== undefined) {
naturalLanguageUnderstandingCredentials = JSON.parse(process.env.service_watson_natural_language_understanding);
naturalLanguageUnderstandingUrl = naturalLanguageUnderstandingCredentials['url'];
naturalLanguageUnderstandingUsername = naturalLanguageUnderstandingCredentials['username'];
naturalLanguageUnderstandingPassword = naturalLanguageUnderstandingCredentials['password'];
}
const NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
const nlu = new NaturalLanguageUnderstandingV1({
url: naturalLanguageUnderstandingUrl,
username: naturalLanguageUnderstandingUsername,
password: naturalLanguageUnderstandingPassword,
version_date: '2017-02-27'
});
// Endpoint to be called from the client side
app.post('/api/message', function(req, res) {
if (setupError) {
return res.json({ output: { text: 'The app failed to initialize properly. Setup and restart needed.' + setupError } });
}
if (!workspaceID) {
return res.json({
output: {
text: 'Assistant initialization in progress. Please try again.'
}
});
}
bankingServices.getPerson(7829706, function(err, person) {
if (err) {
console.log('Error occurred while getting person data ::', err);
return res.status(err.code || 500).json(err);
}
const payload = {
workspace_id: workspaceID,
context: {
person: person
},
input: {}
};
// common regex patterns
const regpan = /^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$/;
// const regadhaar = /^\d{12}$/;
// const regmobile = /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;
if (req.body) {
if (req.body.input) {
let inputstring = req.body.input.text;
console.log('input string ', inputstring);
const words = inputstring.split(' ');
console.log('words ', words);
inputstring = '';
for (let i = 0; i < words.length; i++) {
if (regpan.test(words[i]) === true) {
// const value = words[i];
words[i] = '1111111111';
}
inputstring += words[i] + ' ';
}
// words.join(' ');
inputstring = inputstring.trim();
console.log('After inputstring ', inputstring);
// payload.input = req.body.input;
payload.input.text = inputstring;
}
if (req.body.context) {
// The client must maintain context/state
payload.context = req.body.context;
}
}
/* if (req.body) {
if (req.body.input) {
payload.input = req.body.input;
}
if (req.body.context) {
// The client must maintain context/state
payload.context = req.body.context;
}
} */
callconversation(payload);
});
/**
* Send the input to the conversation service.
* @param payload
*/
function callconversation(payload) {
const queryInput = JSON.stringify(payload.input);
// const context_input = JSON.stringify(payload.context);
toneAnalyzer.tone(
{
text: queryInput,
tones: 'emotion'
},
function(err, tone) {
let toneAngerScore = '';
if (err) {
console.log('Error occurred while invoking Tone analyzer. ::', err);
// return res.status(err.code || 500).json(err);
} else {
const emotionTones = tone.document_tone.tone_categories[0].tones;
const len = emotionTones.length;
for (let i = 0; i < len; i++) {
if (emotionTones[i].tone_id === 'anger') {
console.log('Input = ', queryInput);
console.log('emotion_anger score = ', 'Emotion_anger', emotionTones[i].score);
toneAngerScore = emotionTones[i].score;
break;
}
}
}
payload.context['tone_anger_score'] = toneAngerScore;
if (payload.input.text != '') {
// console.log('input text payload = ', payload.input.text);
const parameters = {
text: payload.input.text,
features: {
entities: {
emotion: true,
sentiment: true,
limit: 2
},
keywords: {
emotion: true,
sentiment: true,
limit: 2
}
}
};
nlu.analyze(parameters, function(err, response) {
if (err) {
console.log('error:', err);
} else {
const nluOutput = response;
payload.context['nlu_output'] = nluOutput;
// console.log('NLU = ', nlu_output);
// identify location
const entities = nluOutput.entities;
let location = entities.map(function(entry) {
if (entry.type == 'Location') {
return entry.text;
}
});
location = location.filter(function(entry) {
if (entry != null) {
return entry;
}
});
if (location.length > 0) {
payload.context['Location'] = location[0];
console.log('Location = ', payload.context['Location']);
} else {
payload.context['Location'] = '';
}
/*
// identify Company
let company = entities.map(function(entry) {
if (entry.type == 'Company') {
return entry.text;
}
});
company = company.filter(function(entry) {
if (entry != null) {
return entry;
}
});
if (company.length > 0) {
payload.context.userCompany = company[0];
} else {
delete payload.context.userCompany;
}
// identify Person
let person = entities.map(function(entry) {
if (entry.type == 'Person') {
return entry.text;
}
});
person = person.filter(function(entry) {
if (entry != null) {
return entry;
}
});
if (person.length > 0) {
payload.context.Person = person[0];
} else {
delete payload.context.Person;
}
// identify Vehicle
let vehicle = entities.map(function(entry) {
if (entry.type == 'Vehicle') {
return entry.text;
}
});
vehicle = vehicle.filter(function(entry) {
if (entry != null) {
return entry;
}
});
if (vehicle.length > 0) {
payload.context.userVehicle = vehicle[0];
} else {
delete payload.context.userVehicle;
}
// identify Email
let email = entities.map(function(entry) {
if(entry.type == 'EmailAddress') {
return(entry.text);
}
});
email = email.filter(function(entry) {
if(entry != null) {
return(entry);
}
});
if(email.length > 0) {
payload.context.userEmail = email[0];
} else {
delete payload.context.userEmail;
}
*/
}
conversation.message(payload, function(err, data) {
if (err) {
return res.status(err.code || 500).json(err);
} else {
console.log('conversation.message :: ', JSON.stringify(data));
// lookup actions
checkForLookupRequests(data, function(err, data) {
if (err) {
return res.status(err.code || 500).json(err);
} else {
return res.json(data);
}
});
}
});
});
} else {
conversation.message(payload, function(err, data) {
if (err) {
return res.status(err.code || 500).json(err);
} else {
console.log('conversation.message :: ', JSON.stringify(data));
return res.json(data);
}
});
}
}
);
}
});
/**
*
* Looks for actions requested by conversation service and provides the requested data.
*
**/
function checkForLookupRequests(data, callback) {
console.log('checkForLookupRequests');
if (data.context && data.context.action && data.context.action.lookup && data.context.action.lookup != 'complete') {
const payload = {
workspace_id: workspaceID,
context: data.context,
input: data.input
};
// conversation requests a data lookup action
if (data.context.action.lookup === LOOKUP_BALANCE) {
console.log('Lookup Balance requested');
// if account type is specified (checking, savings or credit card)
if (data.context.action.account_type && data.context.action.account_type != '') {
// lookup account information services and update context with account data
bankingServices.getAccountInfo(7829706, data.context.action.account_type, function(err, accounts) {
if (err) {
console.log('Error while calling bankingServices.getAccountInfo ', err);
callback(err, null);
return;
}
const len = accounts ? accounts.length : 0;
const appendAccountResponse = data.context.action.append_response && data.context.action.append_response === true ? true : false;
let accountsResultText = '';
for (let i = 0; i < len; i++) {
accounts[i].balance = accounts[i].balance ? numeral(accounts[i].balance).format('INR 0,0.00') : '';
if (accounts[i].available_credit)
accounts[i].available_credit = accounts[i].available_credit ? numeral(accounts[i].available_credit).format('INR 0,0.00') : '';
if (accounts[i].last_statement_balance)
accounts[i].last_statement_balance = accounts[i].last_statement_balance ? numeral(accounts[i].last_statement_balance).format('INR 0,0.00') : '';
if (appendAccountResponse === true) {
accountsResultText += accounts[i].number + ' ' + accounts[i].type + ' Balance: ' + accounts[i].balance + '<br/>';
}
}
payload.context['accounts'] = accounts;
// clear the context's action since the lookup was completed.
payload.context.action = {};
if (!appendAccountResponse) {
console.log('call conversation.message with lookup results.');
conversation.message(payload, function(err, data) {
if (err) {
console.log('Error while calling conversation.message with lookup result', err);
callback(err, null);
} else {
console.log('checkForLookupRequests conversation.message :: ', JSON.stringify(data));
callback(null, data);
}
});
} else {
console.log('append lookup results to the output.');
// append accounts list text to response array
if (data.output.text) {
data.output.text.push(accountsResultText);
}
// clear the context's action since the lookup and append was completed.
data.context.action = {};
callback(null, data);
}
});
}
} else if (data.context.action.lookup === LOOKUP_TRANSACTIONS) {
console.log('Lookup Transactions requested');
bankingServices.getTransactions(7829706, data.context.action.category, function(err, transactionResponse) {
if (err) {
console.log('Error while calling account services for transactions', err);
callback(err, null);
} else {
let responseTxtAppend = '';
if (data.context.action.append_total && data.context.action.append_total === true) {
responseTxtAppend += 'Total = <b>' + numeral(transactionResponse.total).format('INR 0,0.00') + '</b>';
}
if (transactionResponse.transactions && transactionResponse.transactions.length > 0) {
// append transactions
const len = transactionResponse.transactions.length;
const sDt = new Date(data.context.action.startdt);
const eDt = new Date(data.context.action.enddt);
if (sDt && eDt) {
for (let i = 0; i < len; i++) {
const transaction = transactionResponse.transactions[i];
const tDt = new Date(transaction.date);
if (tDt > sDt && tDt < eDt) {
if (data.context.action.append_response && data.context.action.append_response === true) {
responseTxtAppend +=
'<br/>' + transaction.date + ' ' + numeral(transaction.amount).format('INR 0,0.00') + ' ' + transaction.description;
}
}
}
} else {
for (let i = 0; i < len; i++) {
const transaction1 = transactionResponse.transactions[i];
if (data.context.action.append_response && data.context.action.append_response === true) {
responseTxtAppend +=
'<br/>' + transaction1.date + ' ' + numeral(transaction1.amount).format('INR 0,0.00') + ' ' + transaction1.description;
}
}
}
if (responseTxtAppend != '') {
console.log('append lookup transaction results to the output.');
if (data.output.text) {
data.output.text.push(responseTxtAppend);
}
// clear the context's action since the lookup and append was completed.
data.context.action = {};
}
callback(null, data);
// clear the context's action since the lookup was completed.
payload.context.action = {};
return;
}
}
});
} else if (data.context.action.lookup === LOOKUP_5TRANSACTIONS) {
console.log('Lookup Transactions requested');
bankingServices.getTransactions(7829706, data.context.action.category, function(err, transactionResponse) {
if (err) {
console.log('Error while calling account services for transactions', err);
callback(err, null);
} else {
let responseTxtAppend = '';
if (data.context.action.append_total && data.context.action.append_total === true) {
responseTxtAppend += 'Total = <b>' + numeral(transactionResponse.total).format('INR 0,0.00') + '</b>';
}
transactionResponse.transactions.sort(function(a1, b1) {
const a = new Date(a1.date);
const b = new Date(b1.date);
return a > b ? -1 : a < b ? 1 : 0;
});
if (transactionResponse.transactions && transactionResponse.transactions.length > 0) {
// append transactions
const len = 5; // transaction_response.transactions.length;
for (let i = 0; i < len; i++) {
const transaction = transactionResponse.transactions[i];
if (data.context.action.append_response && data.context.action.append_response === true) {
responseTxtAppend +=
'<br/>' + transaction.date + ' ' + numeral(transaction.amount).format('INR 0,0.00') + ' ' + transaction.description;
}
}
}
if (responseTxtAppend != '') {
console.log('append lookup transaction results to the output.');
if (data.output.text) {
data.output.text.push(responseTxtAppend);
}
// clear the context's action since the lookup and append was completed.
data.context.action = {};
}
callback(null, data);
// clear the context's action since the lookup was completed.
payload.context.action = {};
return;
}
});
} else if (data.context.action.lookup === 'branch') {
console.log('************** Branch details *************** InputText : ' + payload.input.text);
const loc = data.context.action.Location.toLowerCase();
bankingServices.getBranchInfo(loc, function(err, branchMaster) {
if (err) {
console.log('Error while calling bankingServices.getAccountInfo ', err);
callback(err, null);
return;
}
const appendBranchResponse = data.context.action.append_response && data.context.action.append_response === true ? true : false;
let branchText = '';
if (appendBranchResponse === true) {
if (branchMaster != null) {
branchText =
'Here are the branch details at ' +
branchMaster.location +
' <br/>Address: ' +
branchMaster.address +
'<br/>Phone: ' +
branchMaster.phone +
'<br/>Operation Hours: ' +
branchMaster.hours +
'<br/>';
} else {
branchText = "Sorry currently we don't have branch details for " + data.context.action.Location;
}
}
payload.context['branch'] = branchMaster;
// clear the context's action since the lookup was completed.
payload.context.action = {};
if (!appendBranchResponse) {
console.log('call conversation.message with lookup results.');
conversation.message(payload, function(err, data) {
if (err) {
console.log('Error while calling conversation.message with lookup result', err);
callback(err, null);
} else {
console.log('checkForLookupRequests conversation.message :: ', JSON.stringify(data));
callback(null, data);
}
});
} else {
console.log('append lookup results to the output.');
// append accounts list text to response array
if (data.output.text) {
data.output.text.push(branchText);
}
// clear the context's action since the lookup and append was completed.
data.context.action = {};
callback(null, data);
}
});
} else if (data.context.action.lookup === DISCOVERY_ACTION) {
console.log('************** Discovery *************** InputText : ' + payload.input.text);
let discoveryResponse = '';
if (!discoveryParams) {
console.log('Discovery is not ready for query.');
discoveryResponse = 'Sorry, currently I do not have a response. Discovery initialization is in progress. Please try again later.';
if (data.output.text) {
data.output.text.push(discoveryResponse);
}
// Clear the context's action since the lookup and append was attempted.
data.context.action = {};
callback(null, data);
// Clear the context's action since the lookup was attempted.
payload.context.action = {};
} else {
const queryParams = {
natural_language_query: payload.input.text,
passages: true
};
Object.assign(queryParams, discoveryParams);
discovery.query(queryParams, (err, searchResponse) => {
discoveryResponse = 'Sorry, currently I do not have a response. Our Customer representative will get in touch with you shortly.';
if (err) {
console.error('Error searching for documents: ' + err);
} else if (searchResponse.passages.length > 0) {
const bestPassage = searchResponse.passages[0];
console.log('Passage score: ', bestPassage.passage_score);
console.log('Passage text: ', bestPassage.passage_text);
// Trim the passage to try to get just the answer part of it.
const lines = bestPassage.passage_text.split('\n');
let bestLine;
let questionFound = false;
for (let i = 0, size = lines.length; i < size; i++) {
const line = lines[i].trim();
if (!line) {
continue; // skip empty/blank lines
}
if (line.includes('?') || line.includes('<h1')) {
// To get the answer we needed to know the Q/A format of the doc.
// Skip questions which either have a '?' or are a header '<h1'...
questionFound = true;
continue;
}
bestLine = line; // Best so far, but can be tail of earlier answer.
if (questionFound && bestLine) {
// We found the first non-blank answer after the end of a question. Use it.
break;
}
}
discoveryResponse =
bestLine || 'Sorry I currently do not have an appropriate response for your query. Our customer care executive will call you in 24 hours.';
}
if (data.output.text) {
data.output.text.push(discoveryResponse);
}
// Clear the context's action since the lookup and append was completed.
data.context.action = {};
callback(null, data);
// Clear the context's action since the lookup was completed.
payload.context.action = {};
});
}
} else {
callback(null, data);
return;
}
} else {
callback(null, data);
return;
}
}
/**
* Handle setup errors by logging and appending to the global error text.
* @param {String} reason - The error message for the setup error.
*/
function handleSetupError(reason) {
setupError += ' ' + reason;
console.error('The app failed to initialize properly. Setup and restart needed.' + setupError);
// We could allow our chatbot to run. It would just report the above error.
// Or we can add the following 2 lines to abort on a setup error allowing Bluemix to restart it.
console.error('\nAborting due to setup error!');
process.exit(1);
}
module.exports = app;