-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
240 lines (217 loc) · 9.66 KB
/
server.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
/**
* Created by Christian Bartram on 6/8/17.
*/
'use strict';
global.navigator = { userAgent: 'all' };
const path = require('path');
const http = require('http');
const Express = require('express');
const bodyParser = require('body-parser');
const moment = require('moment');
const { Wit } = require('node-wit');
const SocketIO = require('socket.io');
// initialize the server and configure support for ejs templates
const app = new Express();
const server = http.Server(app);
let io = new SocketIO(server);
let authenticated = false;
const client = new Wit({
accessToken: "WIPCNZGMJ5KSPEHCQQUAEVYP55MXIIX2",
});
// define the folder that will be used for static assets
app.use(Express.static(path.join(__dirname, 'static')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
io.on('connection', (socket) => {
socket.on('message', (data) => {
//Invoke Wit.AI NLP Engine
client.message(data.text, {})
.then((data) => {
//Find providers and notify client
if(data.entities.hasOwnProperty('intent')) {
switch(data.entities.intent[0].value) {
case 'find_providers':
socket.emit('message', {
id: 1,
user: 0,
requireAuth: false,
auth: authenticated,
msg: `You have 5 Providers near you the closest one is 3.2 miles from you, I've marked their location in your links!`,
type: 'message',
link: 'http://maps.apple.com/?q=Doctor',
subject: 'Care Providers',
label: 'danger',
timestamp: moment().format('h:mm a')
});
break;
case 'deductible_info':
socket.emit('message', {
id: 2,
user: 0,
requireAuth: true,
auth: authenticated,
msg: 'You have the Premium plan it has a $500 deductible and lots of great healthy benefits.',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'insurance_purchase':
socket.emit('message', {
id: 3,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Awesome! I can recommend the BlueSelect or BlueOptions plans for you for only $226 and $310 per month respectively!',
type: 'message',
link: 'https://consumer.websales.floridablue.com/cws/shopping/info',
subject: 'Insurance Purchase Info',
label: 'primary2',
timestamp: moment().format('h:mm a')
});
break;
case 'test':
socket.emit('message', {
id: 4,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'I read you loud and clear! What can I assist you with today?',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'greeting':
socket.emit('message', {
id: 5,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Hello! How can I help you today?',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'bye':
socket.emit('message', {
id: 6,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Glad I could help, have a fantastic rest of your day!',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'help':
socket.emit('message', {
id: 7,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'What can I help you with? I can help with things like Insurance Purchases, Deductible information, and Provider data.',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'personal_inquiry':
socket.emit('message', {
id: 8,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Im doing quite well thanks for asking! How are you?',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
case 'auth':
//TODO AUTHENTICATE USER
console.log(data);
let birthdate = data.entities.datetime[0];
//TODO just comparing it to JSON for the prototype
authenticated = true;
socket.emit('message', {
id: 9,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Thank you for authenticating! What can I help you with',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
break;
default:
socket.emit('message', {
id: 10,
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Yikes not really sure what to do',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
});
}
} else {
socket.emit('message', {
user: 0,
requireAuth:false,
auth: authenticated,
msg: 'Sorry im not sure what to do with your request. Try asking something like "Find my provider" or "Help me find a plan"',
type: 'message',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
})
}
}).catch(() => {
socket.emit('message', {
user: 2,
requireAuth: false,
auth: authenticated,
msg: 'Failed to Connect',
type: 'event',
link: null,
subject: null,
label: null,
timestamp: moment().format('h:mm a')
})
});
});
});
// universal routing and rendering
app.get('*', (req, res) => {
});
// start the server
const port = process.env.PORT || 3001;
const env = process.env.NODE_ENV || 'production';
server.listen(port, err => {
if (err) {
return console.error(err);
}
console.info(`Server running on http://localhost:${port} [${env}]`);
});