File size: 3,636 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package twiml

import (
	"encoding/xml"
	"time"
)

// Hangup is a verb that hangs up the call.
type Hangup struct{}

// The Pause verb waits silently for a specific number of seconds.
//
// If Pause is the first verb in a TwiML document, Twilio will wait the specified number of seconds before picking up the call.
// https://www.twilio.com/docs/voice/twiml/pause
type Pause struct {
	// Dur derives from the `length` attribute and indicates the length of the pause.
	//
	// https://www.twilio.com/docs/voice/twiml/pause#attributes-length
	Dur time.Duration `xml:"-"`
}

func (p *Pause) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type PauseRaw Pause
	var pp struct {
		PauseRaw
		Length NullInt `xml:"length,attr"`
	}
	if err := d.DecodeElement(&pp, &start); err != nil {
		return err
	}
	*p = Pause(pp.PauseRaw)
	if pp.Length.Valid {
		p.Dur = time.Duration(pp.Length.Value) * time.Second
	} else {
		p.Dur = time.Second
	}
	return nil
}

func (p Pause) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	type PauseRaw Pause
	var pp struct {
		PauseRaw
		Length *int `xml:"length,attr,omitempty"`
	}
	sec := int(p.Dur / time.Second)
	pp.Length = &sec
	pp.PauseRaw = PauseRaw(p)
	return e.EncodeElement(pp, start)
}

// The Redirect verb redirects the current call to a new TwiML document.
type Redirect struct {
	// Method defaults to POST.
	//
	// https://www.twilio.com/docs/voice/twiml/redirect#attributes-method
	Method string `xml:"method,attr,omitempty"`

	// URL is the URL to redirect to.
	URL string `xml:",chardata"`
}

func (r *Redirect) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type RedirectRaw Redirect
	var rr struct {
		RedirectRaw
	}
	if err := d.DecodeElement(&rr, &start); err != nil {
		return err
	}
	*r = Redirect(rr.RedirectRaw)
	if r.Method == "" {
		r.Method = "POST"
	}
	return nil
}

// The Reject verb rejects an incoming call.
type Reject struct {
	// Reason defaults to 'rejected'.
	//
	// https://www.twilio.com/docs/voice/twiml/reject#attributes-reason
	Reason string `xml:"reason,attr,omitempty"`
}

type RejectReason int

const (
	RejectReasonRejected RejectReason = iota
	RejectReasonBusy
)

func (r *Reject) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	if err := d.DecodeElement(&r, &start); err != nil {
		return err
	}
	if r.Reason == "" {
		r.Reason = "rejected"
	}

	return nil
}

// Say is a TwiML verb that plays back a message to the caller.
type Say struct {
	Content  string `xml:",innerxml"`
	Language string `xml:"language,attr,omitempty"`
	Voice    string `xml:"voice,attr,omitempty"`

	// LoopCount is derived from the `loop` attribute, taking into account the default value
	// and the special value of 0.
	//
	// If LoopCount is below zero, the message is not played.
	//
	// https://www.twilio.com/docs/voice/twiml/say#attributes-loop
	LoopCount int `xml:"-"`
}

var (
	_ xml.Unmarshaler = (*Say)(nil)
	_ xml.Marshaler   = Say{}
)

func (s *Say) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type SayRaw Say
	var ss struct {
		SayRaw
		Loop NullInt `xml:"loop,attr"`
	}
	if err := d.DecodeElement(&ss, &start); err != nil {
		return err
	}
	*s = Say(ss.SayRaw)
	if ss.Loop.Valid {
		s.LoopCount = ss.Loop.Value
		if s.LoopCount == 0 {
			s.LoopCount = 1000
		}
	}
	return nil
}

func (s Say) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	type SayRaw Say
	var ss struct {
		SayRaw
		Content string `xml:",innerxml"`
		Loop    *int   `xml:"loop,attr"`
	}
	ss.Content = s.Content
	if s.LoopCount != 0 {
		ss.Loop = &s.LoopCount
	}
	ss.SayRaw = SayRaw(s)
	return e.EncodeElement(ss, start)
}