/
voice_response.go
252 lines (205 loc) · 6.67 KB
/
voice_response.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
package voice
import (
"encoding/xml"
"github.com/RJPearson94/twilio-sdk-go/twiml/voice/verbs"
"github.com/RJPearson94/twilio-sdk-go/twiml/voice/verbs/nouns"
"github.com/RJPearson94/twilio-sdk-go/utils"
)
// VoiceResponse provides the structure and functions for generation TwiML that can be used
// on Programmable Voice. See https://www.twilio.com/docs/voice/twiml for more details
type VoiceResponse struct {
XMLName xml.Name `xml:"Response"`
Children []interface{}
}
// New create a new instance of VoiceResponse
func New() *VoiceResponse {
return &VoiceResponse{
Children: make([]interface{}, 0),
}
}
func (v *VoiceResponse) Connect() *verbs.Connect {
return v.ConnectWithAttributes(verbs.ConnectAttributes{})
}
func (v *VoiceResponse) ConnectWithAttributes(attributes verbs.ConnectAttributes) *verbs.Connect {
connect := &verbs.Connect{
ConnectAttributes: attributes,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, connect)
return connect
}
func (v *VoiceResponse) Dial(phoneNumber *string) *verbs.Dial {
return v.DialWithAttributes(verbs.DialAttributes{}, phoneNumber)
}
func (v *VoiceResponse) DialWithAttributes(attributes verbs.DialAttributes, phoneNumber *string) *verbs.Dial {
dial := &verbs.Dial{
DialAttributes: attributes,
Text: phoneNumber,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, dial)
return dial
}
func (v *VoiceResponse) Enqueue(name *string) *verbs.Enqueue {
return v.EnqueueWithAttributes(verbs.EnqueueAttributes{}, name)
}
func (v *VoiceResponse) EnqueueWithAttributes(attributes verbs.EnqueueAttributes, name *string) *verbs.Enqueue {
enqueue := &verbs.Enqueue{
EnqueueAttributes: attributes,
Text: name,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, enqueue)
return enqueue
}
func (v *VoiceResponse) Gather() *verbs.Gather {
return v.GatherWithAttributes(verbs.GatherAttributes{})
}
func (v *VoiceResponse) GatherWithAttributes(attributes verbs.GatherAttributes) *verbs.Gather {
gather := &verbs.Gather{
GatherAttributes: attributes,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, gather)
return gather
}
func (v *VoiceResponse) Hangup() {
v.Children = append(v.Children, &verbs.Hangup{})
}
func (v *VoiceResponse) Leave() {
v.Children = append(v.Children, &verbs.Leave{})
}
func (v *VoiceResponse) Pause() {
v.PauseWithAttributes(verbs.PauseAttributes{})
}
func (v *VoiceResponse) PauseWithAttributes(attributes verbs.PauseAttributes) {
v.Children = append(v.Children, &verbs.Pause{
PauseAttributes: attributes,
})
}
func (v *VoiceResponse) Pay() *verbs.Pay {
return v.PayWithAttributes(verbs.PayAttributes{})
}
func (v *VoiceResponse) PayWithAttributes(attributes verbs.PayAttributes) *verbs.Pay {
pay := &verbs.Pay{
PayAttributes: attributes,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, pay)
return pay
}
func (v *VoiceResponse) Play(url *string) {
v.PlayWithAttributes(verbs.PlayAttributes{}, url)
}
func (v *VoiceResponse) PlayWithAttributes(attributes verbs.PlayAttributes, url *string) {
v.Children = append(v.Children, &verbs.Play{
Text: url,
PlayAttributes: attributes,
})
}
func (v *VoiceResponse) Prompt() *verbs.Prompt {
return v.PromptWithAttributes(verbs.PromptAttributes{})
}
func (v *VoiceResponse) PromptWithAttributes(attributes verbs.PromptAttributes) *verbs.Prompt {
prompt := &verbs.Prompt{
PromptAttributes: attributes,
}
v.Children = append(v.Children, prompt)
return prompt
}
func (v *VoiceResponse) Queue(name string) {
v.QueueWithAttributes(nouns.QueueAttributes{}, name)
}
func (v *VoiceResponse) QueueWithAttributes(attributes nouns.QueueAttributes, name string) {
v.Children = append(v.Children, &nouns.Queue{
QueueAttributes: attributes,
Text: name,
})
}
func (v *VoiceResponse) Record() {
v.RecordWithAttributes(verbs.RecordAttributes{})
}
func (v *VoiceResponse) RecordWithAttributes(attributes verbs.RecordAttributes) {
v.Children = append(v.Children, &verbs.Record{
RecordAttributes: attributes,
})
}
func (v *VoiceResponse) Redirect(url string) {
v.RedirectWithAttributes(verbs.RedirectAttributes{}, url)
}
func (v *VoiceResponse) RedirectWithAttributes(attributes verbs.RedirectAttributes, url string) {
v.Children = append(v.Children, &verbs.Redirect{
Text: url,
RedirectAttributes: attributes,
})
}
func (v *VoiceResponse) Refer() *verbs.Refer {
return v.ReferWithAttributes(verbs.ReferAttributes{})
}
func (v *VoiceResponse) ReferWithAttributes(attributes verbs.ReferAttributes) *verbs.Refer {
refer := &verbs.Refer{
ReferAttributes: attributes,
}
v.Children = append(v.Children, refer)
return refer
}
func (v *VoiceResponse) Reject() {
v.RejectWithAttributes(verbs.RejectAttributes{})
}
func (v *VoiceResponse) RejectWithAttributes(attributes verbs.RejectAttributes) {
v.Children = append(v.Children, &verbs.Reject{
RejectAttributes: attributes,
})
}
func (v *VoiceResponse) Say(message string) {
v.SayWithAttributes(verbs.SayAttributes{}, message)
}
func (v *VoiceResponse) SayWithAttributes(attributes verbs.SayAttributes, message string) {
v.Children = append(v.Children, &verbs.Say{
Text: message,
SayAttributes: attributes,
})
}
func (v *VoiceResponse) Sms(message string) {
v.SmsWithAttributes(verbs.SmsAttributes{}, message)
}
func (v *VoiceResponse) SmsWithAttributes(attributes verbs.SmsAttributes, message string) {
v.Children = append(v.Children, &verbs.Sms{
Text: message,
SmsAttributes: attributes,
})
}
func (v *VoiceResponse) Start() *verbs.Start {
return v.StartWithAttributes(verbs.StartAttributes{})
}
func (v *VoiceResponse) StartWithAttributes(attributes verbs.StartAttributes) *verbs.Start {
start := &verbs.Start{
StartAttributes: attributes,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, start)
return start
}
func (v *VoiceResponse) Stop() *verbs.Stop {
return v.StopWithAttributes(verbs.StopAttributes{})
}
func (v *VoiceResponse) StopWithAttributes(attributes verbs.StopAttributes) *verbs.Stop {
stop := &verbs.Stop{
StopAttributes: attributes,
Children: make([]interface{}, 0),
}
v.Children = append(v.Children, stop)
return stop
}
// ToTwiML generates the TwiML string or returns an error if the response cannot be marshalled
func (v *VoiceResponse) ToTwiML() (*string, error) {
return v.ToString()
}
// ToString generates the TwiML string or returns an error if the response cannot be marshalled
func (v *VoiceResponse) ToString() (*string, error) {
output, err := xml.Marshal(v)
if err != nil {
return nil, err
}
return utils.String(xml.Header + string(output)), nil
}