-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
1422 lines (1327 loc) · 82.5 KB
/
server.js
File metadata and controls
1422 lines (1327 loc) · 82.5 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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const express = require('express');
const path = require('path');
const session = require('express-session');
const MessagingResponse = require('twilio').twiml.MessagingResponse;
const bodyParser = require('body-parser');
const request = require('request');
const fs = require('fs');
const PORT = process.env.PORT || 3001;
const app = express();
const crypto = require("crypto");
const firebaseAdmin = require("firebase-admin");
const serviceAccount = require("./serviceAccountKey.json");
const {Storage} = require('@google-cloud/storage');
const TWILIO_ACCOUNT_SID = "ACd4789025c22842f1b8d831ef03ddd403";
const TWILIO_AUTH_TOKEN = "ca7ecd44f3a7fbf65b879c2ad531e8b7";
const client = require('twilio')(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const sightengine = require('sightengine')('371368091', '4BnEGw9V9dMnfRfMYzSP');
const unirest = require('unirest');
const stripe = require('stripe')('STRIPE_PUBLISHABLE_KEY');
const moment = require('moment');
// - - - - - UNCOMMENT ONCE IN DEVELOPMENT MODE - - - - - //
// INPUT STRIPE_SECRET_KEY INTO THE .ENV FILE
// const stripe = require('stripe')(process.env.STRIPE_PUBLISHABLE_KEY); //STRIPE PUBLISHABLE KEY - - WILL NEED TO BE REPLACED TO .ENV SECRET KEY
// require('dotenv').config(); // WHERE WE REQUIRE THE STRIPE_SECRET_KEY
// const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); // STRIPE SECRET KEY
// - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// Initialize the app with a service account, granting admin privileges
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount),
databaseURL: 'https://twiliocms.firebaseio.com',
storageBucket: 'gs://twiliocms.appspot.com/postImages',
});
let dir = './tmp';
// stuff for database and storage
const db = firebaseAdmin.firestore();
let users = db.collection('users');
let posts = db.collection('posts');
let jobs = db.collection('jobs');
const storage = new Storage();
const bucketName = 'textadorposts';
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(session({secret: 'smsCount', resave: true, saveUninitialized: true}));
app.use(express.static('public'));
app.use(require('body-parser').text());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
let messageBody = "";
let messageNumber = "";
let systemNumber = "";
let mySystemNumber = "";
let myUserId = "";
let myPostTitle = "";
let myPostDescription = "";
let myPostPrice = "";
let myCellNumber = "";
let ringOwnerCellNumber = "";
let handle = "";
let myName = "";
let cashAddress = "";
let cashAmount = "";
let textadorBucks = "";
let wage = "";
let job = {};
let myRingJob = '';
app.post('/charge', async (req, res) => {
try {
let { status } = await stripe.charges.create({
amount: 2000,
currency: 'usd',
description: 'An example charge',
source: req.body,
});
res.json({ status });
} catch (err) {
res.status(500).end();
}
});
app.post('/api', (req, res) => {
let smsCount = req.session.counter || 0;
console.log("========================================> FIRST SESSION: ", smsCount)
messageBody = req.body.Body.trim().toLowerCase();
messageNumber = req.body.From.trim();
jobSystemNumber = req.body.To.trim();
systemNumber = req.body.To.trim();
textadorNumber = req.body.To.trim();
users = db.collection('users');
posts = db.collection('posts');
mySystemNumber = "";
console.log("+++++++++ Message FROM: ", messageNumber)
console.log("+++++++++ Message TO: ", systemNumber)
console.log("+++++++++ Message JOB: ", jobSystemNumber)
console.log("+++++++++ Message SMS: ", smsCount)
console.log("+++++++++ Message Body: ", messageBody)
if (messageBody === "join") {
thisUser = users.where("myCellNumber", "==", messageNumber).get().then(thisUserSnap => {
console.log("myCellNumber", "==", messageNumber);
if (thisUserSnap.empty) {
var twiml = new MessagingResponse();
req.session.counter = 100;
message = 'Welcome.\n\nTo join this RING\n\nenter the PASSCODE now.';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
else {
var twiml = new MessagingResponse();
req.session.counter = 0;
message = 'Only one account per Ring is required.';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
});
}
// Get Start Words POST
if (messageBody === "post") {
console.log("POST ++++++++++++++++++++++++")
// enter PIN
var twiml = new MessagingResponse();
req.session.counter = 201;
message = 'Ready to POST\n\n Enter your PASSCODE to confirm.\n';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
// Get Start Words READ
if (messageBody === "read") {
console.log("READ ++++++++++++++++++++++++")
// Send latest post and options for NEXT
posts = posts.where("ringNumber", "==", systemNumber).orderBy('createdAt', 'desc').get().then(snapshot => {
if (snapshot.empty) {
// we don't have a user in the system.
console.log("NO USER FOUND")
let twiml = new MessagingResponse();
message = 'You need to be a member of this RING to POST.\n\nPlease JOIN the RING first.';
req.session.counter = 0;
myPostTitle = messageBody
twiml.message(message);
res.writeHead(200, {'Content-Type': 'texts/xml'});
res.end(twiml.toString());
// we could change the session counter to CREATE USER
}
else {
let doc = snapshot.docs[0];
let authorHandle = doc.data().authorHandle;
authorHandle = authorHandle.toUpperCase();
let title = doc.data().title.toUpperCase();
let description = doc.data().description;
let image = doc.data().image;
let twiml = new MessagingResponse();
let message = twiml.message();
req.session.counter = 0;
message.body(title + '\n\n' + description + '\n\n' + 'Posted by: ' + authorHandle);
message.media(image);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("423 Get Jobhs Session Counter: " + smsCount);
}
}).catch (err => {
console.log(err.errors[0].message);
});
}
// Get Start Words JOBS
if (messageBody === "jobs") {
console.log("JOBS ++++++++++++++++++++++++")
// Send most recent job
findJob = db.collection('jobs').where('jobOpen', '==', true).where("systemNumber", "==", systemNumber).orderBy('startDate', 'desc').get().then(snapshot => {
if (snapshot.empty) {
// we don't have a user in the system.
console.log("NO JOB FOUND")
// we could change the session counter to CREATE USER
let twiml = new MessagingResponse();
req.session.counter = 0;
let message = twiml.message();
message.body('No available jobs in your area.');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("No Jobs COUNTER: " + smsCount);
}
else {
let doc = snapshot.docs[0];
let title = doc.data().title;
title = title.toUpperCase();
let wage = doc.data().wage;
let startAddress = doc.data().startAddress;
let startDate = doc.data().startDate;
startDate = moment(startDate).format('LLL');
console.log("FIRST TITLE: ", title)
console.log("FIRST WAGE: ", wage)
console.log("FIRST Address: ", startAddress)
let twiml = new MessagingResponse();
req.session.counter = 0;
let message = twiml.message();
message.body(title + '\n\nWHERE:\n' + startAddress + '\n\nWHEN:\n' + startDate + '\n\nGET:\n$ ' + wage + '\n\nGet there and send WORK to begin.');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("423 Get Jobhs Session Counter: " + smsCount);
}
}).catch (err => {
console.log('427 ' + err);
});
}
// Get Start Words WORK
if (messageBody === "work") {
console.log("WORK ++++++++++++++++++++++++")
// Send JOB PIN and generate CODE
workJobs = db.collection('jobs').where('jobOpen', '==', true).where("systemNumber", "==", systemNumber).orderBy('startDate', 'desc').get().then((snapshot) => {
if (snapshot.empty) {
// we don't have a user in the system.
console.log("NO JOB FOUND")
// we could change the session counter to CREATE USER
}
else {
let doc = snapshot.docs[0];
let title = doc.data().title;
title = title.toUpperCase();
let wage = doc.data().wage;
let startAddress = doc.data().startAddress;
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> THIS JOB: ", doc.id)
let twiml = new MessagingResponse();
let message = twiml.message();
req.session.counter = 501;
message.body('Confirm you are at:\n' + startAddress + '\n\n' + '\nSend your PASSCODE now:');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + req.session.counter);
}
})
}
// Get Start Words CASH
if (messageBody === "cash") {
console.log("CASH ++++++++++++++++++++++++")
// enter PIN
var twiml = new MessagingResponse();
req.session.counter = 601;
message = 'CASH Request\n\n Enter your PASSCODE to confirm.\n';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
// Get Start Words BAL
if (messageBody === "bal") {
console.log("BALANCE ++++++++++++++++++++++++")
// FIND the USER
user = users.where("myCellNumber", "==", messageNumber).get().then(snapshot => {
if (snapshot.empty) {
let doc = snapshot.docs[0];
console.log("WE FOUND THIS USER: ", doc.id);
// we don't have a user in the system.
console.log("NO USER FOUND")
let twiml = new MessagingResponse();
message = 'You must be a member of this RING first.\n\nSend JOIN to get started.';
req.session.counter = 0;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'texts/xml'});
res.end(twiml.toString());
// we could change the session counter to CREATE USER
}
else {
let doc = snapshot.docs[0];
// We will use these variables for the person sending this message
handle = doc.data().handle.toUpperCase();
balance = doc.data().balance;
console.log("WE FOUND THIS USER: ", doc.id);
console.log("WE FOUND THIS USER: ", handle);
console.log("WE FOUND THIS USER BALANCE: ", balance);
let twiml = new MessagingResponse();
req.session.counter = 0;
message = 'You have:\n\n$' + balance;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
});
}
// Get Start Words LIST
if (messageBody === "list") {
console.log("LIST ++++++++++++++++++++++++")
// List of valid commands
console.log("LIST ++++++++++++++++++++++++")
// List of valid commands
console.log('(else catch all)')
var twiml = new MessagingResponse();
req.session.counter = 0;
message = 'You can use the following commands:\n\nREAD - Read recent posts\nPOST - Post a photo message\nJOBS - Find jobs nearby\nBAL - Balance\nCASH - Request Cash';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
// If work DONE
if (messageBody == "done") {
let twiml = new MessagingResponse();
req.session.counter = 515;
twiml.message("Send YES if everything went as planned.\nOtherwise send a message to the BOSS explainging the issue. ");
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + req.session.counter);
}
// Building a New USER in reverse order of avoid double if/else.
if (smsCount >= 104 && smsCount <= 106) {
console.log('smsCount >= 104 && smsCount <= 106')
thisRing = users.where("myCellNumber", "==", messageNumber).get().then(thisUserSnap => {
let doc = thisUserSnap.docs[0];
thisUser = doc.id;
let passcode = doc.data().passcode;
console.log("this user: ", thisUser);
if(req.body.NumMedia !== '0') {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
};
const filename = `${req.body.MessageSid}.jpg`;
const url = req.body.MediaUrl0;
let localFile = `./tmp/${req.body.MessageSid}.jpg`;
sightengine.check(['nudity', 'type', 'wad', 'scam', 'text', 'offensive']).set_url(url).then(function(result) {
console.log("=========image scan results=====>", result);
console.log("=========image scan url=====>", url);
// if the image passes moderation, then...
if (result.offensive.prob <= .1 && result.nudity.safe >= .9 && result.weapon <= .1 && result.drugs <= .1 && result.alcohol <= .1 && result.scam.prob <= .1 && result.type.illustration <= .15) {
// Download the image
request(url).pipe(fs.createWriteStream(localFile))
.on('close', () => {
console.log('Image downloaded. in Create new textador');
// upload image
function uploadImage() {
storage.bucket(bucketName).upload(localFile, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
}).catch (err => {
console.log("ERROR: ", err);
});
}
// save post to database
function saveUserToDatabase() {
db.collection('users').doc(thisUser).update({
textadorImage: `https://storage.googleapis.com/textadorposts/${filename}`,
imageModeration: result
}).then(ref => {
fs.unlinkSync(localFile);
})
}
// They made it, send them a welcome message.
function notifyNewTextador() {
let twiml = new MessagingResponse();
req.session.counter = 0;
twiml.message('Your profile is complete. Visit online at:\nhttps://textador.com/user/' + thisUser + '\n\nValid commands:\n\nREAD - read recent posts\nJOBS - find jobs\POST - post a message\n\nLIST - other commands\n\nYour PASSCODE: Remember this for authentication:\n\n' + passcode);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + req.session.counter);
}
// this helps with keeping things flowing safely
uploadImage();
setTimeout(saveUserToDatabase, 3000);
setTimeout(notifyNewTextador, 3000);
// they made it, so we can set the counter back to 0.
});
}
// Something is not right with the image. They get two more tries.
else {
message = 'Your photo did not meet our guidlines. Please do not use inapropriate images.';
if (result.text.has_artificial > .1) {
message = message + "\n\n The image contains artificial text. Please send a real photo of yourself";
}
if (result.scam.prob > .15 ) {
message = message + "\n\n The image may be a scam. Please send a real photo of yourself"
}
if (result.drugs > .1 ) {
message = message + "\n\n The image may contain drugs. Please send a real photo of yourself"
}
if (result.alcohol > .1 ) {
message = message + "\n\n The image may contain alcohol. Please send a real photo of yourself"
}
if (result.weapon > .1 ) {
message = message + "\n\n The image may contain weapons. Please send a real photo of yourself"
}
if (result.type.illustration > .15 ) {
message = message + "\n\n This may not be a photograph. Please send a real photo of yourself"
}
if (result.offensive.prob > .1) {
message = message + "\n\n The image may be offensive to some. Please send a real photo of yourself"
}
if (result.nudity.safe < .9) {
message = message + "\n\n The image may contain nudity. Please send an appropriate image of yourself"
}
req.session.counter = smsCount + 1;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
}).catch(function(err) {
// Handle error
console.log(err);
console.log("issue with image scan. Please use .jpg images");
});
}
else {
let twiml = new MessagingResponse();
req.session.counter = smsCount + 1;
message = ('Please send a photograph taken from your phone.');
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + smsCount);
}
}).then(ref => {
console.log("369 thisUserSnap: ", ref)
})
}
// building a USER Reverse SMS count to avoid multiple if/else
if (smsCount === 103) {
console.log('smsCount === 103')
thisUser = users.where("myCellNumber", "==", messageNumber).get().then(thisUserSnap => {
let doc = thisUserSnap.docs[0];
let thisUser = doc.id;
console.log("this user: ", thisUser);
// if they don't reply yes, change their name.
// Let's start building the new Textador, we now have a user, so we won't check to newuser.empty any more
db.collection('users').doc(thisUser).update({
handle: messageBody
}).then(ref => {
// Let's confirm address and get photo
var twiml = new MessagingResponse();
smsCount = 103
req.session.counter = smsCount + 1;
message = 'Now send a PHOTO. Guidlines:\n\n1) Taken in the last 10 minutes.\n2) Your FACE should be visible.\n3)No Nudity, Symbols or Text.';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("FIRST DATABSE SAVE: ", req.session.counter)
}).catch (err => {
console.log(err.errors[0].message);
});
})
}
// finding the RING NUMBER PIN for a new USER
if (smsCount >= 100 && smsCount <= 102) {
thisRing = users.where("mySystemNumber", "==", systemNumber).get().then(thisRingSnap => {
let doc = thisRingSnap.docs[0];
joinPin = doc.data().joinPin.toString();
console.log("Join PIN: ", joinPin);
// New user with invalid PIN
if (smsCount >= 100 && smsCount <= 102 && messageBody != joinPin) {
console.log('smsCount > 100 && smsCount <= 102 && messageBody != pin')
var twiml = new MessagingResponse();
// they three times with this message, then drop all they way down to the last esle here.
req.session.counter = smsCount + 1;
message = 'You must enter a valid PIN to join this TEXTADOR RING.';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
// New user with valid PIN
else if (smsCount >= 100 && smsCount <= 102 && messageBody == joinPin) {
console.log('smsCount > 100 && smsCount <= 102 && messageBody == pin')
console.log("319 SESSION COUNTER ", req.session.counter);
unirest.get("https://proapi.whitepages.com/3.1/phone?api_key=8c06abca696f4b28933f068e34bff006&phone="+messageNumber)
.header("X-RapidAPI-Host", "wppro.p.rapidapi.com")
.end(function (result) {
if (result.body.belongs_to.firstname) {
myName = result.body.belongs_to.firstname
message = 'WELCOME' + myName + '\nTo use this NAME, reply YES. Or send a NAME you want to use for your account.';
}
else if (result.body.belongs_to.name) {
myName = result.body.belongs_to.name
message = 'WELCOME' + myName + '\nTo use this NAME, reply YES. Or send a NAME you want to use for your account.';
}
else {
myName = '';
message = 'WELCOME\nChoose a NAME you want to use for your account.';
}
whitePagesResults = result.body;
db.collection('users').add({
myCellNumber: messageNumber,
myRingNumber: systemNumber,
createdAt: new Date(),
whitePages: whitePagesResults,
balance: 100,
passcode: crypto.randomBytes(2).toString('hex')
}).then(ref => {
var twiml = new MessagingResponse();
// they three times with this message, then drop all they way down to the last esle here.
smsCount = 102;
req.session.counter = smsCount + 1;
twiml.message("Send a NAME you want to use for your account.");
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("329 REVERSE LOOKUP: ", req.session.counter)
})
});
}
});
}
// setup the workflow for POST...
if (req.session.counter >= 200 && req.session.counter <= 299){
console.log("POST ++++++++++++++++++++++++")
// Let's get the Ring Owner and Message Owner
user = users.where("myCellNumber", "==", messageNumber).get().then(snapshot => {
if (snapshot.empty) {
// we don't have a user in the system.
console.log("NO USER FOUND")
let twiml = new MessagingResponse();
message = 'You need to be a member of this RING to POST.\n\nPlease JOIN the RING first.';
req.session.counter = 0;
myPostTitle = messageBody
twiml.message(message);
res.writeHead(200, {'Content-Type': 'texts/xml'});
res.end(twiml.toString());
// we could change the session counter to CREATE USER
}
else {
snapshot.forEach(doc => {
console.log("WE FOUND THIS USER: ", doc.id);
// We will use these variables for the person sending this message
handle = doc.data().handle.toUpperCase();
passcode = doc.data().passcode.toString().trim();
myCellNumber = doc.data().myCellNumber;
mySystemNumber = doc.data().mySystemNumber;
myTextadorImage = doc.data().textadorImage;
balamce = doc.data().balance;
if (req.session.counter >= 201 && req.session.counter <= 202 && messageBody === passcode){
console.log("Successfull Passcode")
console.log("Handle of person sending message: " + handle);
console.log("Message PASSCODE: " + messageBody);
console.log("Message Count: " + smsCount);
let twiml = new MessagingResponse();
req.session.counter = 203;
message = ('Post to ' + systemNumber + '\n\nSend the TITLE now: ');
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + smsCount);
}
else if (req.session.counter >= 201 && req.session.counter <= 202 && messageBody != passcode){
console.log("BAD Passcode")
console.log("Handle of person sending message: " + handle);
console.log("Message PASSCODE: " + messageBody);
console.log("Message Count: " + smsCount);
let twiml = new MessagingResponse();
req.session.counter = smsCount + 1;
message = ('Sorry ' + handle + '\nBAD PASSCODE|\nSend your PASSCODE now: ');
twiml.message(message);
res.writeHead(200, {
'Content-Type': 'text/xml',
});
res.end(twiml.toString());
console.log("Session Counter: " + smsCount);
}
else if (smsCount === 203) {
console.log("smsCount == 203")
let twiml = new MessagingResponse();
message = 'Post TITLE is: ' + messageBody + '\n\n Send your MESSAGE now: ';
req.session.counter = smsCount + 1;
myPostTitle = messageBody
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Post Builder: " + myPostTitle);
console.log("Session Counter: " + smsCount);
}
else if (smsCount === 204) {
console.log('smsCount === 204')
let twiml = new MessagingResponse();
message = 'Post MESSAGE is: ' + messageBody + '\n\n Send a PHOTO now: ';
req.session.counter = smsCount + 1;
myPostDescription = messageBody
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
else if (smsCount >= 205 && smsCount <= 207) {
// post with a photo
console.log('smsCount >= 205 && smsCount <= 207')
if (req.body.NumMedia !== '0') {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
};
const filename = `${req.body.MessageSid}.jpg`;
const url = req.body.MediaUrl0;
let localFile = `./tmp/${req.body.MessageSid}.jpg`;
sightengine.check(['nudity', 'type', 'wad', 'scam', 'text', 'offensive']).set_url(url).then(function(result) {
console.log("========image scan results=====>", result);
console.log("=========image scan url=====>", url);
// if the image passes moderation, then...
if (result.offensive.prob <= .1 && result.nudity.safe >= .9 && result.weapon <= .1 && result.drugs <= .1 && result.alcohol <= .1 && result.scam.prob <= .15 && result.type.illustration <= .15) {
// Download the image
request(url).pipe(fs.createWriteStream(localFile))
.on('close', () => {
console.log('Image downloaded. In new post from member');
// upload image
function uploadPostImage() {
storage.bucket(bucketName).upload(localFile, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
}).catch (err => {
console.log("ERROR: ", err);
});
}
// save post to database
function saveOthersPostToDatabase() {
db.collection('posts').add({
title: myPostTitle,
description: myPostDescription,
image: `https://storage.googleapis.com/textadorposts/${filename}`,
authorId: doc.id,
authorHandle: handle,
ringNumber: systemNumber,
imageModeration: result,
createdAt: new Date()
}).then(ref => {
console.log('Added document with ID: ', ref.id);
// send url to the post.
let twiml = new MessagingResponse();
message = 'Your POST is now live!';
req.session.counter = 0;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
fs.unlinkSync(localFile);
notifyRingNewPost();
}).catch (err => {
console.log(err.errors[0].message);
});
}
// They made it, send them a welcome message.
function notifyRingNewPost() {
let imageUrl = `https://storage.googleapis.com/textadorposts/${filename}`;
client.messages.create({
body: 'New Post: ' + doc.data().handle + ' \njoinded today and now is a subscriber of ' + mySystemNumber + '\n\nView their profile online at:\nhttps://textador.com/users/' + doc.id,
mediaUrl: imageUrl,
to: ringOwnerCellNumber,
from: mySystemNumber
})
.then(message => console.log(message.sid))
.catch (err => {
console.log(err.errors[0].message);
});
}
// this helps with keeping things flowing safely
uploadPostImage();
setTimeout(saveOthersPostToDatabase, 3000);
})
}
// Something is not right with the image. They get two more tries.
else {
message = 'Your photo did not meet our guidlines. Please do not use inapropriate images.';
if (result.text.has_artificial > .1) {
message = message + "\n\n The image contains artificial text. Please send a real photo of yourself";
}
if (result.scam.prob > .15 ) {
message = message + "\n\n The image may be a scam. Please send a real photo of yourself"
}
if (result.drugs > .1 ) {
message = message + "\n\n The image may contain drugs. Please send a real photo of yourself"
}
if (result.alcohol > .1 ) {
message = message + "\n\n The image may contain alcohol. Please send a real photo of yourself"
}
if (result.weapon > .1 ) {
message = message + "\n\n The image may contain weapons. Please send a real photo of yourself"
}
if (result.type.illustration > .15 ) {
message = message + "\n\n This may not be a photograph. Please send a real photo of yourself"
}
if (result.offensive.prob > .1) {
message = message + "\n\n The image may be offensive to some. Please send a real photo of yourself"
}
if (result.nudity.safe < .9) {
message = message + "\n\n The image may contain nudity. Please send an appropriate image of yourself"
}
let twiml = new MessagingResponse();
req.session.counter = smsCount + 1;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
}).catch(function(err) {
// Handle error
console.log(err);
console.log("issue with image scan. Please use .jpg images");
});
}
// post without a photo
else {
db.collection('posts').add({
title: myPostTitle,
description: myPostDescription,
authorId: doc.id,
authorHandle: handle,
ringNumber: systemNumber,
imageModeration: result,
createdAt: new Date()
}).then(ref => {
console.log('Added document with ID: ', ref.id);
// send url to the post. message = 'Your POST is now live!';
req.session.counter = 0;
twiml.message(message);
res.writeHead(200, {
'Content-Type': 'text/xml',
});
res.end(twiml.toString());
}).catch (err => {
console.log(err.errors[0].message);
});
}
}
else {
let twiml = new MessagingResponse();
req.session.counter = 0;
message = ('Sorry ' + handle + '\nsomething has gone wrong.\n\nPlease send list valid commands.');
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + smsCount);
}
}).catch (err => {
console.log(err.errors[0].message);
});
}
})
}
// setup the workflow for READ...
if (req.session.counter >= 300 && req.session.counter <= 399){
console.log("READ ++++++++++++++++++++++++")
}
// setup the workflow for JOBS...
if (req.session.counter >= 400 && req.session.counter <= 499){
console.log("JOBS ++++++++++++++++++++++++")
}
// setup the workflow for WORK...
if (req.session.counter >= 500 && req.session.counter <= 599){
console.log("(req.session.counter >= 500 && req.session.counter <= 599)")
console.log("730 this System number: ", systemNumber);
if (smsCount == 516) {
// Let's send a message to the Boss and let him take a look
let twiml = new MessagingResponse();
let message = twiml.message();
req.session.counter = 0;
message.body('Job Complete.\nThe WAGE has been added to your balance.');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + req.session.counter);
}
else {
thisWork = db.collection('jobs').where('jobOpen', '==', true).where("systemNumber", "==", systemNumber).orderBy('createdAt', 'desc').get().then((snapshot) => {
// Empty jobs
if (snapshot.empty) {
console.log('Bad Request');
// Let's send a message to the Boss and let him take a look
let twiml = new MessagingResponse();
let message = twiml.message();
req.session.counter = 0;
message.body('Something went wrong, please send LIST for a list of valid commands.');
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("Session Counter: " + smsCount);
client.messages.create({
body: 'We got a message from this number: ' + systemNumber + '/n/nFrom: ' + messageNumber + '\n\nSomething looks fishy, we could not find a job or textador with that number.',
to: "+16786376521",
from: mySystemNumber
})
.then(message => console.log(message.sid))
.catch (err => {
console.log(err.errors[0].message);
});
}
// THIS belongs to a JOB.
else {
// THIS NUMBER BELONGS TO A JOB
// GET A SNAPSHOT OF THE JOB INFO
let doc = snapshot.docs[0];
let title = doc.data().title;
title = title.toUpperCase();
let wage = doc.data().wage;
let startAddress = doc.data().startAddress;
thisJobId = doc.id;
title = doc.data().title.toUpperCase();
description = doc.data().description;
startAddress = doc.data().startAddress;
bossCellNumber = doc.data().jobCreatorPhoneNumber;
user = users.where("myCellNumber", "==", messageNumber).get().then(userSnapshot => {
if (snapshot.empty) {
// we don't have a user in the system.
console.log("NO USER FOUND")
let twiml = new MessagingResponse();
message = 'You need to be a member of this RING.\n\nPlease JOIN the RING first.';
req.session.counter = 0;
myPostTitle = messageBody
twiml.message(message);
res.writeHead(200, {'Content-Type': 'texts/xml'});
res.end(twiml.toString());
// we could change the session counter to CREATE USER
}
else {
let thisWorker = userSnapshot.docs[0];
thisWorkerId = thisWorker.id;
thisWorker = db.collection('users').doc(thisWorkerId);
getDoc = thisWorker.get().then(jobCreator => {
if (!jobCreator.exists) {
console.log('No such BOSS!');
} else {
console.log("WE FOUND THIS WORKER ID: ", thisWorkerId);
console.log("WE FOUND THIS JOB Creator Handle: ", jobCreator.data().handle);
// We will use these variables for the person sending this message
handle = jobCreator.data().handle.toUpperCase();
passcode = jobCreator.data().passcode;
balance = jobCreator.data().balance;
console.log("Boss Cell: ", bossCellNumber)
// New user with invalid PIN
if (smsCount > 500 && smsCount <= 502 && messageBody != passcode) {
var twiml = new MessagingResponse();
// they three times with this message, then drop all they way down to the last esle here.
req.session.counter = smsCount + 1;
message = 'You must enter a valid PIN to get started.';
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
}
// New user with valid PIN
else if (smsCount >= 500 && smsCount <= 502 && messageBody === passcode) {
console.log("Pin passed: ", passcode)
console.log("SMS Count: ", smsCount)
console.log("Job Title: ", title)
if (title == 'CASH') {
message = 'You are a banker. Make sure you have the right amount of cash. You are responsible for any math errors. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:';
}
else if (title == 'RIDE') {
message = 'You are giving someone a ride. Be a safe driver. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else if (title == 'DELIVER') {
message = 'You are a delivery person. Please be careful and drive safely. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else if (title == 'PHOTO') {
message = 'You are taking a photo. Make sure to get a great picture. Accuracy is important. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else if (title == 'LABOR') {
message = 'You are strong, like a bull. Let\'s get ready to for a workout. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else if (title == 'COUNT') {
message = 'You are counting things. Be sure to count twice. Accuracy is important. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else if (title == 'CLEAN') {
message = 'You are making the world a cleaner place. Attention to detail is important. Please follow the instructions below: \n\nFrom the boss:\n' + description + '\n\nSEND a PHOTO of you starting the job now:'
}
else {
console.log("Bad job title");
}
let twiml = new MessagingResponse();
smsCount = 511
req.session.counter = smsCount + 1;
twiml.message(message);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("839 SMS COUNT: ", req.session.counter)
}
else if (smsCount >= 511 && smsCount <= 513) {
// got a photo
console.log('smsCount >= 511 && smsCount <= 513');
if (req.body.NumMedia !== '0') {
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const filename = `${req.body.MessageSid}.jpg`;
const url = req.body.MediaUrl0;
let localFile = `./tmp/${req.body.MessageSid}.jpg`;
console.log("++++++++ Got this PHOTO: ", url)
sightengine.check(['nudity', 'type', 'wad', 'scam', 'text', 'offensive']).set_url(url).then(function(result) {
console.log('853 checking...')
console.log("854 =========image scan results=====>", result.scam);
console.log("855 =========image scan url=====>", url);
// if the image passes moderation, then...
if (result.offensive.prob <= .1 && result.nudity.safe >= .9 && result.weapon <= .1 && result.drugs <= .1 && result.alcohol <= .1 && result.scam.prob <= .1) {
// Download the image
request(url).pipe(fs.createWriteStream(localFile))
.on('close', () => {
console.log('861 - Image downloaded passed image processing');
// upload image
function uploadWorkStartImage() {
console.log('uploadWorkStartImage')
storage.bucket(bucketName).upload(localFile, {
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
},
}).catch (err => {
console.log("ERROR: ", err);
});
saveWorkStartToDatabase();
}
// save post to database
function saveWorkStartToDatabase() {
console.log('saveWorkStartToDatabase')
db.collection('jobs').doc(thisJobId).update({
workStartPhoto: `https://storage.googleapis.com/textadorposts/${filename}`,
workStartTime: new Date(),
workerId: thisWorkerId
}).then(ref => {
console.log('889 >>>> Added document with ID: ', thisJobId);
fs.unlinkSync(localFile);
sendBossStartMessage();
}).catch(err => console.log("ERROR: ", err))
}
function sendBossStartMessage() {
console.log('904 notifyWorker')
let twiml = new MessagingResponse();
req.session.counter = 0;
messageBody = 'The BOSS says:\nGET TO WORK.\n\nSend DONE when the job is complete.';
twiml.message(messageBody);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
console.log("912 SMS COUNT: ", smsCount)
}
uploadWorkStartImage();
});
}
// Something is not right with the image. They get two more tries.
else {