forked from Vonage/vonage-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice.go
514 lines (429 loc) · 16.1 KB
/
voice.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package vonage
import (
"context"
"encoding/json"
"errors"
"github.com/antihax/optional"
"github.com/vonage/vonage-go-sdk/ncco"
"github.com/vonage/vonage-go-sdk/pkg/voice"
)
// VoiceClient for working with the Voice API
type VoiceClient struct {
Config *voice.Configuration
JWT string
}
// NewVoiceClient Creates a new Voice Client, supplying an Auth to work with
func NewVoiceClient(Auth Auth) *VoiceClient {
client := new(VoiceClient)
creds := Auth.GetCreds()
client.JWT = creds[0]
client.Config = voice.NewConfiguration()
client.Config.UserAgent = GetUserAgent()
client.Config.AddDefaultHeader("Authorization", "Bearer "+client.JWT)
return client
}
// List your calls
func (client *VoiceClient) GetCalls() (voice.GetCallsResponse, VoiceErrorResponse, error) {
// create the client
voiceClient := voice.NewAPIClient(client.Config)
// set up and then parse the options
voiceOpts := voice.GetCallsOpts{}
ctx := context.Background()
result, _, err := voiceClient.CallsApi.GetCalls(ctx, &voiceOpts)
// catch HTTP errors
if err != nil {
return voice.GetCallsResponse{}, VoiceErrorResponse{}, err
}
return result, VoiceErrorResponse{}, nil
}
// GetCall for the details of a specific call
func (client *VoiceClient) GetCall(uuid string) (voice.GetCallResponse, VoiceErrorResponse, error) {
// create the client
voiceClient := voice.NewAPIClient(client.Config)
ctx := context.Background()
result, _, err := voiceClient.CallsApi.GetCall(ctx, uuid)
// catch HTTP errors
if err != nil {
return voice.GetCallResponse{}, VoiceErrorResponse{}, err
}
return result, VoiceErrorResponse{}, nil
}
// CreateCallOpts: Options for creating a call
type CreateCallOpts struct {
From CallFrom
To CallTo
Ncco ncco.Ncco
AnswerUrl []string
AnswerMethod string
EventUrl []string
EventMethod string
MachineDetection string
LengthTimer int32
RingingTimer int32
}
// CallFrom details of the caller
type CallFrom struct {
Type string
Number string
}
// CallTo details of the callee
type CallTo struct {
Type string
Number string
DtmfAnswer string
}
// VoiceErrorResponse is a container for error types since we can get more than
// one type of error back and they have incompatible data types
type VoiceErrorResponse struct {
Error interface{}
}
// VoiceErrorInvalidParamsResponse can come with a 400 response if
// it is caused by some invalid_parameters
type VoiceErrorInvalidParamsResponse struct {
Type int `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
InvalidParameters []map[string]string `json:"invalid_parameters,omitempty"`
}
// VoiceErrorGeneralResponse covers some common error types that come
// from the webserver/gateway rather than the API itself
type VoiceErrorGeneralResponse struct {
Type string `json:"type,omitempty"`
Title string `json:"error_title,omitempty"`
}
func (client *VoiceClient) createCallCommon(opts CreateCallOpts) voice.CreateCallRequestBase {
var target voice.CreateCallRequestBase
// assuming phone to start with, this needs other endpoints added later
var to []voice.EndpointPhoneTo
to_phone := voice.EndpointPhoneTo{Type: "phone", Number: opts.To.Number}
if opts.To.DtmfAnswer != "" {
to_phone.DtmfAnswer = opts.To.DtmfAnswer
}
to = append(to, to_phone)
target.To = to
// from has to be a phone
target.From = voice.EndpointPhoneFrom{Type: "phone", Number: opts.From.Number}
// event settings
if len(opts.EventUrl) > 0 {
target.EventUrl = opts.EventUrl
if opts.EventMethod != "" {
target.EventMethod = opts.EventMethod
}
}
// other fields
if opts.MachineDetection != "" {
target.MachineDetection = opts.MachineDetection
}
if opts.RingingTimer != 0 {
target.RingingTimer = opts.RingingTimer
}
if opts.LengthTimer != 0 {
target.LengthTimer = opts.LengthTimer
}
return target
}
// CreateCall Makes a phone call given the from/to details and an NCCO or an Answer URL
func (client *VoiceClient) CreateCall(opts CreateCallOpts) (voice.CreateCallResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
// use the same validation regardless of which type of call this is
commonFields := client.createCallCommon(opts)
// ncco has its own features
if len(opts.Ncco.GetActions()) > 0 {
// copy the common fields into the appropriate struct
voiceCallOpts := voice.CreateCallRequestNcco{}
voiceCallOpts.To = commonFields.To
voiceCallOpts.From = commonFields.From
voiceCallOpts.EventUrl = commonFields.EventUrl
voiceCallOpts.EventMethod = commonFields.EventMethod
voiceCallOpts.MachineDetection = commonFields.MachineDetection
voiceCallOpts.LengthTimer = commonFields.LengthTimer
voiceCallOpts.RingingTimer = commonFields.RingingTimer
// add NCCO
voiceCallOpts.Ncco = opts.Ncco.GetActions()
callOpts := optional.NewInterface(voiceCallOpts)
ctx := context.Background()
createCallOpts := &voice.CreateCallOpts{Opts: callOpts}
NccoResult, _, NccoErr := voiceClient.CallsApi.CreateCall(ctx, createCallOpts)
return client.handleCreateCallErrors(NccoResult, NccoErr)
} else if len(opts.AnswerUrl) > 0 {
voiceCallOpts := voice.CreateCallRequestAnswerUrl{}
// copy the common fields into the appropriate struct
voiceCallOpts.To = commonFields.To
voiceCallOpts.From = commonFields.From
voiceCallOpts.EventUrl = commonFields.EventUrl
voiceCallOpts.EventMethod = commonFields.EventMethod
voiceCallOpts.MachineDetection = commonFields.MachineDetection
voiceCallOpts.LengthTimer = commonFields.LengthTimer
voiceCallOpts.RingingTimer = commonFields.RingingTimer
// answer details
voiceCallOpts.AnswerUrl = opts.AnswerUrl
if opts.AnswerMethod != "" {
voiceCallOpts.AnswerMethod = opts.AnswerMethod
}
callOpts := optional.NewInterface(voiceCallOpts)
ctx := context.Background()
createCallOpts := &voice.CreateCallOpts{Opts: callOpts}
AnswerResult, _, AnswerErr := voiceClient.CallsApi.CreateCall(ctx, createCallOpts)
return client.handleCreateCallErrors(AnswerResult, AnswerErr)
}
// this is a backstop, we shouldn't end up here
return voice.CreateCallResponse{}, VoiceErrorResponse{}, errors.New("Unsupported combination of parameters, supply an answer URL or valid NCCO")
}
func (client *VoiceClient) handleCreateCallErrors(result voice.CreateCallResponse, err error) (voice.CreateCallResponse, VoiceErrorResponse, error) {
if err != nil {
e := err.(voice.GenericOpenAPIError)
errorType := e.Error()
data := e.Body()
// now handle the errors we know we might get
if errorType == "401 Unauthorized" {
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return voice.CreateCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
} else if errorType == "404 Not Found" {
var errResp VoiceErrorInvalidParamsResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return voice.CreateCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
} else if errorType == "400 Bad Request" {
var errResp VoiceErrorInvalidParamsResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return voice.CreateCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
} else {
return voice.CreateCallResponse{}, VoiceErrorResponse{}, err
}
}
return result, VoiceErrorResponse{}, nil
}
// TransferCallOpts: Options for transferring a call
type TransferCallOpts struct {
Uuid string
Ncco ncco.Ncco
AnswerUrl []string
}
type ModifyCallResponse struct {
Status string
}
type TransferDestinationUrl struct {
Type string `json:"type"`
Url []string `json:"url"`
}
type TransferWithUrlOpts struct {
Action string `json:"action"`
Destination TransferDestinationUrl `json:"destination"`
}
type TransferDestinationNcco struct {
Type string `json:"type"`
Ncco ncco.Ncco `json:"ncco"`
}
type TransferWithNccoOpts struct {
Action string `json:"action"`
Destination TransferDestinationNcco `json:"destination"`
}
// TransferCall wraps the Modify Call API endpoint
func (client *VoiceClient) TransferCall(opts TransferCallOpts) (ModifyCallResponse, VoiceErrorResponse, error) {
// create the client
voiceClient := voice.NewAPIClient(client.Config)
if len(opts.AnswerUrl) > 0 {
destination := TransferDestinationUrl{Type: "ncco", Url: opts.AnswerUrl}
transfer := TransferWithUrlOpts{Action: "transfer", Destination: destination}
modifyCallOpts := voice.ModifyCallOpts{Opts: optional.NewInterface(transfer)}
ctx := context.Background()
response, err := voiceClient.CallsApi.UpdateCall(ctx, opts.Uuid, &modifyCallOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
errorType := e.Error()
if errorType == "400 Bad Request" {
var errResp VoiceErrorInvalidParamsResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return ModifyCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
}
return ModifyCallResponse{}, VoiceErrorResponse{Error: response}, err
} else {
// not a whole lot to return as it's a 204, this branch is success
return ModifyCallResponse{Status: "0"}, VoiceErrorResponse{}, nil
}
} else if len(opts.Ncco.GetActions()) > 0 {
destination := TransferDestinationNcco{Type: "ncco", Ncco: opts.Ncco}
transfer := TransferWithNccoOpts{Action: "transfer", Destination: destination}
modifyCallOpts := voice.ModifyCallOpts{Opts: optional.NewInterface(transfer)}
ctx := context.Background()
response, err := voiceClient.CallsApi.UpdateCall(ctx, opts.Uuid, &modifyCallOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
errorType := e.Error()
if errorType == "400 Bad Request" {
var errResp VoiceErrorInvalidParamsResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return ModifyCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
}
return ModifyCallResponse{}, VoiceErrorResponse{Error: response}, err
} else {
// not a whole lot to return as it's a 204, this branch is success
return ModifyCallResponse{Status: "0"}, VoiceErrorResponse{}, nil
}
}
// this is a backstop, we shouldn't end up here
return ModifyCallResponse{}, VoiceErrorResponse{}, errors.New("Unsupported combination of parameters, supply an answer URL or valid NCCO")
}
type ModifyCallOpts struct {
Action string `json:"action"`
}
// Hangup wraps the Modify Call API endpoint
func (client *VoiceClient) Hangup(uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
return client.voiceAction("hangup", uuid)
}
// Mute wraps the Modify Call API endpoint
func (client *VoiceClient) Mute(uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
return client.voiceAction("mute", uuid)
}
// Unmute wraps the Modify Call API endpoint
func (client *VoiceClient) Unmute(uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
return client.voiceAction("unmute", uuid)
}
// Earmuff wraps the Modify Call API endpoint
func (client *VoiceClient) Earmuff(uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
return client.voiceAction("earmuff", uuid)
}
// Unearmuff wraps the Modify Call API endpoint
func (client *VoiceClient) Unearmuff(uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
return client.voiceAction("unearmuff", uuid)
}
// voiceAction holds the code for the actions that have no extra params
func (client *VoiceClient) voiceAction(action string, uuid string) (ModifyCallResponse, VoiceErrorResponse, error) {
// create the client
voiceClient := voice.NewAPIClient(client.Config)
modifyCallOpts := voice.ModifyCallOpts{Opts: optional.NewInterface(ModifyCallOpts{Action: action})}
ctx := context.Background()
response, err := voiceClient.CallsApi.UpdateCall(ctx, uuid, &modifyCallOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
errorType := e.Error()
if errorType == "400 Bad Request" {
var errResp VoiceErrorInvalidParamsResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return ModifyCallResponse{}, VoiceErrorResponse{Error: errResp}, err
}
}
return ModifyCallResponse{}, VoiceErrorResponse{Error: response}, err
} else {
// not a whole lot to return as it's a 204, this branch is success
return ModifyCallResponse{Status: "0"}, VoiceErrorResponse{}, nil
}
}
type PlayAudioOpts struct {
Loop string
Level int
}
// PlayAudioStream starts an audio file from a URL playing in a call
func (client *VoiceClient) PlayAudioStream(uuid string, streamUrl string, opts PlayAudioOpts) (voice.StartStreamResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
streamOpts := voice.StartStreamRequest{StreamUrl: []string{streamUrl}}
ctx := context.Background()
response, _, err := voiceClient.StreamAudioApi.StartStream(ctx, uuid, streamOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return response, VoiceErrorResponse{Error: errResp}, err
}
}
return response, VoiceErrorResponse{}, err
}
// StopAudioStream stops the currently-playing audio stream
func (client *VoiceClient) StopAudioStream(uuid string) (voice.StopStreamResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
ctx := context.Background()
response, _, err := voiceClient.StreamAudioApi.StopStream(ctx, uuid)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return response, VoiceErrorResponse{Error: errResp}, err
}
}
return response, VoiceErrorResponse{}, err
}
type PlayTtsOpts struct {
Text string
Loop int32
Level string
VoiceName string
}
// PlayTts starts playing TTS into the call
func (client *VoiceClient) PlayTts(uuid string, text string, opts PlayTtsOpts) (voice.StartTalkResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
req_vars := voice.StartTalkRequest{Text: text}
if opts.Loop != 0 {
req_vars.Loop = opts.Loop
}
if opts.Level != "" {
req_vars.Level = opts.Level
}
if opts.VoiceName != "" {
req_vars.VoiceName = voice.VoiceName(opts.VoiceName)
}
talkOpts := voice.StartTalkOpts{StartTalkRequest: optional.NewInterface(req_vars)}
ctx := context.Background()
response, _, err := voiceClient.PlayTTSApi.StartTalk(ctx, uuid, &talkOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return response, VoiceErrorResponse{Error: errResp}, err
}
}
return response, VoiceErrorResponse{}, err
}
// StopTts stops the current TTS from playing
func (client *VoiceClient) StopTts(uuid string) (voice.StopTalkResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
ctx := context.Background()
response, _, err := voiceClient.PlayTTSApi.StopTalk(ctx, uuid)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return response, VoiceErrorResponse{Error: errResp}, err
}
}
return response, VoiceErrorResponse{}, err
}
// PlayDTMF starts playing a string of DTMF digits into the call
func (client *VoiceClient) PlayDtmf(uuid string, dtmf string) (voice.DtmfResponse, VoiceErrorResponse, error) {
voiceClient := voice.NewAPIClient(client.Config)
dtmfOpts := voice.DtmfRequest{Digits: dtmf}
ctx := context.Background()
response, _, err := voiceClient.PlayDTMFApi.StartDTMF(ctx, uuid, dtmfOpts)
if err != nil {
e := err.(voice.GenericOpenAPIError)
data := e.Body()
var errResp VoiceErrorGeneralResponse
jsonErr := json.Unmarshal(data, &errResp)
if jsonErr == nil {
return response, VoiceErrorResponse{Error: errResp}, err
}
}
return response, VoiceErrorResponse{}, err
}