-
Notifications
You must be signed in to change notification settings - Fork 15
/
twilio.gs
1684 lines (1594 loc) · 82.2 KB
/
twilio.gs
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
//define some useful globals
var https = "https://"
var appName = "api.twilio.com/";
var twilioVersion = "2010-04-01/";
var accounts = "Accounts/"
var webName = appName + twilioVersion + accounts;
var googleLangList = ['Afrikaans: af','Albanian: sq','Arabic: ar','Azerbaijani: az','Basque: eu','Bengali: bn','Belarusian: be','Bulgarian: bg','Catalan: ca','Chinese Simplified: zh-CN','Chinese Traditional: zh-TW','Croatian: hr','Czech: cs','Danish: da','Dutch: nl','English: en','Esperanto: eo','Estonian: et','Filipino: tl','Finnish: fi','French: fr','Galician: gl','Georgian: ka','German: de','Greek: el','Gujarati: gu','Haitian Creole: ht','Hebrew: iw','Hindi: hi','Hungarian: hu','Icelandic: is','Indonesian: id','Irish: ga','Italian: it','Japanese: ja','Kannada: kn','Korean: ko','Latin: la','Latvian: lv','Lithuanian: lt','Macedonian: mk','Malay: ms','Maltese: mt','Norwegian: no','Persian: fa','Polish: pl','Portuguese: pt','Romanian: ro','Russian: ru','Serbian: sr','Slovak: sk','Slovenian: sl','Spanish: es','Swahili: sw','Swedish: sv','Tamil: ta','Telugu: te','Thai: th','Turkish: tr','Ukrainian: uk','Urdu: ur','Vietnamese: vi','Welsh: cy','Yiddish: yi'];
var langList = ['en','en-gb','es','fr','de'];
//function used during installation to verify that the user has in fact made their webapp usable by anonymous visitors (i.e. Twilio)
function formMule_verifyPublic() {
var url = ScriptApp.getService().getUrl();
var test = "FALSE";
if (!url) {
test = "Error: You have not published your script as a web app. Please revisit the SMS and Voice instructions.";
return test;
}
url += "?Verify=TRUE"
var content = UrlFetchApp.fetch(url).getContentText();
try {
test = Xml.parse(content);
test = test.verified.getText();
return test;
} catch(err) {
test = 'Error: You have published your script as a web app, but you have not made it available to "Anyone, even anonymous." Please revisit the SMS and Voice instructions.';
return test;
}
}
// Ui providing step by step instructions for how to publish the script as a webApp
function formMule_howToPublishAsWebApp() {
var app = UiApp.createApplication().setTitle('Step 2c. Set up SMS and Voice - Publish formMule as a web app').setHeight(540).setWidth(600);
var thisSs = SpreadsheetApp.getActiveSpreadsheet();
ScriptProperties.setProperty('ssId', thisSs.getId());
var public = formMule_verifyPublic();
// Once it is determined that the webapp is public, proceed to Twilio setup instructions
if (public=="TRUE") {
app.close();
formMule_howToSetUpTwilio();
return app;
}
var panel = app.createVerticalPanel();
var handler = app.createServerHandler('formMule_howToSetUpTwilio').addCallbackElement(panel);
var button = app.createButton("Confirm my settings").addClickHandler(handler);
var scrollpanel = app.createScrollPanel().setHeight("360px");
var grid = app.createGrid(9, 2).setBorderWidth(0).setCellSpacing(0);
var html = app.createHTML('<strong>Important to understand:</strong> Using formMule with the Twilio service is OPTIONAL, but requires publishing this script as a web app, which will provide a URL that Twilio will use to communicate with the script. The instructions below explain how to publish your form as a web app.');
panel.add(html);
var text1 = app.createLabel("Instructions:").setStyleAttribute("width", "100%").setStyleAttribute("backgroundColor", "grey").setStyleAttribute("color", "white").setStyleAttribute("padding", "5px 5px 5px 5px");
panel.add(text1);
grid.setWidget(0, 0, app.createHTML('1. Go to \'Tools->Script editor\' from the Spreadsheet that contains your form.</li>'));
grid.setWidget(0, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/tools%20menu.png?attachauth=ANoY7crn7wMOEG9WpO7uZVHw6b6t5H2WtOs3S_lZ7qV8i2uQsizc1DtFE0HUGG3zXDbqa65wbz3YVJ_9m9zGr-wujYf0jRgz-U45s9Sids1HmI02fPwWNCjkp1GDmzViBwcuL-MwlPNu_WOz4J282Sv1EdVOFQ9l2dKrhPwwOPko1QcWqJ8-v2hS1jOxJaM6VC7EbybBeeLJ1Fy7Z9o56lg1fGcqc28WRklYD3pb_zSh_q_WLfwnl-l0lVQ8HjiWzGNyE5kn4J0Y&attredirects=0').setWidth("430px"));
grid.setStyleAttribute(1, 0, "backgroundColor", "grey").setStyleAttribute(1, 1, "backgroundColor", "grey");
grid.setWidget(2, 0, app.createHTML('2. Under the \'File\' menu in the Script Editor, select \'Manage versions\' and save a new version of the script. Because it\'s optional, you can leave the \'Describe what changed\' field blank.</li>'));
grid.setWidget(2, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/file%20menu.png?attachauth=ANoY7cqYPycZqgCH7TseuZRL9wq2lv7xI2bhXhDuQ1BVMp5VkB83wpbQRz3X2q36ouPGZyds-CPzEdX5_o-RfDJfVruf7TUxZddxm9sQQwlY6sUab9nVn8KbDgiqiED0itUzfWKvMfP86ya0twRDnONOtGQmq1XWw26vFLQ82DeaKLASttZ6Mo_Qg-FBloN0iVsMlmLLd5o40C4RbaK7qkXUp5eoEE3eYPf_dpx7u3gBb6gyjIZ8yCSKbajC-HfG32MeS0UtrxTW&attredirects=0').setWidth("430px"));
grid.setWidget(3, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/manage%20versions.png?attachauth=ANoY7cpVfU-WEwiPuOMKMIAzbK6EdA8xkmv_M2R8GKdlcGLC7mo00ZJykbBFrtJEZQHDpKVdvizQQnuyfGVc65iigmGuGr_ZwC2Z4rnh1V67_ogOJKXH2TWmDAafxa-q_5fngrasDYYN2w2-hR_eR95GoY6e5Rza-mtWb1iAp97Cm8n9kVHRk67dURdrdD5AIaS8ZOkse1MmfaN-ZJpMv7bLYBKpisq8GldTTjo7W55OUIJhFuDcxLEc__vguXArjfb9Pd_e2bZD&attredirects=0').setWidth("430px"));
grid.setStyleAttribute(4, 0, "backgroundColor", "grey").setStyleAttribute(4, 1, "backgroundColor", "grey");
grid.setWidget(5, 0, app.createHTML('3. Under the \'Publish\' menu in the Script Editor, select \'Deploy as web app\'. Choose the version you want to publish (usually #1). Under \'Execute web app as\', choose \'me\'. For twilio to access the script, the URL must be visible to anonymous users, so select \'Anyone, even anonymous\'. In the context of this script, this setting can reveal no data to anonymous users.'));
grid.setWidget(5, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/publish.png?attachauth=ANoY7crNAwLK0jZOIWN9M0oD2EzLC5pDflZJNnG9pZQcUD4liYn3lBFe_5yr9Hlnc5UgU837A28Mow5hsWQuIJh-ihgu_s_wtDqG51m697X7aFrKPBJoE8WQZRhzgPhLKEh_gYi4Cs9KXJb4Ie-suQb65WbOCvxC4fyStExxZrAcYYcws0U5MJqoStfNqLbvb5iBgys8G6IMSce3tNo-cad8UKVW18aB7J0BLPeTO7spj0u-cJVFXVFFPq22pKwCViQSciRyNYaF&attredirects=0').setWidth("430px"));
grid.setWidget(6, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/webapp.png?attachauth=ANoY7co9bP7Sb4cK3aN9Vf3l57RFiWpQ8fDG6_XbNkx8hG7offLTHQAPXm6JMgHjgpEsruUr5mVnf6ha-pc6phil_1MFWsPs7nD2eMeKV_hylLf0qwXLKS3psU4Mu36iw1xvTG8lA6yUJLHb8jA-yRW1jol3bhBD9LKxTzjavHqjtMffZXohjXIV_d_diuNzK6DYBySmuDzeXIp4LyEabrwPglXk5rN6LOXcJ9vPTunUe5cSLhKlLLW2Vj17A3qwmWXxf94gx-yN&attredirects=0').setWidth("430px"));
grid.setWidget(7, 1, app.createImage('https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/images-for-formmutant/webapp2.png?attachauth=ANoY7crqHkuxcVazt1Bi1_aJwGchj2iACUhPxWHTCa4c8Qrh_jJSgbQQA1CajpsNvIIllJtUIH3yQxCxH-ZiN0wbk_L03Ow7dJorxbqVU5LUjV1ic7gowIbdKhTqS3b3-Sg81PEppEzUlfA8slzI9goYt1KxAXy86RjNHIKUDV1Zo3nORWV7_18GJ5DMpiNB84JV8yHer041WB18JmXYZzpSSJwN3ej00GWo9hknfayVCNy9EhM-QAH8q7pUYJyzwdIRAcW2cayC&attredirects=0').setWidth("430px"));
scrollpanel.add(grid);
panel.add(scrollpanel);
panel.add(button);
app.add(panel);
thisSs.show(app);
return app;
}
// Ui showing step by step instructions for how to set up Twilio
function formMule_howToSetUpTwilio() {
var url = ScriptApp.getService().getUrl();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle("Step 2c. Set up SMS and Voice - Get formMule and Twilio talking").setWidth(600).setHeight(500);
var public = formMule_verifyPublic();
if (public!="TRUE") {
app.close();
Browser.msgBox(public);
formMule_howToPublishAsWebApp();
return app;
}
var scrollPanel = app.createScrollPanel().setId("scrollPanel").setWidth("595px").setHeight("410px");
var panel = app.createVerticalPanel().setId("panel");
var htmlBody = "Congrats on getting formMule published correctly as a web app. Now on to configuring Twilio, the 3rd party, paid service that can enable SMS and Voice messages to be sent from this script with a few simple setup steps. Twilio's pricing can be found at http://www.twilio.com/voice/pricing<br>";
var html = app.createHTML(htmlBody);
app.add(html);
var linkLabel = app.createLabel("This script's URL for Twilio SMS and Voice Requests:").setStyleAttribute("margin", "10px 0px 10px 0px").setStyleAttribute("padding", "5px 5px 5px 5px");
var link = app.createTextBox()
if (!url) {
Browser.msgBox("Error: You have not yet published this script as a web app. Please launch Step 2c. again and complete the steps outlined in the instructions.");
app.close();
return app;
} else {
ScriptProperties.setProperty('webAppUrl', url);
}
link.setText(url).setEnabled(false).setWidth("400px").setStyleAttribute("margin", "10px 10px 10px 10px").setStyleAttribute("padding", "5px 5px 5px 5px");
var text1 = app.createLabel("Instructions:").setStyleAttribute("width", "100%").setStyleAttribute("backgroundColor", "grey").setStyleAttribute("color", "white").setStyleAttribute("padding", "5px 5px 5px 5px");
app.add(text1);
var text2 = app.createLabel("1. Create an account at http://www.twilio.com");
var image2 = app.createImage("https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/searchable-docs-collection/2.jpg?attachauth=ANoY7comDBRVlYUnW6rFi__cI_prSJtORGUDtTjJcGG9QpEI7kNh2MW-u8sxwuB8Ndvs9Nv2HbKQeN2B83DC3YUMjQW-fVd95aMTI7tChp_lxGwAszP5y4ylN5A4rf2_GQhN5nVxv9NdCVf5SfPN1hQiTIS1lNvjRlzjB5voEhbBXq9JLK_apapm6ZHn6qFWfd1vweN6TCtZPiORGJpz235vvxPDxliNZCOvjrzCjf_i-qATTn0TSq4iYy-hHbuUQO2Wpo8zNgtg&attredirects=0").setWidth("430px");
var text3 = app.createLabel("2. Verify yourself as a human (Twilio will call you and ask you to verify a code), get assigned a free phone number, and proceed to the \"Numbers\" section of your account. Click on your Twilio phone number.");
var image3 = app.createImage("https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/searchable-docs-collection/6.jpg?attachauth=ANoY7cpOcS5hG8YQwcs01G5LZYz417BhmzEa3lmQgkp-sFSS-xEdE5mu0kFkuuiQ0rwsE6H18OhnqfrXCODtG6FD0OHp8wXynSdaC6kgDb8TCH0OZD55QEgr7Uc3ltdWmj7RkP9BFaOsJ6cxtFMDtpzD6gvJ0CJTr6St7HvdAtTAvTV3XHi75HUlYLCl2drEaAKvDZr396m04Hv_Ohb2ueaH2FtNxaFBZI07SqsPu6pgQC63bIAiCTPEOBq88nCLL0xqCab_mLVZ&attredirects=0").setWidth("430px");
var numberLabel = app.createLabel("Enter your Twilio number here");
var numberBox = app.createTextBox().setName("twilioNumber");
var text4 = app.createLabel("3. Paste the URL of your published script (below) into the Voice and SMS Request URL fields. Change both URL methods to GET. Save Changes");
var image4 = app.createImage("https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/searchable-docs-collection/9.jpg?attachauth=ANoY7cp8HufXi1K_3FamZ8SbLFEd3_9DG0ToNsR7vYTvrIzHyKieNlJZn42NJamy9NKxyXdDLgRIl9hXPRx3oWxVRJSVVss3TrFVw2Q2HM28XmylowPDOFyYNLjDW1r-acFaoag74hxzKSLyUAQ6CUB5J_Zheof1Q6XLuE1JZf5dTTXYOtvPANMvrBWrRjOwJ2u9tUnNXAcJfb3BG11MClNWV4d6t1woiexK00zaU6l9bi3QgKDRFZEJXzNhYDnvvlP6qA_SaZQr&attredirects=0").setWidth("430px");
var fieldPanel = app.createGrid(1,4);
var accountLabel = app.createLabel("Account SID");
var accountLabelBox = app.createTextBox().setName('accountSID');
var accountSID = ScriptProperties.getProperty('accountSID');
if (accountSID) {
accountLabelBox.setText(accountSID);
}
var authTokenLabel = app.createLabel("Auth Token");
var authTokenBox = app.createTextBox().setName('authToken');
var authToken = ScriptProperties.getProperty('authToken');
if (authToken) {
authTokenBox.setText(authToken);
}
var text5 = app.createLabel("4. Paste and save your Account SID and Auth Token below");
fieldPanel.setWidget(0, 0, accountLabel).setWidget(0, 1, accountLabelBox).setWidget(0, 2, authTokenLabel).setWidget(0, 3, authTokenBox);
var image5 = app.createImage("https://c04a7a5e-a-3ab37ab8-s-sites.googlegroups.com/a/newvisions.org/data-dashboard/searchable-docs-collection/1.jpg?attachauth=ANoY7cqnvcVDrWwnN9p3T_tZ1fJ2eDb-AS4JszGViXjYExOVyaW8u6m4CdUGGw460rm7eYhgbKZWqwO4DM9ugkI7vTWX23ZCFVPBE7XiPyz8l6Oeie7To7GGOJKXJTo4Lbs6ufiLVST7iQpgW-0Q3uRKvozd3jUFfrRKO4TMozdZdEjQnLAF2XECiBR1gGBNcxfjJJs1sxAFJNeBuNf5u4jvtt9BYXCD7NGRBRAm2Tp2TiTUNctwbQUqHx8i6-AFYsvSmuM-jfiL&attredirects=0").setWidth("430px");
var saveHandler = app.createServerHandler('formMule_saveTwilioSettings').addCallbackElement(scrollPanel);
var button = app.createButton("Verify settings").addClickHandler(saveHandler).setId("testButton");
var grid = app.createGrid(14, 2).setBorderWidth(0).setCellSpacing(0).setId("grid");
grid.setWidget(0, 0, text2).setWidget(0, 1, image2);
grid.setStyleAttribute(1, 0, "backgroundColor", "grey").setStyleAttribute(1, 1, "backgroundColor", "grey");
grid.setWidget(2, 0, text3).setWidget(2, 1, image3);
grid.setStyleAttribute(3, 0, "backgroundColor", "grey").setStyleAttribute(3, 1, "backgroundColor", "grey");
grid.setWidget(4, 0, text4).setWidget(4, 1, image4);
grid.setWidget(5, 0, linkLabel).setWidget(5, 1, link);
grid.setStyleAttribute(6, 0, "backgroundColor", "grey").setStyleAttribute(6, 1, "backgroundColor", "grey");
grid.setWidget(7, 0, text5).setWidget(7, 1, image5);
grid.setWidget(8, 0, button).setWidget(8, 1, fieldPanel);
scrollPanel.add(grid);
app.add(scrollPanel);
ss.show(app);
}
//Saves Twilio settings
function formMule_saveTwilioSettings(e) {
var app = UiApp.getActiveApplication();
var testButton = app.getElementById('testButton');
var accountSID = e.parameter.accountSID;
var authToken = e.parameter.authToken;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
ScriptProperties.setProperty("ssId", ssId);
if ((accountSID!='')&&(authToken!='')) {
ScriptProperties.setProperty("accountSID", accountSID);
ScriptProperties.setProperty("authToken", authToken);
}
var grid = app.getElementById("grid");
var numberList = app.createListBox().setName("twilioNumber");
var phoneNumbers = formMule_getPhoneNumbers(accountSID,authToken);
var saveHandler = app.createServerHandler('formMule_saveTwilioNumber').addCallbackElement(grid);
var button = app.createButton("Save settings").setEnabled(false).addClickHandler(saveHandler);
for (var i = 0; i<phoneNumbers.length; i++) {
numberList.addItem(phoneNumbers[i]);
}
grid.setStyleAttribute(9, 0, "backgroundColor", "grey").setStyleAttribute(9, 1, "backgroundColor", "grey");
// If no phone numbers are returned by Twilio, warn the user
if ((!phoneNumbers)||(phoneNumbers.length==0)) {
button.setEnabled(false);
testButton.setEnabled(true);
grid.setWidget(10, 0, app.createLabel("There appears to be a problem with your settings. Please double check all steps and try again."))
} else {
button.setEnabled(true);
testButton.setEnabled(false);
grid.setWidget(10, 0, app.createLabel("Twilio phone number(s) successfully detected. If more than one, select which one you want this script to use."));
var twilioNumber = ScriptProperties.getProperty('twilioNumber');
if ((twilioNumber)&&(phoneNumbers.indexOf(twilioNumber)!=-1)) {
numberList.setSelectedIndex(phoneNumbers.indexOf(twilioNumber));
}
grid.setWidget(10, 1, numberList);
grid.setStyleAttribute(11, 0, "backgroundColor", "grey").setStyleAttribute(11, 1, "backgroundColor", "grey");
var langLabel = app.createLabel("Select default language (from those supported by Twilio) for voice messages");
var langSelectBox = app.createListBox().setName("defaultLang");
for (var i=0; i<langList.length; i++) {
langSelectBox.addItem(langList[i]);
}
var langSelected = ScriptProperties.getProperty('defaultVoiceLang');
if (langSelected) {
var index = langList.indexOf(langSelected);
if (index!=-1) {
langSelectBox.setSelectedIndex(index);
}
}
grid.setWidget(12, 0, langLabel);
grid.setWidget(12, 1, langSelectBox);
grid.setWidget(13, 0, button);
}
return app;
}
function formMule_saveTwilioNumber(e) {
var app = UiApp.getActiveApplication();
var twilioNumber = e.parameter.twilioNumber;
ScriptProperties.setProperty('twilioNumber', twilioNumber);
var defaultLang = e.parameter.defaultLang;
ScriptProperties.setProperty('defaultVoiceLang', defaultLang);
onOpen();
Browser.msgBox("Nice work! Now for a bit of magic: Try texting \n \"Woot woot\" to " + twilioNumber + " to see if the script is able to receive and send text messages");
app.close();
return app;
}
function formMule_smsAndVoiceSettings(tabIndex) {
var properties = ScriptProperties.getProperties();
if (!tabIndex) {
tabIndex = 0;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ScriptProperties.getProperty('sheetName');
var sheet = ss.getSheetByName(sheetName);
if (!sheet) {
sheet = ss.getSheets()[0];
sheetName = sheet.getName();
ScriptProperties.setProperty('sheetName', sheetName);
Browser.msgBox("No source sheet detected. Source was automatically set to the top sheet: " + sheetName);
}
try {
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
} catch(err) {
sheet.getRange(1,1,1,3).setValues([['Dummy Header 1', 'Dummy Header 2', 'Dummy Header 3']]);
sheet.setFrozenRows(1);
Browser.msgBox("Your source sheet must have headers to complete this step");
formMule_smsAndVoiceSettings();
return;
}
var app = UiApp.createApplication().setTitle("Step 2c: Set up SMS message and voice message merge").setWidth("640").setHeight("450");
var mainPanel = app.createVerticalPanel().setId("mainPanel").setWidth("640px").setHeight("400px");
var refreshPanel = app.createVerticalPanel().setId('refreshPanel').setVisible(false);
var spinner = app.createImage(MULEICONURL).setWidth(150);
spinner.setStyleAttribute("position", "absolute");
spinner.setStyleAttribute("top", "120px");
spinner.setStyleAttribute("left", "190px");
spinner.setId("dialogspinner");
refreshPanel.add(spinner);
var smsGrid = app.createGrid(1, 2);
var vmGrid = app.createGrid(1, 2);
var tabPanel = app.createTabPanel();
var smsPanel = app.createVerticalPanel().setWidth("430px").setHeight("360px");
var smsFeaturePanel = app.createVerticalPanel().setStyleAttribute("backgroundColor", "whiteSmoke").setWidth("100%").setHeight("100px");
var smsEnabledCheckBox = app.createCheckBox("Turn on SMS merge feature").setName("smsEnabled").setStyleAttribute("color", "purple");
if (properties.smsEnabled=="true") {
smsEnabledCheckBox.setValue(true);
}
var smsOnTriggerCheckBox = app.createCheckBox("Trigger this feature on form submit").setName("smsTrigger").setStyleAttribute("color", "purple");
if (properties.smsTrigger=="true") {
smsOnTriggerCheckBox.setValue(true);
}
var smsLengthPanel = app.createHorizontalPanel();
var smsLengthLabel = app.createLabel("Max SMS length").setStyleAttribute("padding", "5px");
var smsLengthSelect = app.createListBox().setName('smsMaxLength');
smsLengthSelect.addItem("160 char (1 msg per send)", '1');
smsLengthSelect.addItem("320 char (2 msgs per send)", '2');
smsLengthSelect.addItem("480 char (3 msgs per send)", '3');
if (properties.smsMaxLength) {
smsLengthSelect.setSelectedIndex(parseInt(properties.smsMaxLength)-1);
}
smsLengthPanel.add(smsLengthLabel);
smsLengthPanel.add(smsLengthSelect);
var smsNumSelectPanel = app.createHorizontalPanel();
var smsNumSelectHandler = app.createServerHandler('refreshSmsTemplateGrid').addCallbackElement(tabPanel);
var smsNumSelect = app.createListBox().setName('smsNumSelected');
smsNumSelect.addItem('1')
.addItem('2')
.addItem('3');
var smsNumSelected = 1;
if(properties.smsNumSelected) {
smsNumSelected = properties.smsNumSelected;
}
smsNumSelect.setSelectedIndex(smsNumSelected-1);
smsNumSelect.addChangeHandler(smsNumSelectHandler);
var smsNumLabel = app.createLabel('unique possible SMS message(s) per row').setStyleAttribute("padding", "5px");
smsNumSelectPanel.add(smsNumSelect).add(smsNumLabel);
smsFeaturePanel.setStyleAttribute("padding", "5px");
smsFeaturePanel.add(smsEnabledCheckBox);
smsFeaturePanel.add(smsOnTriggerCheckBox);
smsFeaturePanel.add(smsLengthPanel);
smsFeaturePanel.add(smsNumSelectPanel);
smsPanel.add(smsFeaturePanel)
var smsTemplatePanel = app.createVerticalPanel().setHeight("250px")
var smsTemplateScrollPanel = app.createScrollPanel().setHeight("250px");
var smsTemplateGrid = app.createGrid(smsNumSelected*4, 5).setId('smsTemplateGrid').setBorderWidth(0).setCellSpacing(0);
var smsPropertyString = properties.smsPropertyString;
var smsPropertyObject = new Object();
if ((smsPropertyString)&&(smsPropertyString!='')) {
smsPropertyObject = Utilities.jsonParse(smsPropertyString);
}
for (var i = 0; i<smsNumSelected; i++) {
smsTemplateGrid.setWidget(0+(i*4), 0, app.createLabel('Template Name'));
smsTemplateGrid.setWidget(1+(i*4), 0, app.createLabel('Phone #'));
smsTemplateGrid.setWidget(2+(i*4), 0, app.createLabel('Body'));
smsTemplateGrid.setStyleAttribute(3+(i*4), 0, 'backgroundColor','grey');
var smsTemplateNameBox = app.createTextBox().setName('smsName-'+i).setWidth("140px");
if (smsPropertyObject['smsName-'+i]) {
smsTemplateNameBox.setValue(smsPropertyObject['smsName-'+i]);
}
var smsPhoneBox = app.createTextBox().setName('smsPhone-'+i).setWidth("140px");
if (smsPropertyObject['smsPhone-'+i]) {
smsPhoneBox.setValue(smsPropertyObject['smsPhone-'+i]);
}
var smsBodyArea = app.createTextArea().setName('smsBody-'+i).setWidth("140px").setHeight("110px");
if (smsPropertyObject['smsBody-'+i]) {
smsBodyArea.setValue(smsPropertyObject['smsBody-'+i]);
}
smsTemplateGrid.setWidget(0+(i*4), 1, smsTemplateNameBox);
smsTemplateGrid.setWidget(1+(i*4), 1, smsPhoneBox);
smsTemplateGrid.setWidget(2+(i*4), 1, smsBodyArea);
smsTemplateGrid.setStyleAttribute(3+(i*4), 1, 'backgroundColor','grey');
smsTemplateGrid.setWidget(0+(i*4), 2, app.createLabel('send if'));
var smsCondCol = app.createListBox().setName('smsCol-'+i).setWidth("90px");
for (var j=0; j<headers.length; j++) {
smsCondCol.addItem(headers[j]);
}
if (smsPropertyObject['smsCol-'+i]) {
var index = headers.indexOf(smsPropertyObject['smsCol-'+i]);
smsCondCol.setSelectedIndex(index);
}
smsTemplateGrid.setWidget(1+(i*4), 2, smsCondCol);
smsTemplateGrid.setWidget(2+(i*4), 2, app.createLabel('Language code (merge tags accepted)').setStyleAttribute('textAlign', 'right'));
smsTemplateGrid.setStyleAttribute(3+(i*4), 2, 'backgroundColor','grey');
smsTemplateGrid.setWidget(1+(i*4), 3, app.createLabel("="));
smsTemplateGrid.setStyleAttribute(3+(i*4), 3, 'backgroundColor','grey');
var smsCondVal = app.createTextBox().setName('smsVal-'+i).setWidth("100px");
if (smsPropertyObject['smsVal-'+i]) {
smsCondVal.setValue(smsPropertyObject['smsVal-'+i]);
}
var smsLang = app.createTextBox().setName('smsLang-'+i).setWidth("50px");
if (smsPropertyObject['smsLang-'+i]) {
smsLang.setValue(smsPropertyObject['smsLang-'+i]);
} else {
smsLang.setValue('en');
}
smsTemplateGrid.setWidget(1+(i*4), 4, smsCondVal);
smsTemplateGrid.setWidget(2+(i*4), 4, smsLang);
smsTemplateGrid.setStyleAttribute(3+(i*4), 4, 'backgroundColor','grey');
}
smsTemplateScrollPanel.add(smsTemplateGrid);
smsTemplatePanel.add(app.createLabel("SMS Template(s)").setStyleAttribute("color", "white").setStyleAttribute("backgroundColor", "grey").setStyleAttribute("width", "100%").setStyleAttribute('padding', '5px'));
smsTemplatePanel.add(smsTemplateScrollPanel);
smsPanel.add(smsTemplatePanel);
smsGrid.setWidget(0, 0, smsPanel);
var vmPanel = app.createVerticalPanel().setWidth("430px").setHeight("350px");
var vmFeaturePanel = app.createVerticalPanel().setStyleAttribute("backgroundColor", "whiteSmoke").setWidth("100%").setHeight("80px").setStyleAttribute("padding", "5px");
var vmEnabledCheckBox = app.createCheckBox("Turn on Voice Message merge feature").setName('vmEnabled').setStyleAttribute("color", "#FF6600");
if (properties.vmEnabled=="true") {
vmEnabledCheckBox.setValue(true);
}
var vmOnTriggerCheckBox = app.createCheckBox("Trigger this feature on form submit").setName('vmTrigger').setStyleAttribute("color", "#FF6600");
if (properties.vmTrigger=="true") {
vmOnTriggerCheckBox.setValue(true);
}
var vmNumSelectPanel = app.createHorizontalPanel();
var vmNumSelectHandler = app.createServerHandler('refreshVmTemplateGrid').addCallbackElement(tabPanel);
var vmNumSelect = app.createListBox().setName('vmNumSelected');
vmNumSelect.addItem('1')
.addItem('2')
.addItem('3');
var vmNumSelected = 1;
if(properties.vmNumSelected) {
vmNumSelected = properties.vmNumSelected;
}
vmNumSelect.setSelectedIndex(vmNumSelected-1);
vmNumSelect.addChangeHandler(vmNumSelectHandler);
var vmNumLabel = app.createLabel('unique possible Voice Message(s) per row').setStyleAttribute("padding", "5px");
vmNumSelectPanel.add(vmNumSelect).add(vmNumLabel);
vmFeaturePanel.add(vmEnabledCheckBox);
vmFeaturePanel.add(vmOnTriggerCheckBox);
vmFeaturePanel.add(vmNumSelectPanel);
vmPanel.add(vmFeaturePanel);
var vmTemplatePanel = app.createVerticalPanel().setHeight("270px")
var vmTemplateScrollPanel = app.createScrollPanel().setHeight("270px");
var vmTemplateGrid = app.createGrid(vmNumSelected*6, 5).setId("vmTemplateGrid").setBorderWidth(0).setCellSpacing(0);
var recordingSheet = ss.getSheetByName("MyVoiceRecordings");
if (!recordingSheet) {
recordingSheet = ss.insertSheet("MyVoiceRecordings");
var recordingHeaders = [['Recording Name','Recording URL']];
recordingSheet.getRange(1, 1, 1, 2).setValues(recordingHeaders).setComment("Don't change the name of this column or sheet. formMule needs these to run SMS and Voice merge services.");
recordingSheet.setColumnWidth(2, 800);
recordingSheet.setFrozenRows(1);
Browser.msgBox("formMule just automatically created a sheet to store the URLs of your outbound voice messages");
formMule_smsAndVoiceSettings();
app.close();
return app;
}
if (recordingSheet.getLastRow()-1>0) {
var recordingSelectTexts = recordingSheet.getRange(2, 1, recordingSheet.getLastRow()-1, 1).getValues();
var recordingSelectValues = recordingSheet.getRange(2, 2, recordingSheet.getLastRow()-1, 1).getValues();
} else {
var recordingSelectTexts = [];
var recordingSelectValues = [];
}
var vmPropertyString = properties.vmPropertyString;
var vmPropertyObject = new Object();
if ((vmPropertyString)&&(vmPropertyString!='')) {
vmPropertyObject = Utilities.jsonParse(vmPropertyString);
}
for (var i = 0; i<vmNumSelected; i++) {
vmTemplateGrid.setWidget(0+(i*6), 0, app.createLabel('Template Name'));
vmTemplateGrid.setWidget(1+(i*6), 0, app.createLabel('Phone #'));
vmTemplateGrid.setWidget(2+(i*6), 0, app.createLabel('Read Message (leave blank for none)'));
vmTemplateGrid.setWidget(3+(i*6), 0, app.createLabel('Play recording'))
vmTemplateGrid.setWidget(4+(i*6), 0, app.createLabel('Record response?'));
vmTemplateGrid.setStyleAttribute(5+(i*6), 0, 'backgroundColor','grey');
var vmTemplateNameBox = app.createTextBox().setName('vmName-'+i).setWidth("140px");
if (vmPropertyObject['vmName-'+i]) {
vmTemplateNameBox.setValue(vmPropertyObject['vmName-'+i]);
}
var vmPhoneBox = app.createTextBox().setName('vmPhone-'+i).setWidth("140px");
if (vmPropertyObject['vmPhone-'+i]) {
vmPhoneBox.setValue(vmPropertyObject['vmPhone-'+i]);
}
var vmBodyArea = app.createTextArea().setName('vmBody-'+i).setWidth("140px").setHeight("90px");
if (vmPropertyObject['vmBody-'+i]) {
vmBodyArea.setValue(vmPropertyObject['vmBody-'+i]);
}
var vmSoundFileListBox = app.createListBox().setName('vmSoundFile-'+i).setWidth("140px");
var soundFileArray = [];
vmSoundFileListBox.addItem("None", "");
var m = 0;
for (var k=0; k<recordingSelectTexts.length; k++) {
vmSoundFileListBox.addItem(recordingSelectTexts[k][0], recordingSelectValues[k][0]);
soundFileArray.push(recordingSelectValues[k][0]);
}
if (vmPropertyObject['vmSoundFile-'+i]) {
var index = soundFileArray.indexOf(vmPropertyObject['vmSoundFile-'+i]);
if (index!=-1) {
vmSoundFileListBox.setSelectedIndex(index+1);
m = index;
}
}
var vmSoundFileLinkPanel = app.createHorizontalPanel();
var vmSoundFilePlayLinkRefreshHandler = app.createServerHandler('refreshVmSoundFilePlayLink').addCallbackElement(vmSoundFileListBox).addCallbackElement(vmSoundFileLinkPanel);
vmSoundFileListBox.addChangeHandler(vmSoundFilePlayLinkRefreshHandler);
var linkText = "";
if ((vmPropertyObject['vmSoundFile-'+i]!="")&&(recordingSelectValues.length>0)) {
linkText = "Play";
var vmSoundFilePlayLink = app.createAnchor(linkText,recordingSelectValues[m][0]).setId('vmSoundFileLink-'+i);
} else {
var vmSoundFilePlayLink = app.createAnchor(linkText,'').setId('vmSoundFileLink-'+i);
}
var vmSoundFilePlayLinkClientHandler = app.createClientHandler().forTargets(vmSoundFilePlayLink).setStyleAttribute('color','grey');
vmSoundFileListBox.addChangeHandler(vmSoundFilePlayLinkClientHandler);
var vmSoundFileIndex = app.createTextBox().setValue(i).setName('vmIndex').setVisible(false);
vmSoundFileLinkPanel.add(vmSoundFilePlayLink).add(vmSoundFileIndex);
var vmRecordOption = app.createListBox().setName('vmRecordOption-'+i).addItem("No").addItem("Yes");
if (vmPropertyObject['vmRecordOption-'+i]) {
if (vmPropertyObject['vmRecordOption-'+i]=="Yes") {
vmRecordOption.setSelectedIndex(1);
}
}
var vmRecordHandler = app.createServerHandler('recordVm').addCallbackElement(mainPanel);
var vmRecordButton = app.createButton('Call me to record voice message').addClickHandler(vmRecordHandler);
vmTemplateGrid.setWidget(0+(i*6), 1, vmTemplateNameBox);
vmTemplateGrid.setWidget(1+(i*6), 1, vmPhoneBox);
vmTemplateGrid.setWidget(2+(i*6), 1, vmBodyArea);
vmTemplateGrid.setWidget(3+(i*6), 1, vmSoundFileListBox);
vmTemplateGrid.setWidget(3+(i*6), 2, vmSoundFileLinkPanel);
vmTemplateGrid.setWidget(3+(i*6), 4, vmRecordButton);
vmTemplateGrid.setWidget(4+(i*6), 1, vmRecordOption);
vmTemplateGrid.setStyleAttribute(5+(i*6), 1, 'backgroundColor','grey');
vmTemplateGrid.setWidget(0+(i*6), 2, app.createLabel('call if'));
var vmCondCol = app.createListBox().setName('vmCol-'+i).setWidth("90px");
for (var j=0; j<headers.length; j++) {
vmCondCol.addItem(headers[j]);
}
if (vmPropertyObject['vmCol-'+i]) {
var index = headers.indexOf(vmPropertyObject['vmCol-'+i]);
if (index!=-1) {
vmCondCol.setSelectedIndex(index);
}
}
vmTemplateGrid.setWidget(1+(i*6), 2, vmCondCol);
vmTemplateGrid.setWidget(2+(i*6), 2, app.createLabel('Language code (merge tags accepted)').setStyleAttribute('textAlign', 'right'));
vmTemplateGrid.setStyleAttribute(5+(i*6), 2, 'backgroundColor','grey');
vmTemplateGrid.setWidget(1+(i*6), 3, app.createLabel("="));
vmTemplateGrid.setStyleAttribute(5+(i*6), 3, 'backgroundColor','grey');
var vmCondVal = app.createTextBox().setName('vmVal-'+i).setWidth("100px");
if (vmPropertyObject['vmVal-'+i]) {
vmCondVal.setValue(vmPropertyObject['vmVal-'+i]);
}
var vmLang = app.createTextBox().setName('vmLang-'+i).setWidth("50px");
if (vmPropertyObject['vmLang-'+i]) {
vmLang.setValue(vmPropertyObject['vmLang-'+i]);
} else {
vmLang.setValue("en");
}
vmTemplateGrid.setWidget(1+(i*6), 4, vmCondVal);
vmTemplateGrid.setWidget(2+(i*6), 4, vmLang);
vmTemplateGrid.setStyleAttribute(5+(i*6), 4, 'backgroundColor','grey');
}
vmTemplateScrollPanel.add(vmTemplateGrid);
vmTemplatePanel.add(app.createLabel("Voice Message(s)").setStyleAttribute("color", "white").setStyleAttribute("backgroundColor", "grey").setStyleAttribute("width", "100%").setStyleAttribute('padding', '5px'));
vmTemplatePanel.add(vmTemplateScrollPanel);
vmPanel.add(vmTemplatePanel);
vmGrid.setWidget(0,0,vmPanel);
var inboundPanel = app.createVerticalPanel().setWidth("620px").setHeight("360px");
var inboundSMSPanel = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setWidth("425px");
var inboundSMSLabel = app.createLabel("Inbound SMS Handling").setStyleAttribute('width', '425px').setStyleAttribute('backgroundColor', 'grey').setStyleAttribute('color', 'white').setStyleAttribute('margin', '5px').setStyleAttribute('padding', '5px');
var inboundTextCheckBox = app.createCheckBox("send this text reply to all inbound SMS messages").setName('smsAutoReplyOption').setStyleAttribute('margin', '5px');
if (properties.smsAutoReplyOption == "true") {
inboundTextCheckBox.setValue(true);
}
var inboundTextBody = app.createTextArea().setName('smsAutoReplyBody').setWidth("425px").setStyleAttribute('margin', '5px');
if (properties.smsAutoReplyBody) {
inboundTextBody.setValue(properties.smsAutoReplyBody);
}
var inboundTextForwardPanel = app.createHorizontalPanel().setStyleAttribute('margin', '5px')
var inboundTextForwardCheckBox = app.createCheckBox('forward all inbound SMS messages to this email address').setName('smsAutoForwardOption').setStyleAttribute('padding', '5px');
if (properties.smsAutoForwardOption=="true") {
inboundTextForwardCheckBox.setValue(true);
}
var inboundTextForwardEmail = app.createTextBox().setName('smsAutoForwardEmail').setStyleAttribute('margin', '5px').setWidth("130px");
if (properties.smsAutoForwardEmail) {
inboundTextForwardEmail.setValue(properties.smsAutoForwardEmail);
}
inboundTextForwardPanel.add(inboundTextForwardCheckBox).add(inboundTextForwardEmail);
inboundSMSPanel.add(inboundSMSLabel).add(inboundTextCheckBox).add(inboundTextBody).add(inboundTextForwardPanel);
var inboundVoicePanel = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setWidth("425");
var inboundVoiceLabel = app.createLabel("Inbound Call Handling").setStyleAttribute('width', '425px').setStyleAttribute('backgroundColor', 'grey').setStyleAttribute('color', 'white').setStyleAttribute('margin', '5px').setStyleAttribute('marginTop', '8px').setStyleAttribute('padding', '5px');
var inboundVoiceCheckBox = app.createCheckBox("read this message to all inbound callers").setName('vmAutoReplyReadOption').setStyleAttribute('margin', '5px');
if (properties.vmAutoReplyReadOption=="true") {
inboundVoiceCheckBox.setValue(true);
}
var inboundVoiceBody = app.createTextArea().setWidth("425px").setName('vmAutoReplyBody').setStyleAttribute('margin', '5px');
if (properties.vmAutoReplyBody) {
inboundVoiceBody.setValue(properties.vmAutoReplyBody);
}
var inboundVoiceMessagePanel = app.createHorizontalPanel();
var inboundVoiceMessageCheckBox = app.createCheckBox("play this voice recording").setName('vmAutoReplyPlayOption').setStyleAttribute('margin', '5px');
if (properties.vmAutoReplyPlayOption=="true") {
inboundVoiceMessageCheckBox.setValue(true);
}
var inboundSoundFileListBox = app.createListBox().setName('vmAutoReplyFile').setWidth("150px");
var m = 0;
var inboundSoundFileChangeHandler = app.createServerHandler('refreshInboundPlayLink').addCallbackElement(inboundVoiceMessagePanel);
inboundSoundFileListBox.addChangeHandler(inboundSoundFileChangeHandler);
inboundSoundFileListBox.addItem("None", "")
for (var k=0; k<recordingSelectTexts.length; k++) {
inboundSoundFileListBox.addItem(recordingSelectTexts[k][0], recordingSelectValues[k][0]);
if (properties.vmAutoReplyFile) {
if (properties.vmAutoReplyFile==recordingSelectValues[k][0]) {
inboundSoundFileListBox.setSelectedIndex(k+1);
m = k;
}
}
}
if (tabIndex==2) {
inboundSoundFileListBox.setSelectedIndex(recordingSelectTexts.length);
}
var inboundRecordHandler = app.createServerHandler('inboundRecordVm').addCallbackElement(mainPanel);
if (recordingSelectValues.length>0) {
if (properties.vmAutoReplyFile!='') {
var inboundPlayLink = app.createAnchor("Play", recordingSelectValues[m][0]).setId('inboundPlayLink');
} else {
var inboundPlayLink = app.createLabel("").setId('inboundPlayLink');
}
} else {
var inboundPlayLink = app.createLabel("").setId('inboundPlayLink');
}
var inboundSoundFileDissapearHandler = app.createClientHandler().forTargets(inboundPlayLink).setStyleAttribute('color','grey');
inboundSoundFileListBox.addChangeHandler(inboundSoundFileDissapearHandler);
var inboundRecordButton = app.createButton('Call me to record voice message').addClickHandler(inboundRecordHandler);
inboundVoiceMessagePanel.add(inboundVoiceMessageCheckBox).add(inboundSoundFileListBox).add(inboundPlayLink).add(inboundRecordButton);
var inboundVoiceForwardPanel = app.createHorizontalPanel().setStyleAttribute('margin', '5px').setWidth("425px");
var inboundVoiceForwardCheckBox = app.createCheckBox('forward all inbound calls to this number').setName('vmAutoForwardOption').setStyleAttribute('margin', '5px');
if (properties.vmAutoForwardOption=="true") {
inboundVoiceForwardCheckBox.setValue(true);
}
var inboundVoiceForwardNumber = app.createTextBox().setName('vmAutoForwardNumber').setStyleAttribute('margin', '5px');
if (properties.vmAutoForwardNumber) {
inboundVoiceForwardNumber.setValue(properties.vmAutoForwardNumber);
}
inboundVoiceForwardPanel.add(inboundVoiceForwardCheckBox).add(inboundVoiceForwardNumber);
inboundVoicePanel.add(inboundVoiceLabel).add(inboundVoiceCheckBox).add(inboundVoiceBody).add(inboundVoiceMessagePanel).add(inboundVoiceForwardPanel);
inboundPanel.add(inboundSMSPanel).add(inboundVoicePanel);
tabPanel.add(smsGrid, "SMS Merge").add(vmGrid, "Voice Message Merge").add(inboundPanel,"Inbound Traffic Handling").selectTab(tabIndex);
mainPanel.add(tabPanel);
var rightSMSPanel = app.createVerticalPanel();
var rightVMPanel = app.createVerticalPanel();
var variablesPanel = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setStyleAttribute('padding', '10px').setBorderWidth(2).setStyleAttribute('borderColor', '#92C1F0').setWidth("100%");
var variablesScrollPanel = app.createScrollPanel().setHeight(130);
var variablesLabel = app.createLabel().setText("Merge tags: ").setStyleAttribute('fontWeight', 'bold');
var tags = formMule_getAvailableTags();
var flexTable = app.createFlexTable().setBorderWidth(0);
for (var i = 0; i<tags.length; i++) {
var tag = app.createLabel().setText(tags[i]);
flexTable.setWidget(i, 0, tag);
}
variablesPanel.add(variablesLabel);
variablesScrollPanel.add(flexTable);
variablesPanel.add(variablesScrollPanel);
var variablesPanel2 = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setStyleAttribute('padding', '10px').setBorderWidth(2).setStyleAttribute('borderColor', '#92C1F0').setWidth("100%");
var variablesScrollPanel2 = app.createScrollPanel().setHeight(130);
var variablesLabel2 = app.createLabel().setText("Merge tags: ").setStyleAttribute('fontWeight', 'bold');
var tags2 = formMule_getAvailableTags();
var flexTable2 = app.createFlexTable().setBorderWidth(0);
for (var i = 0; i<tags2.length; i++) {
var tag2 = app.createLabel().setText(tags2[i]);
flexTable2.setWidget(i, 0, tag2);
}
variablesPanel2.add(variablesLabel2);
variablesScrollPanel2.add(flexTable2);
variablesPanel2.add(variablesScrollPanel2);
var SMSLangPanel = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setStyleAttribute('padding', '10px').setBorderWidth(2).setStyleAttribute('borderColor', '#92C1F0').setWidth("184px").setStyleAttribute('marginTop', '2px');
var SMSLangScrollPanel = app.createScrollPanel().setHeight(130);
var SMSLangLabel = app.createLabel().setText("SMS Language Codes").setStyleAttribute('fontWeight', 'bold');
var SMSLangs = googleLangList;
var SMSLangFlexTable = app.createFlexTable().setBorderWidth(0);
for (var i = 0; i<SMSLangs.length; i++) {
var tag = app.createLabel().setText(SMSLangs[i]);
SMSLangFlexTable.setWidget(i, 0, tag);
}
SMSLangPanel.add(SMSLangLabel);
SMSLangScrollPanel.add(SMSLangFlexTable);
SMSLangPanel.add(SMSLangScrollPanel);
var VMLangPanel = app.createVerticalPanel().setStyleAttribute('backgroundColor', 'whiteSmoke').setStyleAttribute('padding', '10px').setBorderWidth(2).setStyleAttribute('borderColor', '#92C1F0').setWidth("184px").setStyleAttribute('marginTop', '2px');
var VMLangScrollPanel = app.createScrollPanel().setHeight(130);
var VMLangLabel = app.createLabel().setText("Voice Language Codes").setStyleAttribute('fontWeight', 'bold');
var VMLangs = langList;
var VMLangFlexTable = app.createFlexTable().setBorderWidth(0);
for (var i = 0; i<VMLangs.length; i++) {
var tag = app.createLabel().setText(VMLangs[i]);
VMLangFlexTable.setWidget(i, 0, tag);
}
VMLangPanel.add(VMLangLabel);
VMLangScrollPanel.add(VMLangFlexTable);
VMLangPanel.add(VMLangScrollPanel);
var rightSMSBottomPanel = app.createVerticalPanel();
var rightVMBottomPanel = app.createVerticalPanel();
rightVMBottomPanel.add(VMLangPanel);
rightSMSBottomPanel.add(SMSLangPanel);
rightSMSPanel.add(variablesPanel2);
rightSMSPanel.add(rightSMSBottomPanel);
rightVMPanel.add(variablesPanel);
rightVMPanel.add(rightVMBottomPanel);
smsGrid.setWidget(0, 1, rightSMSPanel);
vmGrid.setWidget(0, 1, rightVMPanel);
var saveHandler = app.createServerHandler('saveSmsAndVoiceSettings').addCallbackElement(mainPanel);
var saveSpinnerHandler = app.createClientHandler().forTargets(mainPanel).setStyleAttribute('opacity', '0.5').forTargets(refreshPanel).setVisible(true);
var button = app.createButton("Save settings").addClickHandler(saveHandler).addClickHandler(saveSpinnerHandler);
mainPanel.add(button);
app.add(mainPanel);
app.add(refreshPanel);
ss.show(app);
return app;
}
function refreshInboundPlayLink(e) {
var app = UiApp.getActiveApplication();
var link = app.getElementById('inboundPlayLink');
var newUrl = e.parameter.vmAutoReplyFile;
if (newUrl != '') {
link.setText("Play")
link.setHref(newUrl);
link.setStyleAttribute('color','blue');
} else {
link.setText('');
}
return app;
}
function refreshVmSoundFilePlayLink(e) {
var app = UiApp.getActiveApplication();
var i = e.parameter.vmIndex;
var link = app.getElementById('vmSoundFileLink-'+i);
var newUrl = e.parameter['vmSoundFile-'+i];
if (newUrl != '') {
link.setText("Play")
link.setHref(newUrl);
link.setStyleAttribute('color','blue');
} else {
link.setText('');
}
return app;
}
function refreshSmsTemplateGrid(e) {
var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ScriptProperties.getProperty('sheetName');
var sheet = ss.getSheetByName(sheetName);
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
var properties = ScriptProperties.getProperties();
var smsNumSelected = e.parameter.smsNumSelected;
properties.smsNumSelected = smsNumSelected;
var smsTemplateGrid = app.getElementById('smsTemplateGrid');
smsTemplateGrid.resize(smsNumSelected*4, 5);
var smsPropertyString = properties.smsPropertyString;
var smsPropertyObject = new Object();
for (var i=0; i<properties.smsNumSelected; i++) {
smsPropertyObject['smsName-'+i] = e.parameter['smsName-'+i];
smsPropertyObject['smsPhone-'+i] = e.parameter['smsPhone-'+i];
smsPropertyObject['smsBody-'+i] = e.parameter['smsBody-'+i];
smsPropertyObject['smsLang-'+i] = e.parameter['smsLang-'+i];
smsPropertyObject['smsCol-'+i] = e.parameter['smsCol-'+i];
smsPropertyObject['smsVal-'+i] = e.parameter['smsVal-'+i];
}
for (var i = 0; i<smsNumSelected; i++) {
smsTemplateGrid.setWidget(0+(i*4), 0, app.createLabel('Template Name'));
smsTemplateGrid.setWidget(1+(i*4), 0, app.createLabel('Phone #'));
smsTemplateGrid.setWidget(2+(i*4), 0, app.createLabel('Body'));
smsTemplateGrid.setStyleAttribute(3+(i*4), 0, 'backgroundColor','grey');
var smsTemplateNameBox = app.createTextBox().setName('smsName-'+i).setWidth("140px");
if (smsPropertyObject['smsName-'+i]) {
smsTemplateNameBox.setValue(smsPropertyObject['smsName-'+i]);
}
var smsPhoneBox = app.createTextBox().setName('smsPhone-'+i).setWidth("140px");
if (smsPropertyObject['smsPhone-'+i]) {
smsPhoneBox.setValue(smsPropertyObject['smsPhone-'+i]);
}
var smsBodyArea = app.createTextArea().setName('smsBody-'+i).setWidth("140px").setHeight("110px");
if (smsPropertyObject['smsBody-'+i]) {
smsBodyArea.setValue(smsPropertyObject['smsBody-'+i]);
}
smsTemplateGrid.setWidget(0+(i*4), 1, smsTemplateNameBox);
smsTemplateGrid.setWidget(1+(i*4), 1, smsPhoneBox);
smsTemplateGrid.setWidget(2+(i*4), 1, smsBodyArea);
smsTemplateGrid.setStyleAttribute(3+(i*4), 1, 'backgroundColor','grey');
smsTemplateGrid.setWidget(0+(i*4), 2, app.createLabel('send if'));
var smsCondCol = app.createListBox().setName('smsCol-'+i).setWidth("90px");
for (var j=0; j<headers.length; j++) {
smsCondCol.addItem(headers[j]);
}
if (smsPropertyObject['smsCol-'+i]) {
var index = headers.indexOf(smsPropertyObject['smsCol-'+i]);
smsCondCol.setSelectedIndex(index);
}
smsTemplateGrid.setWidget(1+(i*4), 2, smsCondCol);
smsTemplateGrid.setWidget(2+(i*4), 2, app.createLabel('Language code (merge tags accepted)').setStyleAttribute('textAlign', 'right'));
smsTemplateGrid.setStyleAttribute(3+(i*4), 2, 'backgroundColor','grey');
smsTemplateGrid.setWidget(1+(i*4), 3, app.createLabel("="));
smsTemplateGrid.setStyleAttribute(3+(i*4), 3, 'backgroundColor','grey');
var smsCondVal = app.createTextBox().setName('smsVal-'+i).setWidth("100px");
if (smsPropertyObject['smsVal-'+i]) {
smsCondVal.setValue(smsPropertyObject['smsVal-'+i]);
}
var smsLang = app.createTextBox().setName('smsLang-'+i).setWidth("50px");
if (smsPropertyObject['smsLang-'+i]) {
smsLang.setValue(smsPropertyObject['smsLang-'+i]);
} else {
smsLang.setValue('en');
}
smsTemplateGrid.setWidget(1+(i*4), 4, smsCondVal);
smsTemplateGrid.setWidget(2+(i*4), 4, smsLang);
smsTemplateGrid.setStyleAttribute(3+(i*4), 4, 'backgroundColor','grey');
}
return app;
}
function refreshVmTemplateGrid(e) {
var app = UiApp.getActiveApplication();
var mainPanel = app.getElementById("mainPanel");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ScriptProperties.getProperty('sheetName');
var sheet = ss.getSheetByName(sheetName);
var recordingSheet = ss.getSheetByName("MyVoiceRecordings");
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()[0];
var recordingSelectTexts = [];
var recordingSelectValues = [];
if (recordingSheet.getLastRow()-1>0) {
recordingSelectTexts = recordingSheet.getRange(2, 1, recordingSheet.getLastRow()-1, 1).getValues();
recordingSelectValues = recordingSheet.getRange(2, 2, recordingSheet.getLastRow()-1, 1).getValues();
}
var properties = ScriptProperties.getProperties();
var vmNumSelected = e.parameter.vmNumSelected;
properties.vmNumSelected = vmNumSelected;
var vmTemplateGrid = app.getElementById('vmTemplateGrid');
vmTemplateGrid.resize(vmNumSelected*6, 5);
var vmPropertyString = properties.vmPropertyString;
var vmPropertyObject = new Object();
for (var i=0; i<properties.vmNumSelected; i++) {
vmPropertyObject['vmName-'+i] = e.parameter['vmName-'+i];
vmPropertyObject['vmPhone-'+i] = e.parameter['vmPhone-'+i];
vmPropertyObject['vmBody-'+i] = e.parameter['vmBody-'+i];
vmPropertyObject['vmSoundFile-'+i] = e.parameter['vmSoundFile-'+i];
vmPropertyObject['vmRecordOption-'+i] = e.parameter['vmRecordOption-'+i];
vmPropertyObject['vmLang-'+i] = e.parameter['vmLang-'+i];
vmPropertyObject['vmCol-'+i] = e.parameter['vmCol-'+i];
vmPropertyObject['vmVal-'+i] = e.parameter['vmVal-'+i];
}
for (var i = 0; i<vmNumSelected; i++) {
vmTemplateGrid.setWidget(0+(i*6), 0, app.createLabel('Template Name'));
vmTemplateGrid.setWidget(1+(i*6), 0, app.createLabel('Phone #'));
vmTemplateGrid.setWidget(2+(i*6), 0, app.createLabel('Read Message'));
vmTemplateGrid.setWidget(3+(i*6), 0, app.createLabel('Play recording'))
vmTemplateGrid.setWidget(4+(i*6), 0, app.createLabel('Record response?'));
vmTemplateGrid.setStyleAttribute(5+(i*6), 0, 'backgroundColor','grey');
var vmTemplateNameBox = app.createTextBox().setName('vmName-'+i).setWidth("140px");
if (vmPropertyObject['vmName-'+i]) {
vmTemplateNameBox.setValue(vmPropertyObject['vmName-'+i]);
}
var vmPhoneBox = app.createTextBox().setName('vmPhone-'+i).setWidth("140px");
if (vmPropertyObject['vmPhone-'+i]) {
vmPhoneBox.setValue(vmPropertyObject['vmPhone-'+i]);
}
var vmBodyArea = app.createTextArea().setName('vmBody-'+i).setWidth("140px").setHeight("90px");
if (vmPropertyObject['vmBody-'+i]) {
vmBodyArea.setValue(vmPropertyObject['vmBody-'+i]);
}
var vmSoundFileListBox = app.createListBox().setName('vmSoundFile-'+i).setWidth("140px");
var soundFileArray = [];
vmSoundFileListBox.addItem("None", "");
for (var k=0; k<recordingSelectTexts.length; k++) {
vmSoundFileListBox.addItem(recordingSelectTexts[k][0], recordingSelectValues[k][0]);
soundFileArray.push(recordingSelectValues[k][0]);
}
var m = 0;
var n = -1;
if (vmPropertyObject['vmSoundFile-'+i]) {
var index = soundFileArray.indexOf(vmPropertyObject['vmSoundFile-'+i]);
if (index!=-1) {
vmSoundFileListBox.setSelectedIndex(index + 1);
m = index;
n = 0;
}
}
var vmSoundFileLinkPanel = app.createHorizontalPanel();
var vmSoundFilePlayLinkRefreshHandler = app.createServerHandler('refreshVmSoundFilePlayLink').addCallbackElement(vmSoundFileListBox).addCallbackElement(vmSoundFileLinkPanel);
vmSoundFileListBox.addChangeHandler(vmSoundFilePlayLinkRefreshHandler);
var linkText = "";
if ((n!=-1)&&(vmPropertyObject['vmSoundFileLink-'+i]!='')&&(recordingSelectValues.length>0)) {
linkText = "Play";
}
var vmSoundFilePlayLink = app.createAnchor(linkText,recordingSelectValues[m][0]).setId('vmSoundFileLink-'+i);
var vmSoundFilePlayLinkClientHandler = app.createClientHandler().forTargets(vmSoundFilePlayLink).setStyleAttribute('color','grey');
vmSoundFileListBox.addChangeHandler(vmSoundFilePlayLinkClientHandler);
var vmSoundFileIndex = app.createTextBox().setValue(i).setName('vmIndex').setVisible(false);
vmSoundFileLinkPanel.add(vmSoundFilePlayLink).add(vmSoundFileIndex);
var vmRecordOption = app.createListBox().setName('vmRecordOption-'+i).addItem("No").addItem("Yes");
if (vmPropertyObject['vmRecordOption-'+i]) {
if (vmPropertyObject['vmRecordOption-'+i]=="Yes") {
vmRecordOption.setSelectedIndex(1);
}
}
var vmRecordHandler = app.createServerHandler('recordVm').addCallbackElement(mainPanel);
var vmRecordButton = app.createButton('Call me to record voice message').addClickHandler(vmRecordHandler);
vmTemplateGrid.setWidget(0+(i*6), 1, vmTemplateNameBox);
vmTemplateGrid.setWidget(1+(i*6), 1, vmPhoneBox);
vmTemplateGrid.setWidget(2+(i*6), 1, vmBodyArea);
vmTemplateGrid.setWidget(3+(i*6), 1, vmSoundFileListBox);
vmTemplateGrid.setWidget(3+(i*6), 2, vmSoundFileLinkPanel);
vmTemplateGrid.setWidget(3+(i*6), 4, vmRecordButton);
vmTemplateGrid.setWidget(4+(i*6), 1, vmRecordOption);
vmTemplateGrid.setStyleAttribute(5+(i*6), 1, 'backgroundColor','grey');
vmTemplateGrid.setWidget(0+(i*6), 2, app.createLabel('call if'));
var vmCondCol = app.createListBox().setName('vmCol-'+i).setWidth("90px");
for (var j=0; j<headers.length; j++) {
vmCondCol.addItem(headers[j]);
}
if (properties['vmCol-'+i]) {
var index = headers.indexOf(properties['vmCol-'+i]);
if (index!=-1) {
vmCondCol.setSelectedIndex(index);
}
}
vmTemplateGrid.setWidget(1+(i*6), 2, vmCondCol);
vmTemplateGrid.setWidget(2+(i*6), 2, app.createLabel('Language code (merge tags accepted)').setStyleAttribute('textAlign', 'right'));
vmTemplateGrid.setStyleAttribute(5+(i*6), 2, 'backgroundColor','grey');
vmTemplateGrid.setWidget(1+(i*6), 3, app.createLabel("="));
vmTemplateGrid.setStyleAttribute(5+(i*6), 3, 'backgroundColor','grey');
var vmCondVal = app.createTextBox().setName('vmVal-'+i).setWidth("100px");
if (properties['vmVal-'+i]) {
vmCondVal.setValue(properties['vmVal-'+i]);
}
var vmLang = app.createTextBox().setName('vmLang-'+i).setWidth("50px");
if (properties['vmLang-'+i]) {
vmLang.setValue(vmLang);
} else {
vmLang.setValue("en");
}
vmTemplateGrid.setWidget(1+(i*6), 4, vmCondVal);
vmTemplateGrid.setWidget(2+(i*6), 4, vmLang);
vmTemplateGrid.setStyleAttribute(5+(i*6), 4, 'backgroundColor','grey');
}
return app;
}
function inboundRecordVm(e) {
var app = UiApp.getActiveApplication();
app.close();
e.parameter.num = "2";
inboundRecordApp(e)
return app;
}
function recordVm(e) {
var app = UiApp.getActiveApplication();
app.close();
e.parameter.num = "1";
inboundRecordApp(e)
return app;
}
function inboundRecordApp(e) {
saveSmsAndVoiceSettings(e);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('MyVoiceRecordings');
ss.setActiveSheet(sheet);
var app = UiApp.createApplication().setTitle("Record a new message").setHeight(200);
var panel = app.createVerticalPanel();
var phoneNumberLabel = app.createLabel("Please enter the phone number you would like Twilio to call to record your voice message");
var phoneNumberBox = app.createTextBox().setName('phone').setWidth('150px');
var lastPhone = ScriptProperties.getProperty('lastPhone');
if (lastPhone) {
phoneNumberBox.setValue(lastPhone);
}
var recordingName = app.createLabel("Please give a unique name to this recording");
var recordingNameBox = app.createTextBox().setName('name').setWidth('150px');
var hiddenNumBox = app.createTextBox().setName('num').setVisible(false).setValue(e.parameter.num);
var panel2 = app.createVerticalPanel().setId('callingPanel').setVisible(false);
var callingImage = app.createImage('http://status.twilio.com/images/logo.png').setWidth("100px");
var callingLabel = app.createLabel('Twilio is attempting to call the number you provided. This can sometimes take 30-45 seconds. Click \'Done\' once you have hung up and can see your recording show up in the \'MyVoiceRecordings\' sheet');
var doneButtonHandler = app.createServerHandler('doneRecording').addCallbackElement(panel);
var doneButton = app.createButton('Done').addClickHandler(doneButtonHandler);
panel2.add(callingImage).add(callingLabel).add(doneButton);
panel.add(phoneNumberLabel).add(phoneNumberBox).add(recordingName).add(recordingNameBox).add(hiddenNumBox);
var buttonClientHandler = app.createClientHandler().forTargets(panel2).setVisible(true).forTargets(panel).setVisible(false);
var buttonHandler = app.createServerHandler('saveRecording').addCallbackElement(panel);
var button = app.createButton('Call me to record my message').addClickHandler(buttonHandler).addClickHandler(buttonClientHandler);
panel.add(button);
app.add(panel);
app.add(panel2);
ss.show(app);
return app;
}
function saveRecording(e) {
var app = UiApp.getActiveApplication();
var num = e.parameter.num;
var phoneNumber = e.parameter.phone;
ScriptProperties.setProperty('lastPhone', phoneNumber);
var recordingName = e.parameter.name;
var accountSID = ScriptProperties.getProperty("accountSID");
var authToken = ScriptProperties.getProperty("authToken");
var twilioNumber = ScriptProperties.getProperty("twilioNumber");
var args = new Object();
args.RequestResponse = "TRUE";
args.OutboundMessageRecording = "TRUE";
args.RecordingName = recordingName;
var lang = ScriptProperties.getProperty('defaultVoiceLang');
formMule_makeRoboCall(phoneNumber, "Please record your message after the tone.", accountSID, authToken, '', args, lang);
return app;
}
function doneRecording(e) {
var app = UiApp.getActiveApplication();
var num = parseInt(e.parameter.num);
app.close();
formMule_smsAndVoiceSettings(num);
return app;
}
function saveSmsAndVoiceSettings(e) {
var app = UiApp.getActiveApplication();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var properties = ScriptProperties.getProperties();
var sheet = ss.getSheetByName(properties.sheetName);
var headers = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues()[0];