-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger.js
1253 lines (1109 loc) · 55.9 KB
/
messenger.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
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
'use strict'
var request = require('request');
var dof = require('./dofunc.js');
var database = require('./schema.js');
var c = require('./config.js');
var sort = require('./sort.js');
const token = c['fb'].page_token;
module.exports = {
sendTextMessage: function(sender, text) {
let messageData = { text: text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: sender },
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
})
},
empty: function(sender) {
delete sort.states.UserState[sender];
delete sort.data[sender];
// delete sort.MessageQueue.UserMessage[sender];
},
checkStatus: function(digitoken, status, sender, text, callback) {
// console.log(digitoken);
var mod = sort.states.UserState[sender].module;
var stage = sort.states.UserState[sender].stage;
if (text.toLowerCase() == 'exit') {
console.log("exit pressed");
module.exports.empty(sender);
module.exports.sendTextMessage(sender, "OPeration aborted")
}
//Enter token for first time user
else if (mod == 'eToken') {
if (stage == 1) {
console.log("working");
console.log(sort.states.UserState[sender]);
sort.states.UserState[sender].stage++;
console.log("chal rha hai");
module.exports.sendTextMessage(sender, "Enter token or press exir to abort the operation");
} else if (stage == 2) {
saveToken(sender, text);
module.exports.sendTextMessage(sender, "Token Saved");
module.exports.empty(sender);
}
}
//Update token
else if (mod == 'uToken') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
callback("You are about to enter token. Type exit to cancel this event")
} else if (stage == 2) {
updateToken(sender, text);
module.exports.empty(sender);
callback("Token Updated");
}
}
//List Droplet
else if (mod == 'lDroplet') {
if (stage == 1) {
module.exports.listDrop(digitoken, callback);
module.exports.empty(sender);
}
}
//List Action
else if (mod == 'lActions') {
if (stage == 1) {
// console.log("hello")
dof.lastActions(digitoken, function(body) {
// console.log(body.actions.length)
for (var i = 0; i < body.actions.length; i++) {
module.exports.sendTextMessage(sender, "Action: " + body.actions[i].type + "\nStatus: " + body.actions[i].status + "\nStarted At: " + body.actions[i].started_at + "\nCompleted At: " + body.actions[i].completed_at + "\nResource Type: " + body.actions[i].resource_type)
}
module.exports.empty(sender);
});
}
}
//List domain Module
else if (mod == 'lDomains') {
if (stage == 1) {
dof.listDomains(digitoken, function(body){
for(var i = 0; i<body.domains.length; i++ ){
callback("Name: "+body.domains[i].name+"\nTTL: "+body.domains[i].ttl+"\nZone File: "+body.domains[i].zone_file)
}
module.exports.empty(sender);
})
}
}
//Add domain records
else if (mod == 'aDomainRecord') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the domain")
} else if (stage == 2) {
sort.info[sender].domain = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the type of Record");
} else if (stage == 3) {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: text };
module.exports.sendTextMessage(sender, "Enter name if applicable or type null to leave this part");
} else if (stage == 4) {
sort.states.UserState[sender].stage++;
if (text.toLowerCase() != 'null') {
sort.data[sender].name = text;
}
module.exports.sendTextMessage(sender, "Enter data if applicable or type null to leave this part")
} else if (stage == 5) {
sort.states.UserState[sender].stage++;
if (text.toLowerCase() != 'null') {
sort.data[sender].data = text;
}
module.exports.sendTextMessage(sender, "Enter Priority if applicable or null");
} else if (stage == 6) {
sort.states.UserState[sender].stage++;
if (text.toLowerCase() != 'null') {
sort.data[sender].priority = text;
} else {
sort.data[sender].priority = null;
}
module.exports.sendTextMessage(sender, "Enter port if applicable or null");
} else if (stage == 7) {
sort.states.UserState[sender].stage++;
if (text.toLowerCase() != 'null') {
sort.data[sender].port = text;
} else {
sort.data[sender].port = null;
}
module.exports.sendTextMessage(sender, "Enter weight if applicable or null");
} else if (stage == 8) {
sort.states.UserState[sender].stage++;
if (text.toLowerCase() != 'null') {
sort.data[sender].weight = text;
} else {
sort.data[sender].weight = null;
}
module.exports.sendTextMessage(sender, "Press any key to continue or exit abort\n" + JSON.stringify(sort.data[sender]))
} else if (stage == 9) {
console.log(digitoken)
dof.addDomainRecord(digitoken, sort.info[sender].domain, sort.data[sender], function(body) {
// console.log(body);
if (body.id) {
module.exports.sendTextMessage(sender, "Id: " + body.id + "\n Message: " + body.message)
} else {
module.exports.sendTextMessage(sender, "Record Added\nDomain record id: " + body.domain_record.id + "\nType: " + body.domain_record.type + "\nName: " + body.domain_record.name + "\nData: " + body.domain_record.data + "\nTTL: " + body.domain_record.ttl)
}
module.exports.empty(sender);
})
}
}
//List domain records
else if (mod == 'lDomainRecords') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter domain name")
} else if (stage == 2) {
sort.info[sender].domain = text;
dof.listDomainRecords(digitoken, sort.info[sender].domain, function(body) {
console.log(body)
for (var i = 0; i < body.domain_records.length; i++) {
module.exports.sendTextMessage(sender, "domain record id: " + body.domain_records[i].id + "\ntype: " + body.domain_records[i].type + "\nName: " + body.domain_records[i].name + "\nData: " + body.domain_records[i].data + "\nPriority: " + body.domain_records[i].priority + "\nPort: " + body.domain_records[i].port + "\nttl: " + body.domain_records[i].ttl + "\nWeight: " + body.domain_records[i].weight);
}
module.exports.empty(sender);
})
}
}
//Delete domain records module
else if (mod == 'dDomainRecord') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter domain name")
} else if (stage == 2) {
sort.states.UserState[sender].stage++;
sort.info[sender].domain = text;
module.exports.sendTextMessage(sender, "Enter domain record id")
} else if (stage == 3) {
sort.info[sender].record = text;
dof.deleteDomainRecords(digitoken, sort.info[sender].domain, sort.info[sender].record, function(body) {
console.log(body.id)
if (body.id) {
module.exports.sendTextMessage(sender, "id: " + body.id + "\nMessage: " + body.message)
} else {
//TODO check if this is working
module.exports.sendTextMessage(sender, "Domain Record Deleted Successfully")
}
module.exports.empty(sender)
})
}
}
//List regions
else if (mod == 'lRegions') {
if (stage == 1) {
dof.listRegions(digitoken, function(body) {
console.log(body);
for (var i = 0; i < body.regions.length; i++) {
module.exports.sendTextMessage(sender, "Name: " + body.regions[i].name + "\nSlug: " + body.regions[i].slug + "\nSizes: " + body.regions[i].sizes + "\nFeatures: " + body.regions[i].features + "\nAvailability: " + body.regions[i].available);
}
module.exports.empty(sender);
})
}
}
//List Sizes
else if (mod == 'lSizes') {
if (stage == 1) {
dof.listSizes(digitoken, function(body) {
console.log(body);
for (var i = 0; i < body.sizes.length; i++) {
module.exports.sendTextMessage(sender, "Memory: " + body.sizes[i].slug + "\nVirtual CPU: " + body.sizes[i].vcpus + "\nDisk: " + body.sizes[i].disk + "gb\nMonthly Price: " + body.sizes[i].price_monthly + "$\nHourly Price: " + body.sizes[i].price_hourly + "$\nRegions Available: " + body.sizes[i].regions + "\nAvailability: " + body.sizes[i].available)
}
module.exports.empty(sender);
})
}
}
//Delete a Droplet
else if (mod == 'dDroplet') {
//TODO remove droplet from database
if (stage == 1) {
module.exports.sendTextMessage(sender, "Type \"current droplets\" to list droplets for deleting a droplet");
sort.states.UserState[sender].stage++;
} else if (stage == 2) {
//TODO if user types other than current droplet
if(text.toLowerCase() == 'current droplets'){
module.exports.listDrop(digitoken, callback)
sort.states.UserState[sender].stage++;
}
} else if (stage == 3) {
dof.deleteDroplet(digitoken, text, function(body) {
//TODO droplet delete success or fail
// console.log(body);
if(body.id){
callback("ID: "+body.id+"\nMessage: "+body.message)
}
else{
callback("Droplet is getting deleted")
}
module.exports.empty(sender)
})
}
}
//Create Droplet
else if (mod == 'cDroplet') {
//TODO Success message on creating droplet
if (stage == 1) {
sort.states.UserState[sender].stage++;
callback("Enter Name of the Droplet");
} else if (stage == 2) {
sort.data[sender] = { name: text };
// console.log("chal rha")
//TODO slug along with region name after fixing json array error
var regions = [];
sort.states.UserState[sender].stage++;
dof.listRegions(digitoken, function(body) {
for (var i = 0; i < body.regions.length; i++) {
regions.push(body.regions[i].slug);
}
console.log(regions);
callback("Enter region from following regions: \n" + regions);
});
} else if (stage == 3) {
sort.data[sender].region = text;
var size = [];
sort.states.UserState[sender].stage++;
dof.listSizes(digitoken, function(body) {
for (var i = 0; i < body.sizes.length; i++) {
size.push(body.sizes[i].slug);
}
console.log(size);
callback("Enter the size from following sizes: \n" + size);
});
} else if (stage == 4) {
sort.data[sender].size = text;
var image = [];
sort.states.UserState[sender].stage++;
dof.listDistImage(digitoken, function(body) {
// console.log(body);
for (var i = 0; i < body.images.length; i++) {
if (body.images[i].slug != null) {
console.log(body.images[i].slug);
image.push(body.images[i].slug);
}
}
console.log(image);
callback("Choose a image");
});
} else if (stage == 5) {
sort.data[sender].image = text;
sort.states.UserState[sender].stage++;
dof.createDroplet(digitoken, sort.data[sender], function(body) {
console.log(body);
if(body.id){
//TODO TEST THIS
callback("ID: "+body.id+"\nMessage: "+body.message)
}
else{
callback("Droplet Id: "+body.droplet.id+"\nName: "+body.droplet.name+"\nMemory: "+body.droplet.memory+"\nCreated At: "+body.droplet.created_at)
}
})
module.exports.empty(sender);
}
}
//Addig domain Module
else if (mod == 'aDomain') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the name of the domain");
} else if (stage == 2) {
sort.data[sender] = { name: text };
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the IP address");
} else if (stage == 3) {
sort.data[sender].ip_address = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to continue or exit to abort\n" + JSON.stringify(sort.data[sender]))
} else if (stage == 4) {
dof.createDomain(digitoken, sort.data[sender], function(body) {
if (body.id) {
module.exports.sendTextMessage(sender, body.message)
} else {
module.exports.sendTextMessage(sender, "Domain Added\nName: " + body.domain.name + "\nTTL: " + body.domain.ttl + "\nZone File: " + body.domain.zone_file);
}
console.log(body);
module.exports.empty(sender);
})
}
}
//Delete Domain Module
else if (mod == 'dDomain') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the domain you want to delete");
} else if (stage == 2) {
sort.data[sender] = { domain: text };
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to continue or exit to abort\n" + JSON.stringify(sort.data[sender]))
} else if (stage == 3) {
dof.deleteDomain(digitoken, sort.data[sender].domain, function(body) {
//TODO appropriate message for success and failure
console.log(body);
module.exports.empty(sender);
if (body.id) {
module.exports.sendTextMessage(sender, "id: " + body.id + "\nMessage: " + body.message)
} else {
//TODO check if this is working
module.exports.sendTextMessage(sender, "Domain Deleted Successfully")
}
})
}
}
//Action Droplet Module
else if (mod == 'aDroplet') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the droplet id for performing Action:");
} else if (stage == 2) {
sort.info[sender].id = text;
sort.states.UserState[sender].stage++;
// console.log(droplet);
module.exports.sendTextMessage(sender, "Select an Action \n1. Enable Backup \n2. Disable Backups \n3. Reboot \n4. Power Cycle Droplet \n5. Shutdown Droplet \n6. Power Off Droplet \n7. Power On Droplet \n8. Restore Droplet \n9. Password Reset \n10. Resize Droplet \n11. Rebuild Droplet \n12. Rename Droplet \n13. Enable IPv6 \n14. Enable Private \n15. SnapShot Droplet \n16. Retrieve a Droplet Action");
} else if (stage == 3) {
sort.states.UserState[sender].stage++;
if (text == '1') {
console.log("Stage 3")
sort.info[sender].actionType = 'eb';
sort.info[sender].actionStage = 1;
} else if (text == 2) {
sort.info[sender].actionType = 'db';
sort.info[sender].actionStage = 1;
} else if (text == 3) {
sort.info[sender].actionType = 'reb';
sort.info[sender].actionStage = 1;
} else if (text == 4) {
sort.info[sender].actionType = 'pcd';
sort.info[sender].actionStage = 1;
} else if (text == 5) {
sort.info[sender].actionType = 'shutdd';
sort.info[sender].actionStage = 1;
} else if (text == 6) {
sort.info[sender].actionType = 'poff';
sort.info[sender].actionStage = 1;
} else if (text == 7) {
sort.info[sender].actionType = 'pon';
sort.info[sender].actionStage = 1;
} else if (text == 8) {
sort.info[sender].actionType = 'resd';
sort.info[sender].actionStage = 1;
} else if (text == 9) {
sort.info[sender].actionType = 'pr';
sort.info[sender].actionStage = 1;
} else if (text == 10) {
sort.info[sender].actionType = 'red';
sort.info[sender].actionStage = 1;
} else if (text == 11) {
sort.info[sender].actionType = 'rbd';
sort.info[sender].actionStage = 1;
} else if (text == 12) {
sort.info[sender].actionType = 'rd';
sort.info[sender].actionStage = 1;
} else if (text == 13) {
sort.info[sender].actionType = 'eip6';
sort.info[sender].actionStage = 1;
} else if (text == 14) {
sort.info[sender].actionType = 'epn';
sort.info[sender].actionStage = 1;
} else if (text == 15) {
sort.info[sender].actionType = 'sd';
sort.info[sender].actionStage = 1;
} else if (text == 16) {
sort.info[sender].actionType = 'da';
sort.info[sender].actionStage = 1;
}
module.exports.sendTextMessage(sender, "Press Y for Confirm");
console.log(sort.info[sender].actionType);
} else if (stage == 4) {
console.log("Stage 4")
var action = sort.info[sender].actionType;
var aStage = sort.info[sender].actionStage;
//Enable Backup
if (action == 'eb') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "enable_backups" };
// sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to continue\n" + JSON.stringify(sort.data[sender]));
}
//Disable Backup
else if (action == 'db') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "disable_backups" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
//Reboot Droplet
else if (action == 'reb') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "reboot" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Power cycle of a droplet
else if (action == 'pcd') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "power_cycle" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Shut Down a droplet
else if (action == 'shutdd') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "shutdown" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Power off a droplet
else if (action == 'poff') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "power_off" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Power On a droplet
else if (action == 'pon') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "power_on" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
//TODO Restore a Droplet
else if (action == 'resd') {
//TODO backup image retrieve for restore
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "restore" };
//TODO
// sort.data[sender].image = imageID;
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
//Password reset a droplet
else if (action == 'pr') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "password_reset" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
//RESIZE a droplet
else if (action == 'red') {
// sort.states.UserState[sender].stage++;
var size = [];
if (aStage == 1) {
sort.data[sender] = { type: "resize" };
sort.info[sender].actionStage++;
console.log(sort.info[sender].actionStage)
module.exports.sendTextMessage(sender, "Do you also want to resize droplet?");
} else if (aStage == 2) {
console.log(sort.data[sender])
if (text.toLowerCase() == 'yes') {
sort.data[sender].disk = true;
} else {
sort.data[sender].disk = false;
}
// console.log("working")
dof.listSizes(digitoken, function(body) {
console.log(body)
for (var i = 0; i < body.sizes.length; i++) {
// console.log(body.sizes[0].slug)
size.push(body.sizes[i].slug);
}
sort.info[sender].actionStage++;
module.exports.sendTextMessage(sender, "Select a size from following sizes: \n" + size);
});
} else if (aStage == 3) {
sort.data[sender].size = text;
module.exports.sendTextMessage(sender, "Press any key to continue or exit to abort\n" + JSON.stringify(sort.data[sender]));
sort.states.UserState[sender].stage++;
}
}
//Rebuild a droplet
else if (action == 'rbd') {
if (aStage == 1) {
sort.data[sender] = { type: 'rebuild' };
sort.info[sender].actionStage++;
module.exports.sendTextMessage(sender, "Enter Image name: ");
} else if (aStage == 2) {
sort.data[sender].image = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to conitnue or exit to abort\n" + JSON.stringify(sort.data[sender]))
}
}
// Rename a droplet
else if (action == 'rd') {
if (aStage == 1) {
sort.data[sender] = { type: 'rename' };
sort.info[sender].actionStage++;
module.exports.sendTextMessage(sender, "Enter new name");
} else if (aStage == 2) {
sort.data[sender].name = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to conitnue or exit to abort\n" + JSON.stringify(sort.data[sender]))
}
}
// Enable ipv6
else if (action == 'eip6') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "enable_ipv6" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Enable Private Networking
else if (action == 'epn') {
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "enable_private_networking" };
module.exports.sendTextMessage(sender, "Press any Key to Continue \n" + JSON.stringify(sort.data[sender]))
}
// Take a snapshot of a droplet
else if (action == 'sd') {
if (aStage == 1) {
sort.data[sender] = { type: 'snapshot' };
sort.info[sender].actionStage++;
module.exports.sendTextMessage(sender, "Name of your snapshot");
} else if (aStage == 2) {
sort.data[sender].name = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Press any key to conitnue or exit to abort\n" + JSON.stringify(sort.data[sender]))
}
}
else if (action == 'da') {
if (aStage == 1) {
sort.info[sender].actionStage++;
module.exports.sendTextMessage(sender, "Enter Action Id");
} else if (aStage == 2) {
dof.dropletActionR(digitoken, sort.info[sender].id, text, function(body) {
module.exports.sendTextMessage(sender, "Action id: " + body.action.id + "\nStatus: " + body.action.status + "\nType: " + body.action.type + "\nStarted At: " + body.action.started_at + "\nCompleted At: " + body.action.completed_at);
module.exports.empty(sender)
})
}
}
} else if (stage == 5) {
console.log("Stage 5: " + sort.info[sender].id + " data" + sort.data[sender])
dof.dropletActions(digitoken, sort.info[sender].id, sort.data[sender], function(body) {
console.log(sort.info[sender].id)
console.log(body);
if (body.action == undefined) {
module.exports.sendTextMessage(sender, body.message);
} else {
module.exports.sendTextMessage(sender, "Action id: " + body.action.id + "\nAction Status: " + body.action.status);
}
})
module.exports.empty(sender);
}
}
//List Snapshot module
else if (mod == 'lSnapshots') {
if (stage == 1) {
dof.listSnapshots(digitoken, function(body) {
console.log(body.snapshots.length)
for (var i = 0; i < body.snapshots.length; i++) {
module.exports.sendTextMessage(sender, "Id: " + body.snapshots[i].id + "\nName: " + body.snapshots[i].name + "\nReigions: " + body.snapshots[i].regions[0] + "\nResource Type: " + body.snapshots[i].resource_type + "\nSize: " + body.snapshots[i].size_gigabytes + "gb")
}
module.exports.empty(sender)
})
}
}
//Delete Snapshot module
else if (mod == 'dSnapshot') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter the snapshot Id");
} else if (stage == 2) {
dof.deleteSnapshot(digitoken, text, function(body) {
// console.log(body)
if (body.id) {
module.exports.sendTextMessage(sender, "id: " + body.id + "\nMessage: " + body.message)
} else {
//TODO check if this is working
module.exports.sendTextMessage(sender, "Snapshot Deleted Successfully")
}
module.exports.empty(sender)
})
}
}
// List Image
else if (mod == 'lImage') {
if (text.toLowerCase() == 'next') {
console.log("next chal rha")
sort.info[sender].count = sort.info[sender].count + 10;
}
if (stage == 1) {
sort.states.UserState[sender].stage++;
dof.listAllImage(digitoken, function(body) {
console.log(body)
for (var i = 0; i < sort.info[sender].count; i++) {
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
})
} else if (stage == 2) {
dof.listAllImage(digitoken, function(body) {
if (body.images.length < sort.info[sender].count) {
sort.info[sender].count = body.images.length
}
//TODO fix this
if (sort.info[sender].count == 86) {
module.exports.sendTextMessage(sender, "All images are finished")
} else {
for (var i = (sort.info[sender].count - 10); i < sort.info[sender].count; i++) {
console.log(i)
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
}
})
}
}
// List Distribution Images
else if (mod == 'lDImage') {
if (text.toLowerCase() == 'next') {
console.log("next chal rha")
sort.info[sender].count = sort.info[sender].count + 10;
}
if (stage == 1) {
sort.states.UserState[sender].stage++;
dof.listDistImage(digitoken, function(body) {
console.log(body)
for (var i = 0; i < sort.info[sender].count; i++) {
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
})
} else if (stage == 2) {
dof.listDistImage(digitoken, function(body) {
if (body.images.length < sort.info[sender].count) {
sort.info[sender].count = body.images.length
}
//TODO fix this
if (sort.info[sender].count == 86) {
module.exports.sendTextMessage(sender, "All images are finished")
} else {
for (var i = (sort.info[sender].count - 10); i < sort.info[sender].count; i++) {
console.log(i)
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
}
})
}
}
// List Application Images
else if (mod == 'lAImage') {
if (text.toLowerCase() == 'next') {
console.log("next chal rha")
sort.info[sender].count = sort.info[sender].count + 10;
}
if (stage == 1) {
sort.states.UserState[sender].stage++;
dof.listAppImage(digitoken, function(body) {
console.log(body)
for (var i = 0; i < sort.info[sender].count; i++) {
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
})
} else if (stage == 2) {
dof.listAppImage(digitoken, function(body) {
if (body.images.length < sort.info[sender].count) {
sort.info[sender].count = body.images.length
}
//TODO fix this
if (sort.info[sender].count == 86) {
module.exports.sendTextMessage(sender, "All images are finished")
} else {
for (var i = (sort.info[sender].count - 10); i < sort.info[sender].count; i++) {
console.log(i)
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb")
}
}
})
}
}
// List User Images
else if (mod == 'lUImage') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
dof.listUserImage(digitoken, function(body) {
console.log(body)
if (body.images.length == 0) {
module.exports.sendTextMessage(sender, "No user images");
} else {
for (var i = 0; i < body.images.length; i++) {
module.exports.sendTextMessage(sender, "Id: " + body.images[i].id + "\nName: " + body.images[i].name + "\nDistribution: " + body.images[i].distribution + "\nSlug: " + body.images[i].slug + "\nType: " + body.images[i].type + "\nSize " + body.images[i].size_gigabytes + "gb\nRegions: "+JSON.stringify(body.images[i].regions))
}
}
})
}
}
// Delete image
else if (mod == 'dImage') {
if (stage == 1) {
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Enter image ID");
} else if (stage == 2) {
dof.deleteImage(digitoken, text, function(body) {
console.log(body)
if (body.id) {
module.exports.sendTextMessage(sender, "id: " + body.id + "\nMessage: " + body.message)
} else {
//TODO check if this is working
module.exports.sendTextMessage(sender, "Domain Deleted Successfully")
}
module.exports.empty(sender);
})
}
}
//Action for User image
else if (mod == 'aImage') {
if(stage == 1){
sort.states.UserState[sender].stage++;
callback("Enter image ID");
}
else if (stage == 2) {
sort.info[sender].imageID = text;
sort.states.UserState[sender].stage++;
module.exports.sendTextMessage(sender, "Select an Action for your image.\n1. Transfer an Image\n2. Convert image to a snapshot\n3. Retrieve an existing Image action")
} else if (stage == 3) {
sort.states.UserState[sender].stage++;
if (text == 1) {
sort.info[sender].actionType = 'tI'
sort.info[sender].actionStage = 1;
} else if (text == 2) {
sort.info[sender].actionType = 'cI';
sort.info[sender].actionStage = 1;
} else if (text == 3) {
console.log("3 typed")
sort.info[sender].actionType = 'rI';
sort.info[sender].actionStage = 1;
}
module.exports.sendTextMessage(sender, "Press any key to continue")
} else if (stage == 4) {
console.log(sort.info[sender].actionType)
// Transfer Image
if (sort.info[sender].actionType == 'tI') {
if (sort.info[sender].actionStage == 1) {
sort.data[sender] = { type: "transfer" };
sort.info[sender].actionStage++;
var regions = [];
dof.listRegions(digitoken, function(body) {
for (var i = 0; i < body.regions.length; i++) {
regions.push(body.regions[i].slug);
}
console.log(regions);
callback("Enter region from following regions: \n" + regions);
});
// module.exports.sendTextMessage(sender, "Enter the region for your transfer");
}
else if (sort.info[sender].actionStage == 2) {
sort.states.UserState[sender].stage++;
sort.data[sender].region = text;
callback("Press any key to continue or exit to abort\n"+JSON.stringify(sort.data[sender]))
}
}
// Convert Image to snapshot
else if(sort.info[sender].actionType == 'cI'){
console.log("working")
if(sort.info[sender].actionStage == 1){
sort.states.UserState[sender].stage++;
sort.data[sender] = { type: "convert"};
callback("Press any key to continue or exit to abort\n"+JSON.stringify(sort.data[sender]))
}
}
// Retrieve Image action
else if(sort.info[sender].actionType == 'rI'){
console.log("working")
if(sort.info[sender].actionStage == 1){
sort.info[sender].actionStage++;
callback("Enter action ID")
}
else if(sort.info[sender].actionStage == 2){
sort.info[sender].actionID = text;
dof.imageActionR(digitoken, sort.info[sender].imageID, sort.info[sender].actionID, function(body){
//TODO messsage to user
console.log(body);
callback("Action Id: "+body.action.id+"\nStatus: "+body.action.status+"\nType: "+body.action.type+"\nResource Type: "+body.action.resource_type)
module.exports.empty(sender)
})
}
}
}
else if(stage == 5){
dof.actionImage(digitoken, sort.info[sender].imageID, sort.data[sender], function(body){
console.log(body)
if(body.message){
callback("ID: "+body.id+"\nMessage: "+body.message)
module.exports.empty(sender)
}
else{
callback("Action ID: "+body.action.id+"\nStatus: "+body.action.status+"\nStarted At: "+body.action.started_at+"\nResource Type: "+body.action.resource_type)
module.exports.empty(sender)
}
})
}
}
//LIST BLOCK STORAGE
else if(mod == 'lBlock'){
if(stage == 1){
dof.listBlockS(digitoken, function(body){
console.log(body);
for(var i = 0; i<body.volumes.length; i++){
callback("ID: "+body.volumes[i].id+"\nName: "+body.volumes[i].name+"\nRegion: "+JSON.stringify(body.volumes[i].region)+"\nDroplet Ids Associated: "+JSON.stringify(body.volumes[i].droplet_ids)+"\nSize: "+body.volumes[i].size_gigabytes+"gb\nCreate At: "+body.volumes[i].created_at)
}
})
}
}
//CREATE BLOCK STORAGE
else if(mod == 'cBlock'){
if(stage == 1){
sort.states.UserState[sender].stage++;
callback("Enter the size in Gigabytes");
}
else if(stage == 2){
sort.states.UserState[sender].stage++;
sort.data[sender] = { size_gigabytes: text };
callback("Enter the name")
}
else if(stage == 3){
sort.states.UserState[sender].stage++;
sort.data[sender].name = text;
callback("Enter a description for storage or skip")
}
else if(stage == 4){
sort.states.UserState[sender].stage++;
if(text.toLowerCase() != 'skip'){
sort.data[sender].description = text;
}
var regions = [];
dof.listRegions(digitoken, function(body) {
for (var i = 0; i < body.regions.length; i++) {
console.log(body.regions[i].slug)
regions.push(body.regions[i].slug);
}
callback("Enter a region:\n"+regions)
});
}
else if(stage == 5){
sort.states.UserState[sender].stage++
sort.data[sender].region = text;
callback("Press any key to continue or exit "+JSON.stringify(sort.data[sender]))
}
else if(stage == 6){
dof.createStorage(digitoken, sort.data[sender], function(body){
console.log(body)
if(body.id){
callback("Id: "+body.id+"\nMessage: "+body.message)
}
else{