-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
857 lines (742 loc) · 30.7 KB
/
index.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
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
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const axios = require('axios').default;
const user_db = require('./ORM/user/user_orm');
const user_ride_db = require('./ORM/user/user_ride_orm');
const owner_db = require('./ORM/owner/owner_orm');
const rate_card_db = require('./ORM/rate_card/rate_card');
// const wish_user = require('./wish/wish');
const user_state = require('./user_state/user_state');
const r_funcs = require('./reusable_funcs/r_funcs');
const ed = require('./encoder-decoder/ed');
const moment = require('moment');
const dotenv = require('dotenv');
const mysql = require('mysql2/promise');
const cron = require('node-cron');
const { format } = require('mysql2/promise')
const admin_state = require('./admin_state/admin_state');
// const { all } = require('bluebird');
dotenv.config();
const chat_init_msg = ['hi', 'hello', 'hey'];
const owners = [];
const owner_commands = ["show booking", "show bookings", "complete"]
const connect_database = async () => {
try {
let co = await mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.MYSQL_DATABASE,
});
console.log('Database Connected!');
return co;
} catch (e) {
console.error(e);
}
};
connect_database().then(async (con) => {
await user_db.set_user_db_connection(con);
await user_ride_db.set_user_ride_db_connection(con);
await owner_db.set_owner_db_connection(con);
await rate_card_db.set_rate_card_db_connection(con);
});
const client = new Client({
puppeteer: {
headless: false,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
},
authStrategy: new LocalAuth(),
});
client.on('loading_screen', (percent, message) => {
console.log('LOADING SCREEN', percent, message);
});
client.on('authenticated', () => {
console.log('AUTHENTICATED');
});
client.on('auth_failure', msg => {
// Fired if session restore was unsuccessful
console.error('AUTHENTICATION FAILURE', msg);
});
client.on(
'qr',
async (qr) => {
// Generate and scan this code with your phone
console.log('QR RECEIVED', qr);
await qrcode.generate(qr, { small: true });
},
(error) => {
console.log(error);
}
);
client.on(
'ready',
async () => {
console.log('Client is ready!');
owner_db.fetch_owners().then(async (data) => {
for (owner in data) {
owners.push(data[owner].owner_whatsapp_no);
}
// console.log(owners);
// owners.push(data[owner_whatsapp_no]);
})
cron.schedule('*/2 * * * *', async () => {
console.log("cron executed");
let ride_data = await user_ride_db.fetch_not_confirmed_rides();
// console.log("executed")
ride_data.forEach(async (r_data) => {
var hours = Math.abs(new Date() - r_data.registered_datetime) / 36e5;
// console.log(hours)
// let registered_datetime = parseFloat(moment(r_data.registered_datetime, 'hh:mm A').format('HH:mm').replace(":", "."));
// let currentTime = parseFloat(moment().format("HH:mm").toString().replace(":", "."));
// console.log(currentTime - registered_datetime)
if (hours >= 1) {
// console.log(`Status Deleted ${r_data.user_whatsapp_no} ${hours}`)
await SendRideConfirmedMessageToOwner(r_data.user_whatsapp_no);
await user_ride_db.set_ride_completed(r_data.user_whatsapp_no);
await user_db.updateUserState(r_data.user_whatsapp_no, 1);
let number_format = `${r_funcs.extract_integer(r_data.user_whatsapp_no)}@c.us`;
await client.sendMessage(number_format, "Your Ride is Completed By BookMyDriver");
}
});
});
},
(error) => {
console.log(error);
}
);
const SendAllConfirmedBookingsToOwner = async (owner_whatsapp_no, client_number = null) => {
let number_format = `${r_funcs.extract_integer(owner_whatsapp_no)}@c.us`;
let ride_data = undefined;
if (client_number != null) {
// console.log(client_number)
// let customer_number_format = `${r_funcs.extract_integer(client_number)}@c.us`;
ride_data = await user_ride_db.fetch_confimed_client_ride(client_number);
} else {
ride_data = await user_ride_db.fetch_confimed_rides();
}
if (ride_data.length == 0) {
await client.sendMessage(number_format, '*No Ride Found!*');
} else {
await client.sendMessage(number_format, '*--------------------*');
await client.sendMessage(number_format, '*All Ride Confirmed!*');
for (confirmed_ride in ride_data) {
let dn = null;
if (ride_data[confirmed_ride].day_or_night == null) {
dn = 'NA';
} else if (ride_data[confirmed_ride].day_or_night == 1) {
dn = 'Day';
} else {
dn = 'Night';
}
let local_or_outstation = null;
if (ride_data[confirmed_ride].local_or_outstation == 1) {
local_or_outstation = 'Local';
} else {
local_or_outstation = 'Outstation';
}
let oneway_or_return = null;
if (ride_data[confirmed_ride].oneway_or_return == 1) {
oneway_or_return = 'Oneway';
} else {
oneway_or_return = 'Return';
}
let date_time =
String(
moment.utc(ride_data[confirmed_ride].ride_date).format('DD/MM/YYYY')
) +
' ' +
ride_data[confirmed_ride].ride_time;
ride_data[confirmed_ride].pickup_location = await ed.decode_ed(
ride_data[confirmed_ride].pickup_location
);
ride_data[confirmed_ride].drop_location = await ed.decode_ed(
ride_data[confirmed_ride].drop_location
);
await user_db
.isUserExists(ride_data[confirmed_ride].user_whatsapp_no)
.then(async (user_data) => {
// await client.sendMessage(number_format, user_data[0].user_fullname);
let confirmed_msg = `Name: ${user_data[0].user_fullname} \nNumber: ${user_data[0].user_whatsapp_no} \nPick Up Address: ${ride_data[confirmed_ride].pickup_location} \nDrop Address: ${ride_data[confirmed_ride].drop_location} \n${dn} ${date_time} \n${local_or_outstation} : ${oneway_or_return} \n*Ride Confirmed!* `;
await client.sendMessage(number_format, confirmed_msg);
});
}
await client.sendMessage(number_format, '*End Ride Confirmed!*');
await client.sendMessage(number_format, '*--------------------*');
}
};
const SendRideConfirmedMessageToOwner = async (client_whatsapp_no) => {
await user_db.isUserExists(client_whatsapp_no).then(async (result) => {
let user_data = result[0];
await owner_db.fetch_owners().then(async (data) => {
let ride_data = await user_ride_db.get_user_ride_confirmed(
client_whatsapp_no
);
if (ride_data.length == 0) {
return;
}
let dn = null;
if (ride_data[0].day_or_night == null) {
dn = 'NA';
} else if (ride_data[0].day_or_night == 1) {
dn = 'Day';
} else {
dn = 'Night';
}
let local_or_outstation = null;
if (ride_data[0].local_or_outstation == 1) {
local_or_outstation = 'Local';
} else {
local_or_outstation = 'Outstation';
}
let oneway_or_return = null;
if (ride_data[0].oneway_or_return == 1) {
oneway_or_return = 'Oneway';
} else {
oneway_or_return = 'Return';
}
let date_time =
String(moment.utc(ride_data[0].ride_date).format('DD/MM/YYYY')) +
' ' +
ride_data[0].ride_time;
for (user in data) {
let number_format = `${r_funcs.extract_integer(
data[user].owner_whatsapp_no
)}@c.us`;
let pickup_location_decoded = await ed.decode_ed(
ride_data[0].pickup_location
);
let drop_location_decoded = await ed.decode_ed(
ride_data[0].drop_location
);
let confirmed_msg = `Name: ${user_data.user_fullname} \nNumber: ${user_data.user_whatsapp_no} \nPick Up Address: ${pickup_location_decoded} \nDrop Address: ${drop_location_decoded} \n${dn} ${date_time} \n${local_or_outstation} : ${oneway_or_return} \n*Ride Confirmed!* `;
await client.sendMessage(number_format, confirmed_msg);
}
});
});
};
const SendRideCancledMessageToOwner = async (client_whatsapp_no) => {
await user_db.isUserExists(client_whatsapp_no).then(async (result) => {
let user_data = result[0];
// console.log(user_data)
await owner_db.fetch_owners().then(async (data) => {
let ride_data = await user_ride_db.get_user_ride_confirmed(
client_whatsapp_no
);
if (ride_data.length == 0) { return; }
// console.log(ride_data)
let dn = null;
if (ride_data[0].day_or_night == null) {
dn = 'NA';
} else if (ride_data[0].day_or_night == 1) {
dn = 'Day';
} else {
dn = 'Night';
}
let local_or_outstation = null;
if (ride_data[0].local_or_outstation == 1) {
local_or_outstation = 'Local';
} else {
local_or_outstation = 'Outstation';
}
let oneway_or_return = null;
if (ride_data[0].oneway_or_return == 1) {
oneway_or_return = 'Oneway';
} else {
oneway_or_return = 'Return';
}
let date_time =
String(moment.utc(ride_data[0].ride_date).format('DD/MM/YYYY')) +
' ' +
ride_data[0].ride_time;
for (user in data) {
let number_format = `${r_funcs.extract_integer(
data[user].owner_whatsapp_no
)}@c.us`;
let pickup_location_decoded = await ed.decode_ed(
ride_data[0].pickup_location
);
let drop_location_decoded = await ed.decode_ed(
ride_data[0].drop_location
);
let cancelled_msg = `Name: ${user_data.user_fullname} \nNumber: ${user_data.user_whatsapp_no} \nPick Up Address: ${pickup_location_decoded} \nDrop Address: ${drop_location_decoded} \n${dn} ${date_time} \n${local_or_outstation} : ${oneway_or_return} \n*Ride Cancelled!* `;
await client.sendMessage(number_format, cancelled_msg);
}
});
});
};
client.on('message', async (msg) => {
// console.log(msg);
console.log(owners);
// automation for personal chat
if (msg.type === 'chat') {
const message = msg.body.toLowerCase();
let user_whatsapp_no = '+' + r_funcs.extract_integer(msg.from);
owner_number = owners.indexOf(user_whatsapp_no)
if (owner_number != -1) {
let current_owner_number = owners[owner_number];
let number_format = `${r_funcs.extract_integer(current_owner_number)}@c.us`;
// console.log("owner");
// console.log(owner_number);
let command = message.replace(/ /g, "").split(",");
// console.log(command);
// console.log(command.length);
if (chat_init_msg.includes(message)) {
await client.sendMessage(number_format, admin_state.admin_menu());
}
else if (message == "show booking" || message == "show bookings") {
await SendAllConfirmedBookingsToOwner(number_format);
}
else if (command.length == 2) {
let customer_number = command[1];
if (customer_number.length != 12) {
await client.sendMessage(number_format, "Please Enter Valid Number with Country Code (91) in Prefix");
}
else if (command[0] == "showbooking" || command[0] == "showbookings") {
await SendAllConfirmedBookingsToOwner(number_format, `+${customer_number}`);
}
else if (command[0] == "complete") {
let ride_data = await user_ride_db.fetch_confimed_client_ride(`+${customer_number}`);
console.log(ride_data);
if (ride_data.length > 0) {
if (ride_data[0].ride_status == 2) {
await user_ride_db.set_ride_completed(`+${customer_number}`);
let customer_number_format = `${r_funcs.extract_integer(
customer_number
)}@c.us`;
await client.sendMessage(number_format, "Customer Ride Status Updated From *Pending* to *Completed* !");
await client.sendMessage(customer_number_format, "Thank you!");
} else {
// not working because in data the ride_status is 2 so if no data is coming No ride found is executing
await client.sendMessage(number_format, "Ride Already Completed !");
}
} else {
await client.sendMessage(number_format, "*No Ride Found!*");
}
}
}
// else if (message == 'show bookings' || message == 'show booking') {
// }
}
else if (chat_init_msg.includes(message)) {
// console.log("normal user");
await user_db.isUserExists(user_whatsapp_no).then(async (result) => {
if (result.length < 1) {
await user_db.registerUser(user_whatsapp_no);
client.sendMessage(msg.from, user_state.user_state_0());
} else {
if (result[0].user_fullname === null) {
client.sendMessage(msg.from, user_state.user_state_0());
} else {
let user_ride_data = await user_ride_db.get_user_ride_confirmed(
user_whatsapp_no
);
// console.log(user_ride_data.length , user_ride_data);
if (user_ride_data.length > 0) {
client.sendMessage(msg.from, user_state.user_menu());
} else {
// client.sendMessage(msg.from, user_state.user_menu());
let user_data = result[0];
if (user_data.user_state == 0) {
client.sendMessage(msg.from, user_state.user_state_0());
} else if (user_data.user_state == 1) {
client.sendMessage(
msg.from,
user_state.user_state_1(user_data.user_fullname)
);
} else if (user_data.user_state == 2) {
client.sendMessage(msg.from, user_state.user_state_2());
} else if (user_data.user_state == 3) {
client.sendMessage(msg.from, user_state.user_state_3());
} else if (user_data.user_state == 4) {
client.sendMessage(msg.from, user_state.user_state_4());
} else if (user_data.user_state == 5) {
client.sendMessage(msg.from, user_state.user_state_5());
} else if (user_data.user_state == 6) {
client.sendMessage(msg.from, user_state.user_state_6());
} else if (user_data.user_state == 7) {
client.sendMessage(msg.from, user_state.user_state_7());
} else if (user_data.user_state == 8) {
client.sendMessage(msg.from, user_state.user_state_8());
} else if (user_data.user_state == 9) {
client.sendMessage(msg.from, user_state.user_state_9());
} else if (user_data.user_state == 10) {
client.sendMessage(
msg.from,
user_state.user_state_10(user_data.user_whatsapp_no)
);
} else if (user_data.user_state == 11) {
client.sendMessage(msg.from, user_state.user_state_11());
} else if (user_data.user_state == 12) {
client.sendMessage(msg.from, user_state.user_state_12());
} else if (user_data.user_state == 13) {
client.sendMessage(msg.from, user_state.user_state_13());
}
client.sendMessage(msg.from, user_state.user_menu());
}
}
}
});
} else {
await user_db.isUserExists(user_whatsapp_no).then(async (result) => {
if (result.length == 1) {
let user_data = result[0];
if (msg.body.toLowerCase() == 'cancel') {
try {
await SendRideCancledMessageToOwner(user_whatsapp_no);
} catch { }
await user_ride_db.cancle_ride_booking(user_whatsapp_no);
await user_db.updateUserState(user_whatsapp_no, 1);
let ride_data = await user_ride_db.get_user_ride(
user_whatsapp_no
);
if (ride_data[0].ride_status == 1 || ride_data[0].ride_status == 2) {
await client.sendMessage(
msg.from,
'Your Ride is Cancelled Successfully!'
);
} else {
await client.sendMessage(
msg.from,
'No Ride Found!'
);
}
// client.sendMessage(
// msg.from,
// 'Your Ride is Cancelled Successfully!'
// );
} else if (msg.body.toLowerCase() == 'menu') {
client.sendMessage(msg.from, user_state.user_menu());
} else if (msg.body == 11) {
let ride_data = await user_ride_db.get_user_ride_confirmed(
user_whatsapp_no
);
if (ride_data.length > 0) {
let dn = null;
if (ride_data[0].day_or_night == null) {
dn = 'NA';
} else if (ride_data[0].day_or_night == 1) {
dn = 'Day';
} else {
dn = 'Night';
}
let local_or_outstation = null;
if (ride_data[0].local_or_outstation == 1) {
local_or_outstation = 'Local';
} else {
local_or_outstation = 'Outstation';
}
let oneway_or_return = null;
if (ride_data[0].oneway_or_return == 1) {
oneway_or_return = 'Oneway';
} else {
oneway_or_return = 'Return';
}
let date_time =
String(
moment.utc(ride_data[0].ride_date).format('DD/MM/YYYY')
) +
' ' +
ride_data[0].ride_time;
let booked_ride__data_msg = `${user_data.user_fullname
} \nPick Up Address: ${await ed.decode_ed(
ride_data[0].pickup_location
)} \nDrop Address: ${await ed.decode_ed(
ride_data[0].drop_location
)} \n${dn} ${date_time} \n${local_or_outstation} : ${oneway_or_return} `;
client.sendMessage(msg.from, booked_ride__data_msg);
} else {
client.sendMessage(msg.from, 'No Ride Found!');
}
} else if (msg.body == 12) {
let ride_data = await user_ride_db.get_user_ride(
user_whatsapp_no
);
if (ride_data.length > 0) {
await SendRideCancledMessageToOwner(user_whatsapp_no);
await user_ride_db.cancle_ride_booking(user_whatsapp_no);
await user_db.updateUserState(user_whatsapp_no, 1);
if (ride_data[0].ride_status == 1 || ride_data[0].ride_status == 2) {
await client.sendMessage(
msg.from,
'Your Ride is Cancelled Successfully!'
);
} else {
await client.sendMessage(
msg.from,
'No Ride Found!'
);
}
} else {
client.sendMessage(
msg.from,
'No Ride Found!'
);
}
}
// save user Name
else if (user_data.user_state == 0) {
let name = r_funcs.titleCase(msg.body.trim());
if (/^[A-Za-z\s]+$/.test(name) && name.length > 2) {
await user_db.updateUserName(user_whatsapp_no, msg.body);
await user_db.updateUserState(user_whatsapp_no, 1);
client.sendMessage(msg.from, user_state.user_state_1(msg.body));
} else {
client.sendMessage(msg.from, "Please Enter Valid Name");
}
}
// save user travel option local/outstation
else if (user_data.user_state == 1) {
if (msg.body == 1 || msg.body == 2) {
if (msg.body == 1) {
await user_db.updateUserState(user_whatsapp_no, 2);
client.sendMessage(msg.from, user_state.user_state_2());
} else {
await user_db.updateUserState(user_whatsapp_no, 3);
client.sendMessage(msg.from, user_state.user_state_3());
}
user_ride_db.register_ride(user_whatsapp_no);
user_ride_db.set_ride_local_or_outstation(
user_whatsapp_no,
msg.body
);
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
}
// ask for day or night and save
else if (user_data.user_state == 2) {
if (msg.body == 1 || msg.body == 2) {
await user_ride_db.set_ride_day_or_night(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 3);
client.sendMessage(msg.from, user_state.user_state_3());
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
} else if (user_data.user_state == 3) {
if (msg.body == 1 || msg.body == 2) {
await user_ride_db.set_ride_oneway_or_return(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 4);
client.sendMessage(msg.from, user_state.user_state_4());
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
} else if (user_data.user_state == 4) {
if (msg.body == 1 || msg.body == 2) {
if (msg.body == 1) {
await rate_card_db.fetch_rate_card().then((data) => {
client.sendMessage(msg.from, 'Rate Card :) ');
for (d in data) {
let journey_type = data[d].shift_type
? 'Return '
: 'One Way';
let rate_card_msg = `Booking Type: ${data[d].booking_type} \nShift Type: ${data[d].shift_type} \nJourney Type: ${journey_type} \nStart Time: ${data[d].start_time} \nEnd Time: ${data[d].end_time} \nExtra Hour Price: ${data[d].extra_hour_price} \nNote: ${data[d].notes}`;
client.sendMessage(msg.from, rate_card_msg);
}
client.sendMessage(msg.from, user_state.user_state_4());
});
} else {
await user_db.updateUserState(user_whatsapp_no, 5);
client.sendMessage(msg.from, user_state.user_state_5());
}
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
} else if (user_data.user_state == 5) {
await user_ride_db.set_ride_pickup_location(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 6);
client.sendMessage(msg.from, user_state.user_state_6());
} else if (user_data.user_state == 6) {
await user_ride_db.set_ride_drop_location(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 7);
client.sendMessage(msg.from, user_state.user_state_7());
} else if (user_data.user_state == 7) {
if (moment(msg.body, 'DD/MM/YYYY', true).isValid()) {
let db_format_date = moment(msg.body, 'DD/MM/YYYY').format(
'YYYY/MM/DD'
);
if (r_funcs.isToday(moment(db_format_date).toDate()) || r_funcs.isFuture(moment(db_format_date).toDate())) {
await user_ride_db.set_ride_date(
user_whatsapp_no,
db_format_date
);
await user_db.updateUserState(user_whatsapp_no, 8);
client.sendMessage(msg.from, user_state.user_state_8());
}
else {
client.sendMessage(
msg.from,
'Past Date is not Allowed!'
);
}
} else {
client.sendMessage(
msg.from,
'Please Choose Valid Date : DD/MM/YYYY'
);
}
} else if (user_data.user_state == 8) {
if (moment(msg.body, 'LT', true).isValid()) {
if (msg.body.toLowerCase().endsWith("am") || msg.body.toLowerCase().endsWith("pm")) {
let ride_data = await user_ride_db.get_user_ride(user_whatsapp_no);
if (r_funcs.isToday(ride_data[0].ride_date)) {
let convertedTime = parseFloat(moment(msg.body.toUpperCase(), 'hh:mm A').format('HH:mm').replace(":", "."));
let currentTime = parseFloat(moment().format("HH:mm").toString().replace(":", "."));
if (convertedTime > currentTime) {
await user_ride_db.set_ride_time(user_whatsapp_no, msg.body.toUpperCase());
await user_db.updateUserState(user_whatsapp_no, 9);
client.sendMessage(msg.from, user_state.user_state_9());
} else {
client.sendMessage(msg.from, "The time Should be More than Present Time!");
}
} else if (r_funcs.isFuture(ride_data[0].ride_date)) {
await user_ride_db.set_ride_time(user_whatsapp_no, msg.body.toUpperCase());
await user_db.updateUserState(user_whatsapp_no, 9);
client.sendMessage(msg.from, user_state.user_state_9());
}
} else {
client.sendMessage(
msg.from,
'Please Choose Valid Time : HH:MM AM/PM'
);
}
} else {
client.sendMessage(
msg.from,
'Please Choose Valid Time : HH:MM AM/PM'
);
}
} else if (user_data.user_state == 9) {
if (msg.body == 1 || msg.body == 2) {
if (msg.body == 1) {
await user_ride_db.set_ride_confirm_booking(user_whatsapp_no);
client.sendMessage(
msg.from,
user_state.user_state_10(user_data.user_fullname)
);
await SendRideConfirmedMessageToOwner(
user_data.user_whatsapp_no
);
} else {
await user_db.updateUserState(user_whatsapp_no, 10);
client.sendMessage(msg.from, user_state.user_state_11());
}
} else {
if (msg.body.toLowerCase() != 'cancel') {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
}
} else if (user_data.user_state == 10) {
if (
msg.body == 1 ||
msg.body == 2 ||
msg.body == 3 ||
msg.body == 4 ||
msg.body == 5
) {
if (msg.body == 1) {
await user_db.updateUserState(user_whatsapp_no, 12);
client.sendMessage(msg.from, user_state.user_state_12());
} else if (msg.body == 2) {
await user_db.updateUserState(user_whatsapp_no, 13);
client.sendMessage(
msg.from,
user_state.user_state_13(user_data.user_fullname)
);
} else if (msg.body == 3) {
client.sendMessage(
msg.from,
user_state.user_stop(user_data.user_fullname)
);
} else if (msg.body == 4) {
await user_ride_db.cancle_ride_booking(user_whatsapp_no);
await user_db.updateUserState(user_whatsapp_no, 1);
client.sendMessage(msg.from, 'Restarted!');
client.sendMessage(
msg.from,
user_state.user_state_1(user_data.user_fullname)
);
} else if (msg.body == 5) {
await user_ride_db.set_ride_confirm_booking(user_whatsapp_no);
await user_db.updateUserState(user_whatsapp_no, 14);
client.sendMessage(
msg.from,
user_state.user_state_10(user_data.user_fullname)
);
client.sendMessage(msg.from, user_state.user_menu());
await SendRideConfirmedMessageToOwner(
user_data.user_whatsapp_no
);
}
} else {
client.sendMessage(msg.from, user_state.user_state_11());
}
} else if (user_data.user_state == 12) {
if (msg.body == 1 || msg.body == 2) {
await user_ride_db.set_ride_day_or_night(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 10);
client.sendMessage(msg.from, 'Updated Successfully!');
client.sendMessage(msg.from, user_state.user_state_11());
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
} else if (user_data.user_state == 13) {
if (msg.body == 1 || msg.body == 2) {
await user_ride_db.set_ride_oneway_or_return(
user_whatsapp_no,
msg.body
);
await user_db.updateUserState(user_whatsapp_no, 10);
client.sendMessage(msg.from, 'Updated Successfully!');
client.sendMessage(msg.from, user_state.user_state_11());
} else {
client.sendMessage(msg.from, 'Please Choose Valid Option');
}
}
} else {
// send working menu
}
});
}
}
// if (msg.body == '!ping') {
// await msg.reply('pong');
// }
// else if (msg.body.search("!name") != -1) {
// await msg.reply("hi " + msg.body.replace("!name", ""));
// }
// else if (msg.body == "!hi" || msg.body == "!hello" || msg.body == "!hey") {
// client.sendMessage(msg.from, "Hello Sir");
// client.sendMessage(msg.from, "How can i help you?");
// client.sendMessage(msg.from, "Options : 1 - login");
// client.sendMessage(msg.from, "Options : 2 - Register");
// }
// else if(msg.body == "1" || msg.body == "2"){
// client.sendMessage(msg.from, "Under Development");
// }
// else if (msg.body == "!joke") {
// try {
// client.sendMessage(msg.from, "🤔🤔");
// const response = await axios.get('https://api.chucknorris.io/jokes/random');
// client.sendMessage(msg.from, response.data.value);
// } catch (error) {
// console.error(error);
// }
// }
// console.log(await client.getWWebVersion())
});
client.initialize();