This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
forked from Vonage/vonage-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calls.go
107 lines (86 loc) · 3.93 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
package nexmo
import (
"fmt"
"net/http"
"github.com/dghubble/sling"
)
func (c *CallErrorResponse) Error() string {
return fmt.Sprintf("%s: %s", c.ErrorTitle, c.Type)
}
// CreateCall starts a voice call, configured using the provided CreateCallRequest.
func (c *CallService) CreateCall(request CreateCallRequest) (*CreateCallResponse, *http.Response, error) {
sling := c.sling.New().Post("").BodyJSON(request)
callResponse := new(CreateCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// SearchCalls returns information about calls matching the filter in the provided SearchCallsRequest
func (c *CallService) SearchCalls(request SearchCallsRequest) (*SearchCallsResponse, *http.Response, error) {
sling := c.sling.New().Get("").QueryStruct(request)
callResponse := new(SearchCallsResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Get information about a current or past call by call ID
func (c *CallService) GetCallInfo(uuid string) (*CallInfo, *http.Response, error) {
sling := c.sling.New().Get(uuid)
callResponse := new(CallInfo)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Change the in-progress call by performing an action, such as hangup, transfer, mute, etc. See the API reference: https://developer.nexmo.com/api/voice#updateCall
func (c *CallService) ModifyCall(uuid string, request interface{}) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Put(uuid).BodyJSON(request)
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Start playing an audio file into a call
func (c *CallService) Stream(uuid string, request StreamRequest) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Put(fmt.Sprintf("%s/stream", uuid)).BodyJSON(request)
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Stop the audio stream from playing in a call
func (c *CallService) StopStream(uuid string) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Delete(fmt.Sprintf("%s/stream", uuid))
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Send text-to-speech into a call
func (c *CallService) Talk(uuid string, request TalkRequest) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Put(fmt.Sprintf("%s/talk", uuid)).BodyJSON(request)
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Stop the text-to-speech that is currently being sent into a call
func (c *CallService) StopTalk(uuid string) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Delete(fmt.Sprintf("%s/talk", uuid))
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
// Play DTMF tones into a call
func (c *CallService) SendDTMF(uuid string, request DTMFRequest) (*ModifyCallResponse, *http.Response, error) {
sling := c.sling.New().Put(fmt.Sprintf("%s/dtmf", uuid)).BodyJSON(request)
callResponse := new(ModifyCallResponse)
httpResponse, err := c.makeRequest(sling, callResponse)
return callResponse, httpResponse, err
}
func (c *CallService) makeRequest(s *sling.Sling, successV interface{}) (*http.Response, error) {
errorV := new(CallErrorResponse)
if err := c.authSet.ApplyJWT(s); err != nil {
return nil, fmt.Errorf("%s - error applying JWT", err)
}
httpResponse, err := s.Receive(successV, errorV)
if err != nil {
return httpResponse, fmt.Errorf("%s - error receiving data from server", err)
}
if errorV.Type != "" {
return httpResponse, errorV
}
return httpResponse, nil
}