This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 375
/
app.js
637 lines (573 loc) · 24.2 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
/**
* 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 numeral = require('numeral');
const fs = require('fs'); // file system for loading JSON
const AssistantV1 = require('ibm-watson/assistant/v1');
const DiscoveryV1 = require('ibm-watson/discovery/v1');
const NaturalLanguageUnderstandingV1 = require('ibm-watson/natural-language-understanding/v1.js');
const { getAuthenticatorFromEnvironment } = require('ibm-watson/auth');
let auth;
// need to manually set url and disableSslVerification to get around
// current Cloud Pak for Data SDK issue IF user uses
// `CONVERSATION_` prefix in run-time environment.
let url;
let disableSSL = false;
try {
// ASSISTANT should be used
auth = getAuthenticatorFromEnvironment('ASSISTANT');
url = process.env.ASSISTANT_URL;
if (process.env.ASSISTANT_DISABLE_SSL == 'true') {
disableSSL = true;
}
} catch (e) {
// but handle if alternate CONVERSATION is used
auth = getAuthenticatorFromEnvironment('conversation');
url = process.env.CONVERSATION_URL;
if (process.env.CONVERSATION_DISABLE_SSL == 'true') {
disableSSL = true;
}
}
const assistant = new AssistantV1({
version: '2019-02-28',
authenticator: auth,
url: url,
disableSslVerification: disableSSL,
});
const discovery = new DiscoveryV1({
version: '2019-04-30',
});
const nlu = new NaturalLanguageUnderstandingV1({
version: '2019-07-12',
});
const bankingServicesIN = require('./banking_services');
const bankingServicesUS = require('./banking_services_us');
const WatsonDiscoverySetup = require('./lib/watson-discovery-setup');
const WatsonAssistantSetup = require('./lib/watson-assistant-setup');
const DEFAULT_NAME = 'watson-banking-chatbot';
const DISCOVERY_ACTION = 'disco';
const LOOKUP_BALANCE = 'balance';
const LOOKUP_TRANSACTIONS = 'transactions';
const LOOKUP_5TRANSACTIONS = '5transactions';
const SKILL_FILE_US = 'data/conversation/workspaces/banking_US.json';
const SKILL_FILE_IN = 'data/conversation/workspaces/banking_IN.json';
const DISCOVERY_DOCS_US = [
'./data/discovery/docs/en_US/BankFaqRnR-DB-Failure-General.docx',
'./data/discovery/docs/en_US/BankFaqRnR-DB-Terms-General.docx',
'./data/discovery/docs/en_US/BankFaqRnR-e2eAO-Terms.docx',
'./data/discovery/docs/en_US/BankFaqRnR-e2ePL-Terms.docx',
'./data/discovery/docs/en_US/BankRnR-OMP-General.docx',
];
const DISCOVERY_DOCS_IN = [
'./data/discovery/docs/en_IN/BankFaqRnR-DB-Failure-General.docx',
'./data/discovery/docs/en_IN/BankFaqRnR-DB-Terms-General.docx',
'./data/discovery/docs/en_IN/BankFaqRnR-e2eAO-Terms.docx',
'./data/discovery/docs/en_IN/BankFaqRnR-e2ePL-Terms.docx',
'./data/discovery/docs/en_IN/BankRnR-OMP-General.docx',
];
// TODO: Change default if we are mostly documenting US version.
// Default to the original India version.
const locale = process.env.LOCALE || 'EN_IN';
const EN_US = locale.toUpperCase() === 'EN_US';
const SKILL_FILE = EN_US ? SKILL_FILE_US : SKILL_FILE_IN;
const DISCOVERY_DOCS = EN_US ? DISCOVERY_DOCS_US : DISCOVERY_DOCS_IN;
const bankingServices = EN_US ? bankingServicesUS : bankingServicesIN;
const skillJson = JSON.parse(fs.readFileSync(SKILL_FILE));
// Exported JSON uses dialog_nodes but older SDK code wants dialogNodes.
if ('dialog_nodes' in skillJson && !('dialogNodes' in skillJson)) {
skillJson.dialogNodes = skillJson.dialog_nodes;
}
console.log('locale = ' + locale);
const app = express();
// Bootstrap application settings
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());
// setupError will be set to an error message if we cannot recover from service setup or init error.
let setupError = '';
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;
}
});
// Use Watson Assistant V1 to perform any authoring of the dialog components
let skillID; // skillID will be set when the skill is created or validated.
const assistantSetup = new WatsonAssistantSetup(assistant);
const assistantSetupParams = { default_name: DEFAULT_NAME, skill_json: skillJson };
assistantSetup.setupAssistantWorkspace(assistantSetupParams, (err, data) => {
if (err) {
handleSetupError(err);
} else {
console.log('Watson Assistant is ready!');
skillID = data;
}
});
// 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 (!skillID) {
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 = {
workspaceId: skillID,
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;
}
}
callAssistant(payload);
});
/**
* Send the input to the Assistant service.
* @param payload
*/
function callAssistant(payload) {
if (!('text' in payload.input) || payload.input.text == '') {
assistant.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
} else {
console.log('assistant.message1 :: ', JSON.stringify(data.result, null, 2));
return res.json(data.result);
}
});
} else {
const queryInput = JSON.stringify(payload.input);
console.log('queryInput: ' + queryInput);
const parameters = {
text: payload.input.text,
language: 'en', // To avoid language detection errors
features: {
entities: {
sentiment: true,
limit: 2,
},
keywords: {
sentiment: true,
limit: 2,
},
},
};
// call NLU to check if location is included in request
nlu
.analyze(parameters)
.then((response) => {
const nluOutput = response.result;
payload.context['nlu_output'] = nluOutput;
// 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'] = '';
}
assistant.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
} else {
// lookup actions
checkForLookupRequests(data, function (err, data) {
if (err) {
console.log(err);
return res.status(err.code || 500).json(err);
} else {
return res.json(data);
}
});
}
});
})
.catch((err) => {
console.log('error:', err);
});
}
}
});
/**
* Looks for actions requested by Assistant service and provides the requested data.
*/
function checkForLookupRequests(output, callback) {
console.log('checkForLookupRequests');
const data = output.result;
console.log('Assistant result to act on: ' + JSON.stringify(data, null, 2));
// The branchInfo intent is handled here to help keep our Assistant dialog
// within the limits of the free plan. Could simplify this with more dialog.
// For branchInfo intent, set the action/location
if (data.intents.length > 0 && data.intents[0].intent === 'branchInfo') {
if (data.context.Location) {
// with Location we'll do a branch lookup
data.context['action'] = { lookup: 'branch' };
} else {
// without a Location we'll just do Discovery for general branch question
data.intents[0]['intent'] = DISCOVERY_ACTION;
}
}
if (data.context && data.context.action && data.context.action.lookup && data.context.action.lookup != 'complete') {
const payload = {
workspaceId: skillID,
context: data.context,
input: data.input,
};
console.log('data.context.action.lookup: ' + data.context.action.lookup);
// Assistant 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 assistant.message with lookup results.');
assistant.message(payload, function (err, data) {
if (err) {
console.log('Error while calling assistant.message with lookup result', err);
callback(err, null);
} else {
console.log('checkForLookupRequests assistant.message :: ', JSON.stringify(data));
callback(null, data.result);
}
});
} 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 5 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.Location.toLowerCase();
// Use the master (India) lookup for getBranchInfo
// It can return either India or US.
bankingServicesIN.getBranchInfo(loc, function (err, branchMaster) {
if (err) {
console.log('Error while calling bankingServices.getAccountInfo ', err);
callback(err, null);
return;
}
let branchText = '';
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.Location;
}
payload.context['branch'] = branchMaster;
// clear the context's action since the lookup was completed.
payload.context.action = {};
data.output.text = [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) {
// query to trigger this action - "can I use an atm in any city"
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 = {
naturalLanguageQuery: 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.result.matching_results > 0) {
// we have a valid response from Discovery
// now check if we are using SDU or just a simple document query
let bestLine;
console.log('Disco result: ' + JSON.stringify(searchResponse, null, 2));
if ('answer' in searchResponse.result.results[0]) {
console.log('Using Discovery SDU');
let bestScore = 0;
for (let i = 0, size = searchResponse.result.results.length; i < size; i++) {
if (searchResponse.result.results[i].result_metadata['confidence'] > bestScore) {
bestLine = searchResponse.result.results[i].answer[0];
bestScore = searchResponse.result.results[i].result_metadata['confidence'];
}
}
} else if ('passages' in searchResponse.result) {
console.log('Using Passage feature');
// use Passage feature
const bestPassage = searchResponse.result.passages[0];
console.log('Passage score: ', bestPassage.passage_score);
console.log('Passage text: ', bestPassage.passage_text);
// set a default value
bestLine = bestPassage.passage_text;
// Trim the passage to try to get just the answer part of it.
const lines = bestPassage.passage_text.split('.');
// just use the first sentence in the response.
// if it contains a question mark, use the portion after it
const line = lines[0].trim();
if (line.indexOf('?') > -1) {
const subline = line.split('?');
bestLine = subline[1];
} else {
bestLine = line;
}
} else {
console.log('Using default response');
// other formats, like from CPD (non-SDU)
if ('text' in searchResponse.result.results[0]) {
const lines = searchResponse.result.results[0].text.split('.');
const line = lines[0].trim();
if (line.indexOf('?') > -1) {
const subline = line.split('?');
bestLine = subline[1];
} else {
bestLine = line;
}
}
}
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);
}
console.log('Discovery response: ' + 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;