forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_participant.go
312 lines (245 loc) · 10.1 KB
/
proxy_participant.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
// Package gotwilio is a library for interacting with http://www.twilio.com/ API.
package gotwilio
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// https://www.twilio.com/docs/proxy/api/participants
type ParticipantRequest struct {
Identifier string // mandatory
ProxyIdentifier string // optional
FriendlyName string // optional
}
type Participant struct {
Sid string `json:"sid"`
Identifier string `json:"identifier"`
DateUpdated time.Time `json:"date_updated"`
FriendlyName interface{} `json:"friendly_name"`
DateDeleted interface{} `json:"date_deleted"`
AccountSid string `json:"account_sid"`
URL string `json:"url"`
ProxyIdentifier string `json:"proxy_identifier"`
ProxyIdentifierSid string `json:"proxy_identifier_sid"`
DateCreated time.Time `json:"date_created"`
ParticipantSid string `json:"Participant_sid"`
ServiceSid string `json:"service_sid"`
Links struct {
MessageInteractions string `json:"message_interactions"`
} `json:"links"`
}
type ParticipantList struct {
Participants []Participant `json:"participants"`
Meta Meta `json:"meta"`
}
type Interaction struct {
InboundResourceStatus string `json:"inbound_resource_status"`
InboundResourceSid string `json:"inbound_resource_sid"`
OutboundResourceStatus string `json:"outbound_resource_status"`
OutboundResourceSid string `json:"outbound_resource_sid"`
InboundResourceURL string `json:"inbound_resource_url"`
Type string `json:"type"`
AccountSid string `json:"account_sid"`
OutboundResourceType string `json:"outbound_resource_type"`
DateCreated time.Time `json:"date_created"`
InboundResourceType string `json:"inbound_resource_type"`
URL string `json:"url"`
DateUpdated time.Time `json:"date_updated"`
Sid string `json:"sid"`
OutboundParticipantSid string `json:"outbound_participant_sid"`
OutboundResourceURL string `json:"outbound_resource_url"`
SessionSid string `json:"session_sid"`
ServiceSid string `json:"service_sid"`
Data string `json:"data"`
InboundParticipantSid string `json:"inbound_participant_sid"`
}
type InteractionList struct {
Meta Meta `json:"meta"`
Interactions []Interaction `json:"interactions"`
}
type Meta struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
FirstPageURL string `json:"first_page_url"`
PreviousPageURL interface{} `json:"previous_page_url"`
URL string `json:"url"`
NextPageURL interface{} `json:"next_page_url"`
Key string `json:"key"`
}
type ProxyMessage struct {
Body string // The text to send to the participant
MediaUrl string // Url to media file
Callback string // Webhook url to handle processed record.
}
// AddParticipant adds Participant to Session
func (session *ProxySession) AddParticipant(req ParticipantRequest) (response Participant, exception *Exception, err error) {
return session.AddParticipantWithContext(context.Background(), req)
}
func (session *ProxySession) AddParticipantWithContext(ctx context.Context, req ParticipantRequest) (response Participant, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants")
res, err := session.twilio.post(ctx, participantFormValues(req), twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return response, exception, err
}
err = json.Unmarshal(responseBody, &response)
return response, exception, err
}
func (session *ProxySession) ListParticipants() (response []Participant, exception *Exception, err error) {
return session.ListParticipantsWithContext(context.Background())
}
func (session *ProxySession) ListParticipantsWithContext(ctx context.Context) (response []Participant, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants")
res, err := session.twilio.get(ctx, twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return response, exception, err
}
list := ParticipantList{}
err = json.Unmarshal(responseBody, &list)
return list.Participants, exception, err
}
func (session *ProxySession) GetParticipant(participantID string) (response Participant, exception *Exception, err error) {
return session.GetParticipantWithContext(context.Background(), participantID)
}
func (session *ProxySession) GetParticipantWithContext(ctx context.Context, participantID string) (response Participant, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants", participantID)
res, err := session.twilio.get(ctx, twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return response, exception, err
}
err = json.Unmarshal(responseBody, &response)
return response, exception, err
}
// Participants cannot be changed once added. To add a new Participant, delete a Participant and add a new one.
func (session *ProxySession) DeleteParticipant(participantID string) (exception *Exception, err error) {
return session.DeleteParticipantWithContext(context.Background(), participantID)
}
func (session *ProxySession) DeleteParticipantWithContext(ctx context.Context, participantID string) (exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants", participantID)
res, err := session.twilio.delete(ctx, twilioUrl)
if err != nil {
return exception, err
}
respBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusNoContent {
exc := new(Exception)
err = json.Unmarshal(respBody, exc)
return exc, err
}
return nil, nil
}
//// INTERACTIONS
func (session *ProxySession) CreateInteraction(participantSid string, msg ProxyMessage) (response Interaction, exception *Exception, err error) {
return session.CreateInteractionWithContext(context.Background(), participantSid, msg)
}
func (session *ProxySession) CreateInteractionWithContext(ctx context.Context, participantSid string, msg ProxyMessage) (response Interaction, exception *Exception, err error) {
if msg.Body == "" {
return response, exception, errors.New("Message Body Must exist")
}
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Participants", participantSid, "MessageInteractions")
formValues := url.Values{}
formValues.Set("Body", msg.Body)
if msg.MediaUrl != "" {
formValues.Set("MediaUrl", msg.MediaUrl)
}
if msg.Callback != "" {
formValues.Set("Callback", msg.Callback)
}
res, err := session.twilio.post(ctx, formValues, twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return response, exception, err
}
err = json.Unmarshal(responseBody, &response)
return response, exception, err
}
func (session *ProxySession) GetInteractions() (response InteractionList, exception *Exception, err error) {
return session.GetInteractionsWithContext(context.Background())
}
func (session *ProxySession) GetInteractionsWithContext(ctx context.Context) (response InteractionList, exception *Exception, err error) {
twilioUrl := fmt.Sprintf("%s/%s/%s/%s/%s/%s", ProxyBaseUrl, "Services", session.ServiceSid, "Sessions", session.Sid, "Interactions")
res, err := session.twilio.get(ctx, twilioUrl)
if err != nil {
return response, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return response, exception, err
}
err = json.Unmarshal(responseBody, &response)
return response, exception, err
}
// Form values initialization
func participantFormValues(req ParticipantRequest) url.Values {
formValues := url.Values{}
formValues.Set("Identifier", req.Identifier)
if req.ProxyIdentifier != "" {
formValues.Set("ProxyIdentifier", req.ProxyIdentifier)
}
if req.FriendlyName != "" {
formValues.Set("FriendlyName", req.FriendlyName)
}
return formValues
}