forked from abhijeetmane/chatbotConnector
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (137 loc) · 5.22 KB
/
server.js
File metadata and controls
178 lines (137 loc) · 5.22 KB
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
//require the express nodejs module
// import * as firebase from 'firebase';
var firebase = require('firebase'),
express = require('express'),
//set an instance of exress
app = express(),
//require the body-parser nodejs module
bodyParser = require('body-parser'),
//require the path nodejs module
path = require("path"),
config = {
apiKey: "AIzaSyD6jFXalt-AXrK0JsiHHp9MuxumTSXUZ_M",
authDomain: "awesome-flash-99616.firebaseapp.com",
databaseURL: "https://awesome-flash-99616.firebaseio.com",
projectId: "awesome-flash-99616",
storageBucket: "awesome-flash-99616.appspot.com",
messagingSenderId: "64543180480"
},
name;
firebase.initializeApp(config);
let getFireName = (callback) => {
/* Code for reading from firebase database */
console.log('inside getFireName');
return firebase.database().ref('/').on('value', callback);
/* Code for loadeing data once which isn't expected to change frequently */
// firebase.database().ref('/').once('value').then(function(snapshot) {
// snapshot.forEach(function(childSnapshot) {
// var childKey = childSnapshot.key;
// var childData = childSnapshot.val();
// console.log('childData=' + childData); // ...
// // ...
// });
// });
/* Code for wrtiting to firebase database */
// firebase.database().ref('/').set({
// name: 'abhijeeet'
// });
}
//support parsing of application/json type post data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({
extended: true
}));
//tell express that www is the root of our public web folder
app.use(express.static(path.join(__dirname, 'www')));
//tell express what to do when the /form route is requested
app.post('/fulfillment', function(req, res) {
var resultVal = {};
res.setHeader('Content-Type', 'application/json');
switch (req.body.result.action) {
case 'portfolio':
resultVal = processPortfolioRequest(req.body.result.parameters, res);
break;
case 'history':
resultVal = processHistoryRequest(req.body.result.parameters, res);
break;
case 'account':
resultVal = processAccountRequest(req.body.result.parameters, res);
break;
case 'faq':
resultVal = processFaqRequest(req.body.result.parameters, res);
break;
default:
console.log('No intents matched');
}
// res.send(JSON.stringify(resultVal));
//debugging output for the terminal
console.log('request is : ', req.body.result.action, req.body.result.parameters);
});
function processPortfolioRequest(data, res) {
var result = {
"speech": "Portfolio is returned from services",
"displayText": "Portfolio is returned from services",
"data": JSON.stringify(data),
"contextOut": [],
"source": "Portfolio Service"
};
return result;
}
function processHistoryRequest(data, res) {
console.log('inside history');
var callback = function(snapshot) {
var datareturn = {
'name': '',
'portfolioName': 'Advisory Portfolio',
'assetAmount': '2000 Euro'
};
snapshot.forEach(function(childSnapshot) {
console.log('name =' + childSnapshot.val());
datareturn.name = childSnapshot.val();
});
var result = {
"speech": datareturn.name + " Portfolio History is returned from services",
"displayText": "Portfolio History is returned from services",
"data": JSON.stringify(datareturn),
"contextOut": [],
"source": "Portfolio History Service"
};
console.log(result);
res.send(JSON.stringify(result));
};
return getFireName(callback);
}
function processAccountRequest(data, res) {
var result = {
"speech": "Portfolio account is returned from services",
"displayText": "Portfolio account is returned from services",
"data": JSON.stringify(data),
"contextOut": [],
"source": "Portfolio account Service"
};
console.log(result);
res.send(JSON.stringify(result));
return result;
}
function processFaqRequest(data, res) {
var datareturn = {
'botname': 'goku',
'features': ['portfolio', 'virtual credit card', 'questions', 'incident requests'],
'whatelse': 'I am working on more functionality'
};
var result = {
"speech": "Portfolio FAQ is returned from services",
"displayText": "Portfolio FAQ is returned from services",
"data": JSON.stringify(datareturn),
"contextOut": [],
"source": "Portfolio FAQ Service"
};
console.log(result);
res.send(JSON.stringify(result));
return result;
}
//wait for a connection
app.listen(process.env.PORT || 3000, function() {
console.log('Server is running. Point your browser to: http://localhost:3000');
});