forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conference.go
243 lines (205 loc) · 9.33 KB
/
conference.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
package gotwilio
import (
"encoding/json"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
// Conference represents a Twilio Voice conference call
type Conference struct {
Sid string `json:"sid"`
FriendlyName string `json:"friendly_name"`
Status string `json:"status"`
Region string `json:"region"`
}
// ConferenceOptions are used for updating Conferences
type ConferenceOptions struct {
Status string `url:"Status,omitempty"`
AnnounceURL string `url:"AnnounceUrl,omitempty"`
AnnounceMethod string `url:"AnnounceMethod,omitempty"`
}
// ConferenceParticipant represents a Participant in responses from the Twilio API
type ConferenceParticipant struct {
CallSid string `json:"call_sid"`
ConferenceSid string `json:"conference_sid"`
Muted bool `json:"muted"`
Hold bool `json:"hold"`
Status string `json:"status"`
StartConferenceOnEnter bool `json:"start_conference_on_enter"`
EndConferenceOnExit bool `json:"end_conference_on_exit"`
Coaching bool `json:"coaching"`
CallSidToCoach string `json:"call_sid_to_coach"`
}
// ConferenceParticipantOptions are used for creating and updating Conference Participants.
type ConferenceParticipantOptions struct {
From string `url:"From,omitempty"`
To string `url:"To,omitempty"`
StatusCallback string `url:"StatusCallback,omitempty"`
StatusCallbackMethod string `url:"StatusCallbackMethod,omitempty"`
StatusCallbackEvent []string `url:"StatusCallbackEvent,omitempty"`
Timeout int `url:"Timeout"`
Record *bool `url:"Record,omitempty"`
Beep *bool `url:"Beep,omitempty"`
Muted *bool `url:"Muted,omitempty"`
Hold *bool `url:"Hold,omitempty"`
HoldURL *string `url:"HoldURL,omitempty"`
HoldMethod *string `url:"HoldMethod,omitempty"`
AnnounceURL *string `url:"AnnounceURL,omitempty"`
AnnounceMethod *string `url:"AnnounceMethod,omitempty"`
StartConferenceOnEnter *bool `url:"StartConferenceOnEnter,omitempty"`
EndConferenceOnExit *bool `url:"EndConferenceOnExit,omitempty"`
WaitURL string `url:"WaitURL,omitempty"`
WaitMethod string `url:"WaitMethod,omitempty"`
EarlyMedia *bool `url:"EarlyMedia,omitempty"`
MaxParticipants int `url:"MaxParticipants"`
ConferenceRecord string `url:"ConferenceRecord,omitempty"`
ConferenceTrim string `url:"ConferenceTrim,omitempty"`
ConferenceStatusCallback string `url:"ConferenceStatusCallback,omitempty"`
ConferenceStatusCallbackMethod string `url:"ConferenceStatusCallbackMethod,omitempty"`
ConferenceStatusCallbackEvent []string `url:"ConferenceStatusCallbackEvent,omitempty"`
RecordingChannels string `url:"RecordingChannels,omitempty"`
RecordingStatusCallback string `url:"RecordingStatusCallback,omitempty"`
RecordingStatusCallbackMethod string `url:"RecordingStatusCallbackMethod,omitempty"`
RecordingStatusCallbackEvent string `url:"RecordingStatusCallbackEvent,omitempty"`
SipAuthUsername string `url:"SipAuthUsername,omitempty"`
SipAuthPassword string `url:"SipAuthPassword,omitempty"`
Region string `url:"Region,omitempty"`
ConferenceRecordingStatusCallback string `url:"ConferenceRecordingStatusCallback,omitempty"`
ConferenceRecordingStatusCallbackMethod string `url:"ConferenceRecordingStatusCallbackMethod,omitempty"`
Coaching *bool `url:"Coaching,omitempty"`
CallSidToCoach string `url:"CallSidToCoach,omitempty"`
}
// GetConference fetches details for a single conference instance
// https://www.twilio.com/docs/voice/api/conference-resource#fetch-a-conference-resource
func (twilio *Twilio) GetConference(conferenceSid string) (*Conference, *Exception, error) {
res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
// handle NULL response
if res.StatusCode != http.StatusOK {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
conf := new(Conference)
err = decoder.Decode(conf)
return conf, nil, err
}
// UpdateConference to end it or play an announcement
// https://www.twilio.com/docs/voice/api/conference-resource#update-a-conference-resource
func (twilio *Twilio) UpdateConference(conferenceSid string, options *ConferenceOptions) (*Conference, *Exception, error) {
form, err := query.Values(options)
if err != nil {
return nil, nil, err
}
res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s.json", conferenceSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusOK {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
c := new(Conference)
err = decoder.Decode(c)
return c, nil, err
}
type getParticipantsResponse struct {
Participants []*ConferenceParticipant `json:"participants"`
}
// GetConferenceParticipants fetches details for all conference participants resource
// https://www.twilio.com/docs/voice/api/conference-participant-resource#read-multiple-participant-resources
func (twilio *Twilio) GetConferenceParticipants(conferenceSid string) ([]*ConferenceParticipant, *Exception, error) {
res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
// handle NULL response
if res.StatusCode != http.StatusOK {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
conf := new(getParticipantsResponse)
err = decoder.Decode(conf)
return conf.Participants, nil, err
}
// GetConferenceParticipant fetches details for a conference participant resource
// https://www.twilio.com/docs/voice/api/conference-participant-resource#fetch-a-participant-resource
func (twilio *Twilio) GetConferenceParticipant(conferenceSid, callSid string) (*ConferenceParticipant, *Exception, error) {
res, err := twilio.get(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
// handle NULL response
if res.StatusCode != http.StatusOK {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
conf := new(ConferenceParticipant)
err = decoder.Decode(conf)
return conf, nil, err
}
// AddConferenceParticipant adds a Participant to a conference by dialing out a new call
// https://www.twilio.com/docs/voice/api/conference-participant-resource#create-a-participant-agent-conference-only
func (twilio *Twilio) AddConferenceParticipant(conferenceSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) {
form, err := query.Values(participant)
if err != nil {
return nil, nil, err
}
res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants.json", conferenceSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusCreated {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
conf := new(ConferenceParticipant)
err = decoder.Decode(conf)
return conf, nil, err
}
// UpdateConferenceParticipant
// https://www.twilio.com/docs/voice/api/conference-participant-resource#create-a-participant-agent-conference-only
func (twilio *Twilio) UpdateConferenceParticipant(conferenceSid string, callSid string, participant *ConferenceParticipantOptions) (*ConferenceParticipant, *Exception, error) {
form, err := query.Values(participant)
if err != nil {
return nil, nil, err
}
res, err := twilio.post(form, twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid)))
if err != nil {
return nil, nil, err
}
decoder := json.NewDecoder(res.Body)
if res.StatusCode != http.StatusOK {
exception := new(Exception)
err = decoder.Decode(exception)
return nil, exception, err
}
p := new(ConferenceParticipant)
err = decoder.Decode(p)
return p, nil, err
}
// DeleteConferenceParticipant
func (twilio *Twilio) DeleteConferenceParticipant(conferenceSid string, callSid string) (*Exception, error) {
res, err := twilio.delete(twilio.buildUrl(fmt.Sprintf("Conferences/%s/Participants/%s.json", conferenceSid, callSid)))
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
decoder := json.NewDecoder(res.Body)
exception := new(Exception)
err = decoder.Decode(exception)
return exception, err
}
return nil, err
}