forked from saintpete/twilio-go
-
Notifications
You must be signed in to change notification settings - Fork 69
/
calls.go
318 lines (291 loc) · 10.5 KB
/
calls.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
package twilio
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
types "github.com/kevinburke/go-types"
)
const callsPathPart = "Calls"
type CallService struct {
client *Client
}
type Call struct {
Sid string `json:"sid"`
From PhoneNumber `json:"from"`
To PhoneNumber `json:"to"`
Status Status `json:"status"`
StartTime TwilioTime `json:"start_time"`
EndTime TwilioTime `json:"end_time"`
Duration TwilioDuration `json:"duration"`
// The wait time in milliseconds before the call is placed.
QueueTime TwilioDurationMS `json:"queue_time"`
AccountSid string `json:"account_sid"`
Annotation json.RawMessage `json:"annotation"`
AnsweredBy NullAnsweredBy `json:"answered_by"`
CallerName types.NullString `json:"caller_name"`
DateCreated TwilioTime `json:"date_created"`
DateUpdated TwilioTime `json:"date_updated"`
Direction Direction `json:"direction"`
ForwardedFrom PhoneNumber `json:"forwarded_from"`
GroupSid string `json:"group_sid"`
ParentCallSid string `json:"parent_call_sid"`
PhoneNumberSid string `json:"phone_number_sid"`
Price string `json:"price"`
PriceUnit string `json:"price_unit"`
APIVersion string `json:"api_version"`
URI string `json:"uri"`
}
// Ended returns true if the Call has reached a terminal state, and false
// otherwise, or if the state can't be determined.
func (c *Call) Ended() bool {
// https://www.twilio.com/docs/api/rest/call#call-status-values
switch c.Status {
case StatusCompleted, StatusCanceled, StatusFailed, StatusBusy, StatusNoAnswer:
return true
default:
return false
}
}
// EndedUnsuccessfully returns true if the Call has reached a terminal state
// and that state isn't "completed".
func (c *Call) EndedUnsuccessfully() bool {
// https://www.twilio.com/docs/api/rest/call#call-status-values
switch c.Status {
case StatusCanceled, StatusFailed, StatusBusy, StatusNoAnswer:
return true
default:
return false
}
}
// FriendlyPrice flips the sign of the Price (which is usually reported from
// the API as a negative number) and adds an appropriate currency symbol in
// front of it. For example, a PriceUnit of "USD" and a Price of "-1.25" is
// reported as "$1.25".
func (c *Call) FriendlyPrice() string {
if c == nil {
return ""
}
return price(c.PriceUnit, c.Price)
}
// A CallPage contains a Page of calls.
type CallPage struct {
Page
Calls []*Call `json:"calls"`
}
func (c *CallService) Get(ctx context.Context, sid string) (*Call, error) {
call := new(Call)
err := c.client.GetResource(ctx, callsPathPart, sid, call)
return call, err
}
// Update the call with the given data. Valid parameters may be found here:
// https://www.twilio.com/docs/api/rest/change-call-state#post-parameters
func (c *CallService) Update(ctx context.Context, sid string, data url.Values) (*Call, error) {
call := new(Call)
err := c.client.UpdateResource(ctx, callsPathPart, sid, data, call)
return call, err
}
// Cancel an in-progress Call with the given sid. Cancel will not affect
// in-progress Calls, only those in queued or ringing.
func (c *CallService) Cancel(sid string) (*Call, error) {
data := url.Values{}
data.Set("Status", string(StatusCanceled))
return c.Update(context.Background(), sid, data)
}
// Hang up an in-progress call.
func (c *CallService) Hangup(sid string) (*Call, error) {
data := url.Values{}
data.Set("Status", string(StatusCompleted))
return c.Update(context.Background(), sid, data)
}
// Redirect the given call to the given URL.
func (c *CallService) Redirect(sid string, u *url.URL) (*Call, error) {
data := url.Values{}
data.Set("Url", u.String())
return c.Update(context.Background(), sid, data)
}
// Initiate a new Call.
func (c *CallService) Create(ctx context.Context, data url.Values) (*Call, error) {
call := new(Call)
err := c.client.CreateResource(ctx, callsPathPart, data, call)
return call, err
}
// MakeCall starts a new Call from the given phone number to the given phone
// number, dialing the url when the call connects. MakeCall is a wrapper around
// Create; if you need more configuration, call that function directly.
func (c *CallService) MakeCall(from string, to string, u *url.URL) (*Call, error) {
data := url.Values{}
data.Set("From", from)
data.Set("To", to)
data.Set("Url", u.String())
return c.Create(context.Background(), data)
}
func (c *CallService) GetPage(ctx context.Context, data url.Values) (*CallPage, error) {
iter := c.GetPageIterator(data)
return iter.Next(ctx)
}
// GetCallsInRange gets an Iterator containing calls in the range [start, end),
// optionally further filtered by data. GetCallsInRange panics if start is not
// before end. Any date filters provided in data will be ignored. If you have
// an end, but don't want to specify a start, use twilio.Epoch for start. If
// you have a start, but don't want to specify an end, use twilio.HeatDeath for
// end.
//
// Assumes that Twilio returns resources in chronological order, latest
// first. If this assumption is incorrect, your results will not be correct.
//
// Returned CallPages will have at most PageSize results, but may have fewer,
// based on filtering.
func (c *CallService) GetCallsInRange(start time.Time, end time.Time, data url.Values) CallPageIterator {
if start.After(end) {
panic("start date is after end date")
}
d := url.Values{}
for k, v := range data {
d[k] = v
}
d.Del("StartTime")
d.Del("Page") // just in case
if start != Epoch {
startFormat := start.UTC().Format(APISearchLayout)
d.Set("StartTime>", startFormat)
}
if end != HeatDeath {
// If you specify "StartTime<=YYYY-MM-DD", the *latest* result returned
// will be midnight (the earliest possible second) on DD. We want all
// of the results for DD so we need to specify DD+1 in the API.
//
// TODO validate midnight-instant math more closely, since I don't think
// Twilio returns the correct results for that instant.
endFormat := end.UTC().Add(24 * time.Hour).Format(APISearchLayout)
d.Set("StartTime<", endFormat)
}
iter := NewPageIterator(c.client, d, callsPathPart)
return &callDateIterator{
start: start,
end: end,
p: iter,
}
}
// GetNextCallsInRange retrieves the page at the nextPageURI and continues
// retrieving pages until any results are found in the range given by start or
// end, or we determine there are no more records to be found in that range.
//
// If CallPage is non-nil, it will have at least one result.
func (c *CallService) GetNextCallsInRange(start time.Time, end time.Time, nextPageURI string) CallPageIterator {
if nextPageURI == "" {
panic("nextpageuri is empty")
}
iter := NewNextPageIterator(c.client, callsPathPart)
iter.SetNextPageURI(types.NullString{Valid: true, String: nextPageURI})
return &callDateIterator{
start: start,
end: end,
p: iter,
}
}
type callDateIterator struct {
p *PageIterator
start time.Time
end time.Time
}
// Next returns the next page of resources. We may need to fetch multiple
// pages from the Twilio API before we find one in the right date range, so
// latency may be higher than usual. If page is non-nil, it contains at least
// one result.
func (c *callDateIterator) Next(ctx context.Context) (*CallPage, error) {
var page *CallPage
for {
// just wipe it clean every time to avoid remnants hanging around
page = new(CallPage)
if err := c.p.Next(ctx, page); err != nil {
return nil, err
}
if len(page.Calls) == 0 {
return nil, NoMoreResults
}
times := make([]time.Time, len(page.Calls))
for i, call := range page.Calls {
if !call.DateCreated.Valid {
// we really should not ever hit this case but if we can't parse
// a date, better to give you back an error than to give you back
// a list of calls that may or may not be in the time range
return nil, fmt.Errorf("twilio: couldn't verify the date of call: %#v", call)
}
// not ideal but the start time field is not guaranteed to be
// populated.
if call.StartTime.Valid {
times[i] = call.StartTime.Time
} else {
times[i] = call.DateCreated.Time
}
}
if containsResultsInRange(c.start, c.end, times) {
indexesToDelete := indexesOutsideRange(c.start, c.end, times)
// iterate in descending order so we don't delete the wrong index
for i := len(indexesToDelete) - 1; i >= 0; i-- {
index := indexesToDelete[i]
page.Calls = append(page.Calls[:index], page.Calls[index+1:]...)
}
c.p.SetNextPageURI(page.NextPageURI)
return page, nil
}
if shouldContinuePaging(c.start, times) {
c.p.SetNextPageURI(page.NextPageURI)
continue
} else {
// should not continue paging and no results in range, stop
return nil, NoMoreResults
}
}
}
// CallPageIterator lets you retrieve consecutive pages of resources.
type CallPageIterator interface {
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
Next(context.Context) (*CallPage, error)
}
type callPageIterator struct {
p *PageIterator
}
// GetPageIterator returns an iterator which can be used to retrieve pages.
func (c *CallService) GetPageIterator(data url.Values) CallPageIterator {
iter := NewPageIterator(c.client, data, callsPathPart)
return &callPageIterator{
p: iter,
}
}
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
func (c *callPageIterator) Next(ctx context.Context) (*CallPage, error) {
cp := new(CallPage)
err := c.p.Next(ctx, cp)
if err != nil {
return nil, err
}
c.p.SetNextPageURI(cp.NextPageURI)
return cp, nil
}
// GetRecordings returns an array of recordings for this Call. Note there may
// be more than one Page of results.
func (c *CallService) GetRecordings(ctx context.Context, callSid string, data url.Values) (*RecordingPage, error) {
if data == nil {
data = url.Values{}
}
// Cheat - hit the Recordings list view with a filter instead of
// GET /calls/CA123/Recordings. The former is probably more reliable
data.Set("CallSid", callSid)
return c.client.Recordings.GetPage(ctx, data)
}
// GetRecordings returns an iterator of recording pages for this Call.
// Note there may be more than one Page of results.
func (c *CallService) GetRecordingsIterator(callSid string, data url.Values) *RecordingPageIterator {
if data == nil {
data = url.Values{}
}
// Cheat - hit the Recordings list view with a filter instead of
// GET /calls/CA123/Recordings. The former is probably more reliable
data.Set("CallSid", callSid)
return c.client.Recordings.GetPageIterator(data)
}