-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressServe.js
337 lines (238 loc) · 9.84 KB
/
expressServe.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
var express = require('express');
var twilio = require('twilio');
// (process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
var WSS = require("ws").Server;
var bcrypt = require('bcryptjs');
var server = new WSS({port:4000});
var secret = process.env.SECRET_PW;
var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(process.env.SECRET_PW, salt);
console.log(bcrypt.compareSync("2c", hash));
console.log(hash);
console.log(secret);
var clients = [];
var msgLog = [];
var namListS = [];
var connections = [];
var bodyParser = require('body-parser');
var fs = require('fs');
var cors = require('cors');
var opPostCount = 0;
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database("sense.db");
var app = express();
var msgArr = [];
var descriptors = ['red','white','blue','black', 'imperial', 'martial', 'presidential',
'easy','anonymous', 'galactic','federal','cyber','dark', 'the last','daring','jumping','wylde',
'dangerous','screaming','bad news' , 'american','star','night','iron','notorious',
'mysterious','glowing','singing','no-named'];
var namesForUsers = ['shirt','hat','agent','viper','jack','bill','fan','ranger','hero', 'villain',
'swordsman', 'unicorn', 'gunfighter','rider','ranger','warrior','waiter','gangster',
'outlaw','master','watcher','android','runner','hunter','robot','stallion','lord',
'engineer','captain','scout','pilot','ninja','viking','avenger'];
var generatedUserNames = [];
var nameGenerator = function(){
var unique = false;
while (unique === false){
unique = true;
var frontIndex = Math.floor(Math.random() * descriptors.length);
var backIndex = Math.floor(Math.random() * namesForUsers.length);
var nameBuilt = descriptors[frontIndex] + " " + namesForUsers[backIndex];
generatedUserNames.forEach(function(name){
if(name === nameBuilt){unique = false}
});
}
generatedUserNames.push(nameBuilt);
return nameBuilt;
};
var client = new twilio.RestClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
app.set('port', (process.env.PORT || 5000));
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({ extended: false }));
app.use(express.static(__dirname + '/public'));
app.get('/messages', function(request, response) {
db.all("SELECT * FROM users WHERE active = 1", function(err, uRow) {
uRow.forEach(function(user){
console.log('user info is ' + user.phone);
db.all("SELECT * FROM messages WHERE phone = ? AND open_ticket = ?", user.phone,true, function(err, mRow){
if(err){ throw err;}
user['messages'] = mRow;
})
})
console.log(uRow);
setTimeout(function(){response.json(uRow)},100);
});
});
app.put('/close_ticket', function(request, response) {
console.log(request)
var nameIndex = generatedUserNames.indexOf(request['body']['handle']);
generatedUserNames.splice(nameIndex , 1);
// console.log(JSON.parse(request));
// var ticketToClose = JSON.parse(request);
db.all("UPDATE users SET active = 0 WHERE phone = ?",request['body']['phone'], function(err, row){
if(err){
throw err;
}
console.log(row);
})
db.all("UPDATE messages SET open_ticket = ? WHERE phone = ?", false,request['body']['phone'], function(err, row) {
if(err){
throw err;
}
console.log(row);
setTimeout(function(){response.json('ticket closed')},100);
});
});
server.on("connection" , function(ws){
console.log("there has been a connection.")
clients.push(ws);
// post in websocket
app.post('/sms', twilio.webhook({
validate:false
}), function(request, response) {
var rPhone = request['body']['From'];
var rBody = request['body']['Body'];
console.log(rPhone + " <> " + rBody);
db.get("SELECT * FROM users WHERE phone = ?", rPhone, function(err, row) {
console.log(row)
if(row && row.active === 0){
var newName = nameGenerator();
console.log("inactive user being reactivated");
db.run("UPDATE users SET active = 1 WHERE phone = ?" , rPhone, function(err, row) {
if(err) { throw err; }
// clients.forEach(function(clientWs){clientWs.send(infoBack)});
db.run("UPDATE users SET handle = ? WHERE phone = ?" , newName , rPhone, function(err, row) {
if(err) { throw err; }
db.run("INSERT INTO messages (body,phone,open_ticket,received) VALUES(?,?,?,?)" , rBody, rPhone,true,true, function(err){
var infoBack = JSON.stringify({phone:rPhone.slice(1,rPhone.length),message:rBody,handle:newName});
setTimeout(function(){ clients.forEach(function(clientWs){clientWs.send(infoBack)}) },10);
});
});
})
}
if(row && row.active === 1){
console.log("active user being reactivated");
db.run("INSERT INTO messages (body,phone,open_ticket,received) VALUES(?,?,?,?)" , rBody, rPhone,true,true, function(err){
var infoBack = JSON.stringify({phone:rPhone.slice(1,rPhone.length),message:rBody,handle:row.handle});
clients.forEach(function(clientWs){clientWs.send(infoBack)});
});
}else if(!row){
var handleToAssign = nameGenerator();
db.run("INSERT INTO users (phone,handle,active) VALUES (?,?,?)", rPhone, handleToAssign,true, function(err) {
if(err) { throw err; }
// var id = this.lastID; //weird way of getting id of what you just inserted
db.run("INSERT INTO messages (body,phone,open_ticket,received) VALUES(?,?,?,?)" , rBody, rPhone,true,true, function(err) {
if(err) { throw err; }
});
});
var infoBack = JSON.stringify({phone:rPhone.slice(1,rPhone.length),message:rBody,handle:handleToAssign});
clients.forEach(function(clientWs){clientWs.send(infoBack)});
}
});
msgArr.push(request['body']['Body']);
// Create a TwiML response
var twiml = new twilio.TwimlResponse();
twiml.message('Hello from express node.js!');
// Render the TwiML response as XML
//response.send(twiml);
});
ws.on("message" , function(msg){
var opToUserMsg = JSON.parse(msg);
if(bcrypt.compareSync(opToUserMsg.password, hash)){console.log("SAFE")}
else{console.log("FAIL")}
client.messages.create({
to:'+' + opToUserMsg['phone'].toString(),
from:'2132973673',
body:opToUserMsg['body']
}, function(error, message) {
// The HTTP request to Twilio will run asynchronously. This callback
// function will be called when a response is received from Twilio
// The "error" variable will contain error information, if any.
// If the request was successful, this value will be "falsy"
if (!error) {
// The second argument to the callback will contain the information
// sent back by Twilio for the request. In this case, it is the
// information about the text messsage you just sent:
db.run("INSERT INTO messages (body,phone,open_ticket,received) VALUES(?,?,?,?)" , opToUserMsg['body'], opToUserMsg['phone'],true,false, function(err) {
if(err) { throw err; }
});
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
// clients.forEach(function(clientWs){clientWs.send(opToUserMsg)});
} else {
console.log('Oops! There was an error.');
console.log(error);
}
});
})
ws.on("close" , function(){
var escapee = clients.indexOf(ws);
clients.splice(escapee,1);
namListS.splice(escapee,1);
console.log("somebody left");
clients.forEach(function(user){user.send("A user has left the room")});
})
})
app.post('/operator', function(request, response) {
opPostCount ++;
console.log('Operator Post Count is ' + opPostCount);
var sent = request;
var textBody = sent['body']['body'];
// textArray.forEach(function(msgPart){
client.messages.create({
to:'+' + sent['body']['phone'].toString(),
from:'2132973673',
body:textBody
}, function(error, message) {
// The HTTP request to Twilio will run asynchronously. This callback
// function will be called when a response is received from Twilio
// The "error" variable will contain error information, if any.
// If the request was successful, this value will be "falsy"
if (!error) {
// The second argument to the callback will contain the information
// sent back by Twilio for the request. In this case, it is the
// information about the text messsage you just sent:
db.run("INSERT INTO messages (body,phone,received) VALUES(?,?,?)" , textBody, sent['body']['phone'],false, function(err) {
if(err) { throw err; }
});
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
} else {
console.log('Oops! There was an error.');
console.log(error);
}
});
// });
// Create a TwiML response
// Render the TwiML response as XML
// response.send(twiml);
});
//
// var accountSid = ;
// var authToken = "{{ auth_token }}";
// var clientBig = require('twilio')(tas, tat);
//
// console.log(tas);
//
// clientBig.messages.create({
// body: "Jenny please?! I love y",
// to: "+15165781916",
// from: "+14158141829"
// }, function(err, message) {
// process.stdout.write(message.sid);
// });
//
//
//
// var bigMessageURI = "/2010-04-01/Accounts/" + tas + "/Messages";
//
// var textArray = textBody.split('\n');
// Have express create an HTTP server that will listen on port 3000
// or "process.env.PORT", if defined
app.listen(process.env.PORT || 1337);
// app.listen(3000);