forked from mheadd/tropo-webapi-php-original
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathtropo.class.php
3215 lines (2842 loc) · 101 KB
/
tropo.class.php
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
<?php
/**
* This file contains PHP classes that can be used to interact with the Tropo WebAPI/
* @see https://www.tropo.com/docs/webapi/
*
* @copyright 2010 Mark J. Headd (http://www.voiceingov.org)
* @package TropoPHP
* @author Mark Headd
* @author Adam Kalsey
*/
/**
* The main Tropo WebAPI class.
* The methods on this class can be used to invoke specifc Tropo actions.
* @package TropoPHP
* @see https://www.tropo.com/docs/webapi/tropo.htm
*
*/
include 'tropo-rest.class.php';
class Tropo extends BaseClass {
/**
* The container for JSON actions.
*
* @var array
* @access private
*/
public $tropo;
/**
* The TTS voice to use when rendering content.
*
* @var string
* @access private
*/
private $_voice;
/**
* The language to use when rendering content.
*
* @var string
* @access private
*/
private $_language;
/**
* Class constructor for the Tropo class.
* @access private
*/
public function __construct() {
$this->tropo = array();
}
/**
* Set a default voice for use with all Text To Speech.
*
* Tropo's text to speech engine can pronounce your text with
* a variety of voices in different languages. All elements where
* you can create text to speech (TTS) accept a voice parameter.
* Tropo's default is "Allison" but you can set a default for this
* script here.
*
* @param string $voice
*/
public function setVoice($voice) {
$this->_voice = $voice;
}
/**
* Set a default language to use in speech recognition.
*
* When recognizing spoken input, Tropo allows you to set a language
* to let the platform know which language is being spoken and which
* recognizer to use. The default is en-us (US English), but you can
* set a different default to be used in your application here.
*
* @param string $language
*/
public function setLanguage($language) {
$this->_language = $language;
}
/**
* Sends a prompt to the user and optionally waits for a response.
*
* The ask method allows for collecting input using either speech
* recognition or DTMF (also known as Touch Tone). You can either
* pass in a fully-formed Ask object or a string to use as the
* prompt and an array of parameters.
*
* @param string|Ask $ask
* @param array $params
* @see https://www.tropo.com/docs/webapi/ask.htm
*/
public function ask($ask, Array $params=NULL) {
if ($ask instanceof Ask) {
if(null === $ask->getChoices()) {
throw new Exception("Missing required property: 'choices'");
}
if(null === $ask->getSay()) {
throw new Exception("Missing required property: 'say'");
}
} elseif (is_string($ask) && ($ask !== '')) {
if (isset($params) && is_array($params)) {
if (array_key_exists('choices', $params)) {
if ($params["choices"] instanceof Choices) {
$choices = $params["choices"];
} elseif (is_string($params['choices']) && ($params['choices'] !== '')) {
$params["mode"] = isset($params["mode"]) ? $params["mode"] : null;
$params["terminator"] = isset($params["terminator"]) ? $params["terminator"] : null;
$choices = new Choices($params["choices"], $params["mode"], $params["terminator"]);
} else {
throw new Exception("'choices' must be is a string or an instance of Choices.");
}
} else {
throw new Exception("Missing required property: 'choices'");
}
$p = array('event','voice','attempts', 'bargein', 'minConfidence', 'name', 'required', 'timeout', 'allowSignals', 'recognizer', 'interdigitTimeout', 'sensitivity', 'speechCompleteTimeout', 'speechIncompleteTimeout', 'promptLogSecurity', 'asrLogSecurity', 'maskTemplate');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
if (is_array($event)) {
foreach ($event as $e => $val){
$say[] = new Say($val, null, $e);
}
}
$say[] = new Say($ask);
$ask = new Ask($attempts, $bargein, $choices, $minConfidence, $name, $required, $say, $timeout, $voice, $allowSignals, $recognizer, $interdigitTimeout, $sensitivity, $speechCompleteTimeout, $speechIncompleteTimeout, $promptLogSecurity, $asrLogSecurity, $maskTemplate);
} else {
throw new Exception("When Argument 1 passed to Tropo::ask() is a string, argument 2 passed to Tropo::ask() must be of the type array.");
}
} else {
throw new Exception("Argument 1 passed to Tropo::ask() must be a string or an instance of Ask.");
}
$this->ask = sprintf('%s', $ask);
}
/**
* Places a call or sends an an IM, Twitter, or SMS message. To start a call, use the Session API to tell Tropo to launch your code.
*
* @param string|Call $call
* @param array $params
* @see https://www.tropo.com/docs/webapi/call.htm
*/
public function call($call, Array $params=NULL) {
if ($call instanceof Call) {
if(null === $call->getTo()) {
throw new Exception("Missing required property: 'to'");
}
if (is_string($call->getTo()) && ($call->getTo() != '')) {
} elseif (is_array($call->getTo())) {
foreach ($call->getTo() as $value) {
if (!(is_string($value) && ($value != ''))) {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
}
} else {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
} elseif (is_array($call) || (is_string($call) && ($call !== ''))) {
if (is_array($call)) {
$to = null;
foreach ($call as $value) {
if (is_string($value) && ($value !== '')) {
$to[] = $value;
}
}
if (null === $to) {
throw new Exception("Argument 1 passed to Tropo::call() must be a string or a string of array or an instance of Call.");
}
} else {
$to = $call;
}
if (isset($params)) {
if (is_array($params)) {
$p = array('from', 'network', 'channel', 'answerOnMedia', 'timeout', 'headers', 'allowSignals', 'machineDetection', 'voice', 'name', 'required', 'callbackUrl', 'promptLogSecurity', 'label');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$call = new Call($to, $from, $network, $channel, $answerOnMedia, $timeout, $headers, null, $allowSignals, $machineDetection, $voice, $name, $required, $callbackUrl, $promptLogSecurity, $label);
} else {
throw new Exception("When Argument 1 passed to Tropo::call() is a string, argument 2 passed to Tropo::call() must be of the type array.");
}
} else {
$call = new Call($to);
}
} else {
throw new Exception("Argument 1 passed to Tropo::call() must be a string or an instance of Call.");
}
$this->call = sprintf('%s', $call);
}
/**
* This object allows multiple lines in separate sessions to be conferenced together so that the parties on each line can talk to each other simultaneously.
* This is a voice channel only feature.
*
* @param string|Conference $conference
* @param array $params
* @see https://www.tropo.com/docs/webapi/conference.htm
*/
public function conference($conference, Array $params=NULL) {
if ($conference instanceof Conference) {
if(null === $conference->getId()) {
throw new Exception("Missing required property: 'id'");
}
if (!(is_string($conference->getId()) && ($conference->getId() != ''))) {
throw new Exception("Required property: 'id' must be a string.");
}
} elseif (is_string($conference) && ($conference !== '')) {
$id = $conference;
if (isset($params)) {
if (is_array($params)) {
$p = array('name', 'mute', 'on', 'playTones', 'required', 'terminator', 'allowSignals', 'interdigitTimeout', 'joinPrompt', 'leavePrompt', 'voice', 'promptLogSecurity');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$conference = new Conference($name, $id, $mute, $on, $playTones, $required, $terminator, $allowSignals, $interdigitTimeout, $joinPrompt, $leavePrompt, $voice, $promptLogSecurity);
} else {
throw new Exception("When Argument 1 passed to Tropo::conference() is a string, argument 2 passed to Tropo::conference() must be of the type array.");
}
} else {
$conference = new Conference(null, $id);
}
} else {
throw new Exception("Argument 1 passed to Tropo::conference() must be a string or an instance of Conference.");
}
$this->conference = sprintf('%s', $conference);
}
/**
* This function instructs Tropo to "hang-up" or disconnect the session associated with the current session.
* @see https://www.tropo.com/docs/webapi/hangup.htm
*/
public function hangup() {
$hangup = new Hangup();
$this->hangup = sprintf('%s', $hangup);
}
/**
* A shortcut method to create a session, say something, and hang up, all in one step. This is particularly useful for sending out a quick SMS or IM.
*
* @param string|Message $message
* @param array $params
* @see https://www.tropo.com/docs/webapi/message.htm
*/
public function message($message, Array $params=null) {
if ($message instanceof Message) {
if(null === $message->getSay()) {
throw new Exception("Missing required property: 'say'");
}
if(null === $message->getTo()) {
throw new Exception("Missing required property: 'to'");
}
if (is_string($message->getTo()) && ($message->getTo() != '')) {
} elseif (is_array($message->getTo())) {
foreach ($message->getTo() as $value) {
if (!(is_string($value) && ($value != ''))) {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
}
} else {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
} elseif (is_string($message) && ($message!=='')) {
if (isset($params) && is_array($params)) {
if (array_key_exists('to', $params)) {
if (is_array($params["to"])) {
foreach ($params["to"] as $value) {
if (is_string($value) && ($value !== '')) {
$to[] = $value;
} else {
throw new Exception("'to' must be is a string or an array of string.");
}
}
} elseif (is_string($params["to"]) && ($params["to"]!=='')) {
$to = $params["to"];
} else {
throw new Exception("'to' must be is a string or an array of string.");
}
} else {
throw new Exception("Missing required property: 'to'");
}
if (array_key_exists('say', $params)) {
if (is_array($params["say"])) {
$say[] = new Say($message);
foreach ($params["say"] as $value) {
if (is_string($value) && ($value !== '')) {
$say[] = new Say($value);
}
}
} elseif (is_string($params["say"]) && ($params["say"] !== '')) {
$say[] = new Say($message);
$say[] = new Say($params["say"]);
} else {
$say = new Say($message);
}
} else {
$say = new Say($message);
}
$p = array('channel', 'network', 'from', 'voice', 'timeout', 'answerOnMedia','headers','name','required','promptLogSecurity');
foreach ($p as $option) {
$$option = null;
if (is_array($params) && array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$message = new Message($say, $to, $channel, $network, $from, $voice, $timeout, $answerOnMedia, $headers, $name, $required, $promptLogSecurity);
} else {
throw new Exception("When Argument 1 passed to Tropo::message() is a string, argument 2 passed to Tropo::message() must be of the type array.");
}
} else {
throw new Exception("Argument 1 passed to Tropo::message() must be a string or an instance of Message.");
}
$this->message = sprintf('%s', $message);
}
/**
* Adds an event callback so that your application may be notified when a particular event occurs.
* Possible events are: "continue", "error", "incomplete" and "hangup".
*
* @param array $params
* @see https://www.tropo.com/docs/webapi/on.htm
*/
public function on($on) {
if ($on instanceof On) {
if(!(is_string($on->getEvent()) && ($on->getEvent() != ''))) {
throw new Exception("Missing required property: 'event'");
}
} elseif (is_array($on)) {
if (array_key_exists('event', $on)) {
if (is_string($on['event']) && ($on['event'] != '')) {
$event = $on['event'];
} else {
throw new Exception("Required property: 'event' must be a string.");
}
} else {
throw new Exception("Missing required property: 'event'");
}
if (array_key_exists('say', $on)) {
if ($on['say'] instanceof Say) {
if(!(is_string($on['say']->getValue()) && ($on['say']->getValue() != ''))) {
throw new Exception("The value of say must be a string.");
} else {
$say = $on['say'];
}
} elseif (is_array($on['say'])) {
foreach ($on['say'] as $value) {
if ($value instanceof Say) {
if(!(is_string($value->getValue()) && ($value->getValue() != ''))) {
throw new Exception("The value of say must be a string.");
}
} else {
throw new Exception("Property: 'say' must be a Say of array or an instance of Say.");
}
}
$say = $on['say'];
} else {
throw new Exception("Property: 'say' must be a Say of array or an instance of Say.");
}
} else {
$say = null;
}
$next = (array_key_exists('next', $on)) ? $on["next"] : null;
$on = new On($event, $next, $say);
} else {
throw new Exception("Argument 1 passed to Tropo::on() must be a array or an instance of On.");
}
$this->on = array(sprintf('%s', $on));
}
/**
* Plays a prompt (audio file or text to speech) and optionally waits for a response from the caller that is recorded.
* If collected, responses may be in the form of DTMF or speech recognition using a simple grammar format defined below.
* The record funtion is really an alias of the prompt function, but one which forces the record option to true regardless of how it is (or is not) initially set.
* At the conclusion of the recording, the audio file may be automatically sent to an external server via FTP or an HTTP POST/Multipart Form.
* If specified, the audio file may also be transcribed and the text returned to you via an email address or HTTP POST/Multipart Form.
*
* @param array|Record $record
* @see https://www.tropo.com/docs/webapi/record.htm
*/
public function record($record) {
if ($record instanceof Record) {
if(null === $record->getUrl()) {
throw new Exception("Missing required property: 'url'");
}
// if (!(is_string($record->getUrl()) && ($record->getUrl() != ''))) {
// throw new Exception("Required property: 'url' must be a string.");
// }
} elseif (is_array($record)) {
$params = $record;
if (!array_key_exists('url', $params)) {
throw new Exception("Missing required property: 'url'");
}
// if (!(is_string($params['url']) && ($params['url'] != ''))) {
// throw new Exception("Required property: 'url' must be a string.");
// }
$p = array('voice', 'emailFormat', 'transcription', 'terminator');
foreach ($p as $option) {
$params[$option] = array_key_exists($option, $params) ? $params[$option] : null;
}
$choices = isset($params["choices"])
? new Choices(null, null, $params["choices"])
: null;
$choices = isset($params["terminator"])
? new Choices(null, null, $params["terminator"])
: $choices;
if (!isset($params['voice'])) {
$params['voice'] = $this->_voice;
}
if (is_array($params['transcription'])) {
$p = array('url', 'id', 'emailFormat', 'language');
foreach ($p as $option) {
$$option = null;
if (!is_array($params["transcription"]) || !array_key_exists($option, $params["transcription"])) {
$params["transcription"][$option] = null;
}
}
$transcription = new Transcription($params["transcription"]["url"],$params["transcription"]["id"],$params["transcription"]["emailFormat"],$params["transcription"]["language"]);
} else {
$transcription = $params["transcription"];
}
$p = array('attempts', 'allowSignals', 'bargein', 'beep', 'format', 'maxTime', 'maxSilence', 'method', 'password', 'required', 'timeout', 'username', 'url', 'voice', 'minConfidence', 'interdigitTimeout', 'asyncUpload', 'name', 'promptLogSecurity', 'sensitivity');
foreach ($p as $option) {
$$option = null;
if (is_array($params) && array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
if (array_key_exists('say', $params)) {
if (is_string($params["say"]) && ($params["say"] !== '')) {
$say[] = new Say($params["say"]);
}
}
if (array_key_exists('event', $params)) {
if (is_array($params["event"])) {
foreach ($params["event"] as $e => $value) {
$say[] = new Say($value, null, $e);
}
}
}
if (!isset($say)) {
$say = null;
}
$record = new Record($attempts, $allowSignals, $bargein, $beep, $choices, $format, $maxSilence, $maxTime, $method, $password, $required, $say, $timeout, $transcription, $username, $url, $voice, $minConfidence, $interdigitTimeout, $asyncUpload, $name, $promptLogSecurity, $sensitivity);
} else {
throw new Exception("Argument 1 passed to Tropo::record() must be a array or an instance of Record.");
}
$this->record = sprintf('%s', $record);
}
/**
* The redirect function forwards an incoming call to another destination / phone number before answering it.
* The redirect function must be called before answer is called; redirect expects that a call be in the ringing or answering state.
* Use transfer when working with active answered calls.
*
* @param string|Redirect $redirect
* @param array $params
* @see https://www.tropo.com/docs/webapi/redirect.htm
*/
public function redirect($redirect, Array $params=NULL) {
if ($redirect instanceof Redirect) {
if(null === $redirect->getTo()) {
throw new Exception("Missing required property: 'to'");
}
if (!(is_string($redirect->getTo()) && ($redirect->getTo() != ''))) {
throw new Exception("Required property: 'to' must be a string.");
}
} elseif (is_string($redirect) && ($redirect !== '')) {
if (isset($params)) {
if (is_array($params)) {
$required = null;
if (array_key_exists('required', $params)) {
$required = $params["required"];
}
$name = null;
if (array_key_exists('name', $params)) {
$name = $params["name"];
}
$redirect = new Redirect($redirect, null, $name, $required);
} else {
throw new Exception("When Argument 1 passed to Tropo::redirect() is a string, argument 2 passed to Tropo::redirect() must be of the type array.");
}
} else {
$redirect = new Redirect($redirect);
}
} else {
throw new Exception("Argument 1 passed to Tropo::redirect() must be a string or an instance of Redirect.");
}
$this->redirect = sprintf('%s', $redirect);
}
/**
* Allows Tropo applications to reject incoming sessions before they are answered.
* For example, an application could inspect the callerID variable to determine if the user is known, and then use the reject call accordingly.
*
* @see https://www.tropo.com/docs/webapi/reject.htm
*
*/
public function reject() {
$reject = new Reject();
$this->reject = sprintf('%s', $reject);
}
/**
* When the current session is a voice channel this key will either play a message or an audio file from a URL.
* In the case of an text channel it will send the text back to the user via i nstant messaging or SMS.
*
* @param string|Say $say
* @param array $params
* @see https://www.tropo.com/docs/webapi/say.htm
*/
public function say($say, Array $params=NULL) {
if ($say instanceof Say) {
if(!(is_string($say->getValue()) && ($say->getValue() != ''))) {
throw new Exception("Missing required property: 'value'");
}
$say->setEvent(null);
} elseif (is_string($say) && ($say != '')) {
$value = $say;
if (isset($params)) {
if (is_array($params)) {
$p = array('as', 'event','voice', 'allowSignals', 'name', 'required', 'promptLogSecurity', 'media');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$voice = isset($voice) ? $voice : $this->_voice;
$event = null;
$say = new Say($value, $as, $event, $voice, $allowSignals, $name, $required, $promptLogSecurity, $media);
} else {
throw new Exception("When Argument 1 passed to Tropo::say() is a string, argument 2 passed to Tropo::say() must be of the type array.");
}
} else {
$say = new Say($value);
}
} else {
throw new Exception("Argument 1 passed to Tropo::say() must be a string or an instance of Say.");
}
$this->say = array(sprintf('%s', $say));
}
/**
* Allows Tropo applications to begin recording the current session.
* The resulting recording may then be sent via FTP or an HTTP POST/Multipart Form.
*
* @param array|StartRecording $startRecording
* @see https://www.tropo.com/docs/webapi/startrecording.htm
*/
public function startRecording($startRecording) {
if ($startRecording instanceof StartRecording) {
if(null === $startRecording->getUrl()) {
throw new Exception("Missing required property: 'url'");
}
// if (!(is_string($startRecording->getUrl()) && ($startRecording->getUrl() != ''))) {
// throw new Exception("Required property: 'url' must be a string.");
// }
} elseif (is_array($startRecording)) {
if (!array_key_exists('url', $startRecording)) {
throw new Exception("Missing required property: 'url'");
}
// if (!(is_string($startRecording['url']) && ($startRecording['url'] != ''))) {
// throw new Exception("Required property: 'url' must be a string.");
// }
$params = $startRecording;
$p = array('format', 'method', 'password', 'url', 'username', 'transcriptionID', 'transcriptionEmailFormat', 'transcriptionOutURI', 'asyncUpload', 'transcriptionLanguage');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$startRecording = new StartRecording($format, $method, $password, $url, $username, $transcriptionID, $transcriptionEmailFormat, $transcriptionOutURI, $asyncUpload, $transcriptionLanguage);
} else {
throw new Exception("Argument 1 passed to Tropo::startRecording() must be a array or an instance of StartRecording.");
}
$this->startRecording = sprintf('%s', $startRecording);
}
/**
* Stops a previously started recording.
*
* @see https://www.tropo.com/docs/webapi/stoprecording.htm
*/
public function stopRecording() {
$stopRecording = new stopRecording();
$this->stopRecording = sprintf('%s', $stopRecording);
}
/**
* Transfers an already answered call to another destination / phone number.
* Call may be transferred to another phone number or SIP address, which is set through the "to" parameter and is in URL format.
*
* @param string|Transfer $transfer
* @param array $params
* @see https://www.tropo.com/docs/webapi/transfer.htm
*/
public function transfer($transfer, Array $params=NULL) {
if ($transfer instanceof Transfer) {//$transfer is an instance of Transfer
if(null === $transfer->getTo()) {
throw new Exception("Missing required property: 'to'");
}
if (is_string($transfer->getTo()) && ($transfer->getTo() != '')) {
} elseif (is_array($transfer->getTo())) {
foreach ($transfer->getTo() as $value) {
if (!(is_string($value) && ($value != ''))) {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
}
} else {
throw new Exception("Required property: 'to' must be a string or a string of array.");
}
} elseif (is_array($transfer) || (is_string($transfer) && ($transfer !== ''))) {//$transfer is a non-empty string or a non-empty string of array
if (is_array($transfer)) {
$to = null;
foreach ($transfer as $value) {
if (is_string($value) && ($value !== '')) {
$to[] = $value;
}
}
if (null === $to) {
throw new Exception("Argument 1 passed to Tropo::transfer() must be a string or a string of array or an instance of Transfer.");
}
} else {
$to = $transfer;
}
if (isset($params)) {
if (is_array($params)) {
$choices = null;
if (array_key_exists('choices', $params)) {
if ($params["choices"] instanceof Choices) {
$choices = $params["choices"];
} elseif (is_string($params["choices"]) && ($params["choices"] !== '')) {
$choices = new Choices(null, null, $params["choices"]);
} else {
$choices = null;
}
}
if (array_key_exists('terminator', $params)) {
if (is_string($params["terminator"]) && ($params["terminator"] !== '')) {
$choices = new Choices(null, null, $params["terminator"]);
}
}
$p = array('answerOnMedia', 'ringRepeat', 'timeout', 'from', 'allowSignals', 'headers', 'machineDetection', 'voice', 'name', 'required', 'interdigitTimeout', 'playTones', 'callbackUrl', 'promptLogSecurity', 'label');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$on = null;
if (array_key_exists('on', $params)) {
if ($params['on'] instanceof On) {
if (is_string($params['on']->getEvent()) && ((strtolower($params['on']->getEvent()) == 'ring') || (strtolower($params['on']->getEvent()) == 'connect'))) {
$on = $params['on'];
} else {
throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'");
}
} elseif (is_array($params['on'])) {
foreach ($params['on'] as $value) {
if ($value instanceof On) {
if (is_string($value->getEvent()) && ((strtolower($value->getEvent()) == 'ring') || (strtolower($value->getEvent()) == 'connect'))) {
$on[] = $value;
} else {
throw new TropoException("The only event allowed on transfer is 'ring' or 'connect'");
}
}
}
}
}
$transfer = new Transfer($to, $answerOnMedia, $choices, $from, $ringRepeat, $timeout, $on, $allowSignals, $headers, $machineDetection, $voice, $name, $required, $interdigitTimeout, $playTones, $callbackUrl, $promptLogSecurity, $label);
} else {
throw new Exception("When Argument 1 passed to Tropo::transfer() is a string or a string of array, argument 2 passed to Tropo::transfer() must be of the type array.");
}
} else {
$transfer = new Transfer($to);
}
} else {
throw new Exception("Argument 1 passed to Tropo::transfer() must be a string or a string of array or an instance of Transfer.");
}
$this->transfer = sprintf('%s', $transfer);
}
/**
* Makes the Tropo sleep an active call in milliseconds
*
* @param Interger $milliseconds
* @param String or Array $allowSignals
* @see https://www.tropo.com/docs/webapi/wait.htm
*/
public function wait($wait) {
if ($wait instanceof Wait) {
if(null === $wait->getMilliseconds()) {
throw new Exception("Missing required property: 'milliseconds'");
}
} elseif(is_array($wait)) {
$params = $wait;
if (!array_key_exists("milliseconds", $params)) {
throw new Exception("Missing required property: 'milliseconds'");
}
$signal = isset($params['allowSignals']) ? $params['allowSignals'] : null;
$wait = new Wait($params["milliseconds"], $signal);
} else {
throw new Exception("Argument 1 passed to Tropo::wait() must be a array or an instance of Wait.");
}
$this->wait = sprintf('%s', $wait);
}
public function generalLogSecurity($state) {
if (is_string($state) && ($state !== '')) {
$this->generalLogSecurity = $state;
} else {
throw new Exception("Argument 1 passed to Tropo::generalLogSecurity() must be a string.");
}
}
public function answer($answer=NULL) {
if (!isset($answer)) {
$answer = "{}";
} elseif ($answer instanceof Answer) {
} elseif (is_array($answer)) {
$params = $answer;
$p = array('headers');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$answer = new Answer($headers);
} else {
throw new Exception("Argument 1 passed to Tropo::answer() must be a array or an instance of Answer.");
}
$this->answer = sprintf('%s', $answer);
}
/**
* Launches a new session with the Tropo Session API.
* (Pass through to SessionAPI class.)
*
* @param string $token Your outbound session token from Tropo
* @param array $params An array of key value pairs that will be added as query string parameters
* @return bool True if the session was launched successfully
*/
public function createSession($token, Array $params=NULL) {
try {
$session = new SessionAPI();
$result = $session->createSession($token, $params);
return $result;
}
// If an exception occurs, wrap it in a TropoException and rethrow.
catch (Exception $ex) {
throw new TropoException($ex->getMessage(), $ex->getCode());
}
}
public function sendEvent($session_id, $value) {
try {
$event = new EventAPI();
$result = $event->sendEvent($session_id, $value);
return $result;
}
catch (Exception $ex) {
throw new TropoException($ex->getMessage(), $ex->getCode());
}
}
/**
* Creates a new Tropo Application
* (Pass through to ProvisioningAPI class).
*
* @param string $userid
* @param string $password
* @param array $params
* @return string JSON
*/
public function createApplication($userid, $password, Array $params) {
$p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition');
foreach ($p as $property) {
$$property = null;
if (is_array($params) && array_key_exists($property, $params)) {
$$property = $params[$property];
}
}
try {
$provision = new ProvisioningAPI($userid, $password);
$result = $provision->createApplication($href, $name, $voiceUrl, $messagingUrl, $platform, $partition);
return $result;
}
// If an exception occurs, wrap it in a TropoException and rethrow.
catch (Exception $ex) {
throw new TropoException($ex->getMessage(), $ex->getCode());
}
}
/**
* Add/Update an address (phone number, IM address or token) for an existing Tropo application.
* (Pass through to ProvisioningAPI class).
*
* @param string $userid
* @param string $password
* @param string $applicationID
* @param array $params
* @return string JSON
*/
public function updateApplicationAddress($userid, $passwd, $applicationID, Array $params) {
$p = array('type', 'prefix', 'number', 'city', 'state', 'channel', 'username', 'password', 'token');
foreach ($p as $property) {
$$property = null;
if (is_array($params) && array_key_exists($property, $params)) {
$$property = $params[$property];
}
}
try {
$provision = new ProvisioningAPI($userid, $passwd);
$result = $provision->updateApplicationAddress($applicationID, $type, $prefix, $number, $city, $state, $channel, $username, $password, $token);
return $result;
}
// If an exception occurs, wrap it in a TropoException and rethrow.
catch (Exception $ex) {
throw new TropoException($ex->getMessage(), $ex->getCode());
}
}
/**
* Update a property (name, URL, platform, etc.) for an existing Tropo application.
* (Pass through to ProvisioningAPI class).
*
* @param string $userid
* @param string $password
* @param string $applicationID
* @param array $params
* @return string JSON
*/
public function updateApplicationProperty($userid, $password, $applicationID, Array $params) {
$p = array('href', 'name', 'voiceUrl', 'messagingUrl', 'platform', 'partition');
foreach ($p as $property) {
$$property = null;
if (is_array($params) && array_key_exists($property, $params)) {
$$property = $params[$property];
}
}
try {
$provision = new ProvisioningAPI($userid, $password);