forked from watson-developer-cloud/assistant-intermediate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
130 lines (112 loc) · 4.23 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
/**
* Copyright 2015 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';
var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
var AssistantV1 = require('watson-developer-cloud/assistant/v1'); // watson sdk
var Actions = require('./functions/actions');
var actions = new Actions();
var BankFunctions = require('./functions/bankFunctions');
var bankFunctions = new BankFunctions();
var app = express();
// Bootstrap application settings
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());
// Create the service wrapper
var assistant = new AssistantV1({
version: '2018-07-10'
});
var nextMonth = ((new Date().getMonth() + 1) % 12) + 1;
// This is where the end user context is set / injected.
// Ideally the context values would come from an application
// where the user information is maintained.
var userProfileContextData = {
avatarName: "Sam",
channel: "web",
userId: "swamchan",
firstName: "Swami",
lastName: "Chandrassekaran",
//email: "[email protected]",
phone: "+1 972-123-4567",
timezone: "-6", // with respect to GMT
homeCity: "Dallas",
homeState: "TX",
office: "123 Spooner Street",
ibm_function_credentials: "{'api_key':'username:password'}", // IBM Functions service instance API key
openRequests: 1,
org: {
groupId: 156,
orgId: "US-642"
//departmentName: "IT",
},
previousInteractions: [
{dateOfInteraction: "10/21/2018", intents: ['Submit_Service_Request', 'Handle_ChitChat']},
{dateOfInteraction: "10/15/2018", intents: ['Reset_Password', 'Handle_ChitChat']},
],
//enagagement_tone_counter: 0,
enagagement_tone_array: [],
private: {
function_creds: {
user: process.env.CLOUD_FUNCTION_USER,
password: process.env.CLOUD_FUNCTION_PASS,
},
},
};
// Endpoint to be call from the client side
app.post('/api/message', function (req, res) {
var workspace = process.env.WORKSPACE_ID || '<workspace-id>';
if (!workspace || workspace === '<workspace-id>') {
return res.json({
'output': {
'text': 'The app has not been configured with a <b>WORKSPACE_ID</b> environment variable. Please refer to the ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate">README</a> documentation on how to set this variable. <br>' + 'Once a workspace has been defined the intents may be imported from ' + '<a href="https://github.com/watson-developer-cloud/assistant-intermediate/blob/master/training/banking_workspace.json">here</a> in order to get a working application.'
}
});
}
// Fetch context. Ideally the context values would come from an application
// where the user information is maintained.
var contextWithAcc = Object.assign({}, req.body.context, userProfileContextData);
var payload = {
workspace_id: workspace,
context: contextWithAcc || {},
input: req.body.input || {}
};
// Send the input to the assistant service
assistant.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
actions.testForAction(data).then(function (d) {
return res.json(d);
}).catch(function (error) {
return res.json(error);
});
});
});
app.get('/bank/validate', function (req, res) {
var value = req.query.value;
var isAccValid = bankFunctions.validateAccountNumber(Number(value));
// if accountNum is in list of valid accounts
if (isAccValid === true) {
res.send({ result: 'acc123valid' });
} else {
// return invalid by default
res.send({ result: 'acc123invalid' });
}
});
app.get('/bank/locate', function (req, res) {
res.send({ result: 'zip123retrieved' });
});
module.exports = app;