forked from BTBurke/twiml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocabulary.go
737 lines (639 loc) · 19.8 KB
/
vocabulary.go
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
package twiml
import (
"encoding/xml"
"fmt"
)
// Twilio Client TwiML
type Client struct {
XMLName xml.Name `xml:"Client"`
Name string `xml:",chardata"`
Identity string `xml:"Identity,omitempty"` // same as name
Method string `xml:"method,attr,omitempty"`
URL string `xml:"url,attr,omitempty"`
StatusCallback string `xml:"statusCallback,attr,omitempty"`
StatusCallbackEvent string `xml:"statusCallbackEvent,attr,omitempty"`
StatusCallbackMethod string `xml:"statusCallbackMethod,attr,omitempty"`
Children []Markup `xml:",omitempty"`
}
// Add adds noun structs to a Client response as children
func (c *Client) Add(ml ...Markup) {
for _, s := range ml {
c.Children = append(c.Children, s)
}
return
}
// Validate returns an error if the TwiML is constructed improperly
func (c *Client) Validate() error {
var ok bool
if ok = Validate(AllowedMethod(c.Method)); !ok {
return fmt.Errorf("Client markup failed validation")
}
// require either name or identity
if ok = len(c.Name) > 0 || len(c.Identity) > 0; !ok {
return fmt.Errorf("Client markup failed validation")
}
if ok = c.validParameters(); !ok {
return fmt.Errorf("Client markup failed validation")
}
return nil
}
// validParameters checks that if parameters are set, name is empty and we have an identity
func (c *Client) validParameters() bool {
// cannot have both of these be true at once
return len(c.Children) == 0 || (len(c.Name) == 0 || len(c.Identity) != 0)
}
// Type returns the XML name of the verb
func (c *Client) Type() string {
return "Client"
}
// Twilio Client Parameter TwiML
type Parameter struct {
XMLName xml.Name `xml:"Parameter"`
Name string `xml:"name,attr,omitempty"`
Value string `xml:"value,attr,omitempty"`
}
func (p Parameter) Type() string {
return "Parameter"
}
// Validate <Parameter> noun
func (p Parameter) Validate() error {
if ok := Validate(Required(p.Name), Required(p.Value)); !ok {
return fmt.Errorf("parameter markup failed validation")
}
return nil
}
// Conference TwiML
type Conference struct {
XMLName xml.Name `xml:"Conference"`
ConferenceName string `xml:",chardata"`
Muted bool `xml:"muted,attr,omitempty"`
Beep string `xml:"beep,attr,omitempty"`
StartConferenceOnEnter bool `xml:"startConferenceOnEnter,attr,omitempty"`
EndConferenceOnExit bool `xml:"endConferenceOnExit,attr,omitempty"`
WaitURL string `xml:"waitUrl,attr"`
WaitMethod string `xml:"waitMethod,attr,omitempty"`
MaxParticipants int `xml:"maxParticipants,attr,omitempty"`
Record string `xml:"record,attr,omitempty"`
Region string `xml:"region,attr,omitempty"`
Trim string `xml:"trim,attr,omitempty"`
Coach string `xml:"coach,attr,omitempty"`
StatusCallbackEvent string `xml:"statusCallbackEvent,attr,omitempty"`
StatusCallback string `xml:"statusCallback,attr,omitempty"`
StatusCallbackMethod string `xml:"statusCallbackMethod,attr,omitempty"`
RecordingStatusCallback string `xml:"recordingStatusCallback,attr,omitempty"`
RecordingStatusCallbackMethod string `xml:"recordingStatusCallbackMethod,attr,omitempty"`
RecordingStatusCallbackEvent string `xml:"recordingStatusCallbackEvent,attr,omitempty"`
EventCallbackURL string `xml:"eventCallbackUrl,attr,omitempty"`
}
// Validate returns an error if the TwiML is constructed improperly
func (c *Conference) Validate() error {
ok := Validate(
OneOfOpt(c.Beep, "true", "false", "onEnter", "onExit"),
AllowedMethod(c.WaitMethod),
OneOfOpt(c.Record, "do-not-record", "record-from-start"),
OneOfOpt(c.Trim, "trim-silence", "do-not-trim"),
AllowedCallbackEvent(c.StatusCallbackEvent, ConferenceCallbackEvents),
AllowedMethod(c.StatusCallbackMethod),
AllowedMethod(c.RecordingStatusCallbackMethod),
)
if !ok {
return fmt.Errorf("Conference markup failed validation")
}
return nil
}
// Type returns the XML name of the verb
func (c *Conference) Type() string {
return "Conference"
}
// Dial TwiML
type Dial struct {
XMLName xml.Name `xml:"Dial"`
Action string `xml:"action,attr,omitempty"`
AnswerOnBridge bool `xml:"answerOnBridge,attr,omitempty"`
CallerID string `xml:"callerId,attr,omitempty"`
HangupOnStar bool `xml:"hangupOnStar,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
Record string `xml:"record,attr,omitempty"`
RecordingStatusCallback string `xml:"recordingStatusCallback,attr,omitempty"`
RecordingStatusCallbackMethod string `xml:"recordingStatusCallbackMethod,attr,omitempty"`
RecordingStatusCallbackEvent string `xml:"recordingStatusCallbackEvent,attr,omitempty"`
RingTone string `xml:"ringTone,attr,omitempty"`
Timeout int `xml:"timeout,attr,omitempty"`
TimeLimit int `xml:"timeLimit,attr,omitempty"`
Trim string `xml:"trim,attr,omitempty"`
Number string `xml:",chardata"`
Children []Markup `xml:",omitempty"`
}
// Validate returns an error if the TwiML is constructed improperly
func (d *Dial) Validate() error {
var errs []error
for _, s := range d.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb under Dial: '%T'", s)
case "Client", "Conference", "Number", "Queue", "Sip":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
ok := Validate(
OneOfOpt(d.Method, "GET", "POST"),
)
if !ok {
errs = append(errs, fmt.Errorf("Dial did not pass validation"))
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Add adds noun structs to a Dial response as children
func (d *Dial) Add(ml ...Markup) {
for _, s := range ml {
d.Children = append(d.Children, s)
}
return
}
// Type returns the XML name of the verb
func (d *Dial) Type() string {
return "Dial"
}
// Enqueue TwiML
type Enqueue struct {
XMLName xml.Name `xml:"Enqueue"`
Action string `xml:"action,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
WaitURL string `xml:"waitUrl,attr,omitempty"`
WaitURLMethod string `xml:"waitUrlMethod,attr,omitempty"`
WorkflowSid string `xml:"workflowSid,attr,omitempty"`
QueueName string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (e *Enqueue) Validate() error {
ok := Validate(
AllowedMethod(e.Method),
AllowedMethod(e.WaitURLMethod),
)
if !ok {
return fmt.Errorf("%s markup failed validation", e.Type())
}
return nil
}
// Type returns the XML name of the verb
func (e *Enqueue) Type() string {
return "Enqueue"
}
// Hangup TwiML
type Hangup struct {
XMLName xml.Name `xml:"Hangup"`
}
// Validate returns an error if the TwiML is constructed improperly
func (h *Hangup) Validate() error {
return nil
}
// Type returns the XML name of the verb
func (h *Hangup) Type() string {
return "Hangup"
}
// Leave TwiML
type Leave struct {
XMLName xml.Name `xml:"Leave"`
}
// Validate returns an error if the TwiML is constructed improperly
func (l *Leave) Validate() error {
return nil
}
// Type returns the XML name of the verb
func (l *Leave) Type() string {
return "Leave"
}
// Sms TwiML sends an SMS message. Text is required. See the Twilio docs
// for an explanation of the default values of to and from.
type Sms struct {
XMLName xml.Name `xml:"Message"`
To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"`
Action string `xml:"action,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
StatusCallback string `xml:"statusCallback,attr,omitempty"`
Text string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Sms) Validate() error {
ok := Validate(
AllowedMethod(s.Method),
Required(s.Text),
)
if !ok {
return fmt.Errorf("%s markup failed validation", s.Type())
}
return nil
}
// Type returns the XML name of the verb
func (s *Sms) Type() string {
return "Sms"
}
// Number TwiML
type Number struct {
XMLName xml.Name `xml:"Number"`
SendDigits string `xml:"sendDigits,attr,omitempty"`
URL string `xml:"url,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
Number string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (n *Number) Validate() error {
ok := Validate(
NumericOpt(n.SendDigits),
AllowedMethod(n.Method),
Required(n.Number),
)
if !ok {
return fmt.Errorf("%s markup failed validation", n.Type())
}
return nil
}
// Type returns the XML name of the verb
func (n *Number) Type() string {
return "Number"
}
// Pause TwiML
type Pause struct {
XMLName xml.Name `xml:"Pause"`
Length int `xml:"length,attr,omitempty"`
}
// Validate returns an error if the TwiML is constructed improperly
func (p *Pause) Validate() error {
return nil
}
// Type returns the XML name of the verb
func (p *Pause) Type() string {
return "Pause"
}
// Play TwiML
type Play struct {
XMLName xml.Name `xml:"Play"`
Loop int `xml:"loop,attr,omitempty"`
Digits string `xml:"digits,attr,omitempty"`
URL string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (p *Play) Validate() error {
ok := Validate(
NumericOrWait(p.Digits),
)
if !ok {
return fmt.Errorf("%s markup failed validation", p.Type())
}
return nil
}
// Type returns the XML name of the verb
func (p *Play) Type() string {
return "Play"
}
// Queue TwiML
type Queue struct {
XMLName xml.Name `xml:"Queue"`
URL string `xml:"url,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
ReservationSid string `xml:"reservationSid,attr,omitempty"`
PostWorkActivitySid string `xml:"postWorkActivitySid,attr,omitempty"`
Name string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (q *Queue) Validate() error {
ok := Validate(
AllowedMethod(q.Method),
Required(q.Name),
)
if !ok {
return fmt.Errorf("%s markup failed validation", q.Type())
}
return nil
}
// Type returns the XML name of the verb
func (q *Queue) Type() string {
return "Queue"
}
// Record TwiML
type Record struct {
XMLName xml.Name `xml:"Record"`
Action string `xml:"action,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
Timeout int `xml:"timeout,attr,omitempty"`
FinishOnKey string `xml:"finishOnKey,attr,omitempty"`
MaxLength int `xml:"maxLength,attr,omitempty"`
PlayBeep string `xml:"playBeep,attr,omitempty"`
Trim string `xml:"trim,attr,omitempty"`
RecordingStatusCallback string `xml:"recordingStatusCallback,attr,omitempty"`
RecordingStatusCallbackMethod string `xml:"recordingStatusCallbackMethod,attr,omitempty"`
Transcribe bool `xml:"transcribe,attr,omitempty"`
TranscribeCallback string `xml:"transcribeCallback,attr,omitempty"`
}
// Validate returns an error if the TwiML is constructed improperly
func (r *Record) Validate() error {
ok := Validate(
AllowedMethod(r.Method),
OneOfOpt(r.Trim, TrimSilence, DoNotTrim),
AllowedMethod(r.RecordingStatusCallbackMethod),
)
if !ok {
return fmt.Errorf("%s markup failed validation", r.Type())
}
return nil
}
// Type returns the XML name of the verb
func (r *Record) Type() string {
return "Record"
}
// Redirect TwiML
type Redirect struct {
XMLName xml.Name `xml:"Redirect"`
Method string `xml:"method,attr,omitempty"`
URL string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (r *Redirect) Validate() error {
ok := Validate(
AllowedMethod(r.Method),
Required(r.URL),
)
if !ok {
return fmt.Errorf("%s markup failed validation", r.Type())
}
return nil
}
// Type returns the XML name of the verb
func (r *Redirect) Type() string {
return "Redirect"
}
// Reject TwiML
type Reject struct {
XMLName xml.Name `xml:"Reject"`
Reason string `xml:"reason,attr,omitempty"`
}
// Validate returns an error if the TwiML is constructed improperly
func (r *Reject) Validate() error {
ok := Validate(
OneOfOpt(r.Reason, "rejected", "busy"),
)
if !ok {
return fmt.Errorf("%s markup failed validation", r.Type())
}
return nil
}
// Type returns the XML name of the verb
func (r *Reject) Type() string {
return "Reject"
}
// Say TwiML
type Say struct {
XMLName xml.Name `xml:"Say"`
Voice string `xml:"voice,attr,omitempty"`
Language string `xml:"language,attr,omitempty"`
Loop int `xml:"loop,attr,omitempty"`
Text string `xml:",chardata"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Say) Validate() error {
ok := Validate(
OneOfOpt(s.Voice, Man, Woman, Alice),
AllowedLanguage(s.Voice, s.Language),
Required(s.Text),
)
if !ok {
return fmt.Errorf("%s markup failed validation", s.Type())
}
return nil
}
// Type returns the XML name of the verb
func (s *Say) Type() string {
return "Say"
}
// Sip TwiML
type Sip struct {
XMLName xml.Name `xml:"Sip"`
Username string `xml:"username,attr,omitempty"`
Password string `xml:"password,attr,omitempty"`
URL string `xml:"url,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
StatusCallbackEvent string `xml:"statusCallbackEvent,attr,omitempty"`
StatusCallback string `xml:"statusCallback,attr,omitempty"`
StatusCallbackMethod string `xml:"statusCallbackMethod,attr,omitempty"`
Address string `xml:",chardata"`
}
// TODO: Needs helpers to construct the SIP URL (specifying transport
// and headers) See https://www.twilio.com/docs/api/twiml/sip
// Validate returns an error if the TwiML is constructed improperly
func (s *Sip) Validate() error {
//TODO: needs a custom validator type for statusCallbackEvent when set
//because valid values can be concatenated
ok := Validate(
AllowedMethod(s.StatusCallbackMethod),
AllowedCallbackEvent(s.StatusCallbackEvent, SipCallbackEvents),
Required(s.Address),
)
if !ok {
return fmt.Errorf("%s markup failed validation", s.Type())
}
return nil
}
// Type returns the XML name of the verb
func (s *Sip) Type() string {
return "Sip"
}
// Gather TwiML
type Gather struct {
XMLName xml.Name `xml:"Gather"`
Action string `xml:"action,attr,omitempty"`
Method string `xml:"method,attr,omitempty"`
Timeout int `xml:"timeout,attr,omitempty"`
FinishOnKey string `xml:"finishOnKey,attr,omitempty"`
NumDigits int `xml:"numDigits,attr,omitempty"`
Input string `xml:"input,attr,omitempty"`
Hints string `xml:"hints,attr,omitempty"`
PartialResultCallback string `xml:"partialResultCallback,attr,omitempty"`
Language string `xml:"language,attr,omitempty"`
ProfanityFilter bool `xml:"profanityFilter,attr,omitempty"`
SpeechTimeout string `xml:"speechTimeout,attr,omitempty"`
Children []Markup `valid:"-"`
}
// Validate returns an error if the TwiML is constructed improperly
func (g *Gather) Validate() error {
var errs []error
for _, s := range g.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb as child of Gather: '%T'", s)
case "Say", "Play", "Pause":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
ok := Validate(
AllowedMethod(g.Method),
)
if !ok {
errs = append(errs, fmt.Errorf("Gather failed validation"))
return ValidationError{errs}
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Add collects digits a caller enter by pressing the keypad to an existing Gather verb.
// Valid nested verbs: Say, Pause, Play
func (g *Gather) Add(ml ...Markup) {
for _, s := range ml {
g.Children = append(g.Children, s)
}
return
}
// Type returns the XML name of the verb
func (g *Gather) Type() string {
return "Gather"
}
// Start TWiml for Streaming
type Start struct {
XMLName xml.Name `xml:"Start"`
Children []Markup `valid:"-"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Start) Validate() error {
var errs []error
for _, s := range s.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb as child of Gather: '%T'", s)
case "Stream":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Type returns the XML name of the verb
func (s *Start) Type() string {
return "Start"
}
// Add adds noun structs to a Start response as children
func (s *Start) Add(ml ...Markup) {
for _, m := range ml {
s.Children = append(s.Children, m)
}
return
}
// Stop TWiml for Streaming
type Stop struct {
XMLName xml.Name `xml:"Stop"`
Children []Markup `valid:"-"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Stop) Validate() error {
var errs []error
for _, s := range s.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb as child of Gather: '%T'", s)
case "Stream":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Type returns the XML name of the verb
func (s *Stop) Type() string {
return "Stop"
}
// Add adds noun structs to a Stop response as children
func (s *Stop) Add(ml ...Markup) {
for _, m := range ml {
s.Children = append(s.Children, m)
}
return
}
// Stream TWiml for Streaming
type Stream struct {
XMLName xml.Name `xml:"Stream"`
Name string `xml:"name,attr"`
URL string `xml:"url,attr"`
Track string `xml:"track,attr,omitempty"`
StatusCallback string `xml:"statusCallback,attr,omitempty"`
StatusCallbackMethod string `xml:"statusCallbackMethod,attr,omitempty"`
Children []Markup `valid:"-"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Stream) Validate() error {
var errs []error
for _, s := range s.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb as child of Gather: '%T'", s)
case "Parameter":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Type returns the XML name of the verb
func (s *Stream) Type() string {
return "Stream"
}
// Add adds noun structs to a Stream response as children
func (s *Stream) Add(ml ...Markup) {
for _, m := range ml {
s.Children = append(s.Children, m)
}
return
}
// Connect TWiml for Streaming
type Connect struct {
XMLName xml.Name `xml:"Connect"`
Children []Markup `valid:"-"`
}
// Validate returns an error if the TwiML is constructed improperly
func (s *Connect) Validate() error {
var errs []error
for _, s := range s.Children {
switch t := s.Type(); t {
default:
return fmt.Errorf("Not a valid verb as child of Gather: '%T'", s)
case "Connect":
if childErr := s.Validate(); childErr != nil {
errs = append(errs, childErr)
}
}
}
if len(errs) > 0 {
return ValidationError{errs}
}
return nil
}
// Type returns the XML name of the verb
func (s *Connect) Type() string {
return "Connect"
}
// Add adds noun structs to a Connect response as children
func (s *Connect) Add(ml ...Markup) {
for _, m := range ml {
s.Children = append(s.Children, m)
}
return
}