github.com/subosito/twilio@v0.0.1/call.go (about) 1 package twilio 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "net/http" 8 "net/url" 9 "strconv" 10 ) 11 12 type CallResponse struct { 13 Sid string `json:"sid"` 14 DateCreated Timestamp `json:"date_created,omitempty"` 15 DateUpdated Timestamp `json:"date_updated,omitempty"` 16 ParentCallSid string `json:"parent_call_sid"` 17 AccountSid string `json:"account_sid"` 18 To string `json:"to"` 19 ToFormatted string `json:"to_formatted"` 20 From string `json:"from"` 21 FromFormatted string `json:"from_formatted"` 22 PhoneNumberSid string `json:"phone_number_sid"` 23 Status string `json:"status"` 24 StartTime Timestamp `json:"start_time,omitempty"` 25 EndTime Timestamp `json:"end_time,omitempty"` 26 Duration string `json:"duration,omitempty"` 27 Price Price `json:"price,omitempty"` 28 Direction string `json:"direction"` 29 AnsweredBy string `json:"answered_by,omitempty"` 30 ApiVersion string `json:"api_version"` 31 ForwardedFrom string `json:"forwarded_from,omitempty"` 32 CallerName string `json:"caller_name,omitempty"` 33 Uri string `json:"uri"` 34 SubresourceUris struct { 35 Notifications string `json:"notifications"` 36 Recordings string `json:"recordings"` 37 } `json:"subresource_uris"` 38 } 39 40 func (t *Twilio) callEndpoint() string { 41 return fmt.Sprintf("%s/Accounts/%s/Calls", t.BaseUrl, t.AccountSid) 42 } 43 44 type CallParams struct { 45 // required, choose one of these 46 Url string 47 ApplicationSid string 48 49 Method string 50 FallbackUrl string 51 FallbackMethod string 52 StatusCallback string 53 StatusCallbackMethod string 54 SendDigits string 55 IfMachine string // Continue or Hangup 56 Timeout int 57 Record bool 58 } 59 60 func (p CallParams) urlValues() url.Values { 61 uv := url.Values{} 62 63 if p.Url != "" { 64 uv.Set("Url", p.Url) 65 uv.Set("Method", p.Method) 66 uv.Set("FallbackUrl", p.FallbackUrl) 67 uv.Set("FallbackMethod", p.FallbackMethod) 68 uv.Set("StatusCallback", p.StatusCallback) 69 uv.Set("StatusCallbackMethod", p.StatusCallbackMethod) 70 71 p.ApplicationSid = "" // reset 72 } 73 74 if p.ApplicationSid != "" { 75 uv.Set("ApplicationSid", p.ApplicationSid) 76 } 77 78 // set default timeout 79 if p.Timeout == 0 { 80 p.Timeout = 60 81 } 82 83 uv.Set("SendDigits", p.SendDigits) 84 uv.Set("IfMachine", p.IfMachine) 85 uv.Set("Timeout", strconv.Itoa(p.Timeout)) 86 uv.Set("Record", fmt.Sprintf("%t", p.Record)) 87 return uv 88 } 89 90 // Make a voice call. You need to set one of `Url` or `ApplicationSid` parameter on `CallParams` 91 func (t *Twilio) MakeCall(from, to string, p CallParams) (r *CallResponse, err error) { 92 endpoint := fmt.Sprintf("%s.%s", t.callEndpoint(), apiFormat) 93 94 if (p.Url == "") && (p.ApplicationSid == "") { 95 err := errors.New("One of the Url or ApplicationSid is required.") 96 return nil, err 97 } 98 99 params := p.urlValues() 100 params.Set("From", from) 101 params.Set("To", to) 102 103 b, status, err := t.request("POST", endpoint, params) 104 if err != nil { 105 return 106 } 107 108 if status != http.StatusCreated { 109 e := new(Exception) 110 err = json.Unmarshal(b, &e) 111 if err != nil { 112 return 113 } 114 115 return nil, e 116 } 117 118 err = json.Unmarshal(b, &r) 119 if err != nil { 120 return nil, err 121 } 122 123 return 124 } 125 126 type CallSipParams struct { 127 From string 128 SipAuthUsername string 129 SipAuthPassword string 130 } 131 132 func (p CallSipParams) urlValues() url.Values { 133 uv := url.Values{} 134 uv.Set("From", p.From) 135 uv.Set("SipAuthUsername", p.SipAuthUsername) 136 uv.Set("SipAuthPassword", p.SipAuthPassword) 137 return uv 138 } 139 140 func (t *Twilio) MakeSipCall(to string, p CallSipParams) (r *CallResponse, err error) { 141 endpoint := fmt.Sprintf("%s.%s", t.callEndpoint(), apiFormat) 142 143 params := p.urlValues() 144 params.Set("To", to) 145 146 b, status, err := t.request("POST", endpoint, params) 147 if err != nil { 148 return 149 } 150 151 if status != http.StatusOK { 152 e := new(Exception) 153 err = json.Unmarshal(b, &e) 154 if err != nil { 155 return 156 } 157 158 return nil, e 159 } 160 161 err = json.Unmarshal(b, &r) 162 if err != nil { 163 return nil, err 164 } 165 166 return 167 }