-
Notifications
You must be signed in to change notification settings - Fork 2
/
send.go
53 lines (46 loc) · 1.31 KB
/
send.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
package vtwilio
import (
"fmt"
"net/http"
"net/url"
"strings"
)
// SendMessage Sends a twilio message and returns the twilio message SID
func (v *VTwilio) SendMessage(message string, to string, opts ...SendOption) (*Message, error) {
if message == "" {
return nil, fmt.Errorf("must contain a message")
}
if to == "" {
return nil, fmt.Errorf("must contain a phone number to send the message to")
}
config := &sendConfiguration{}
for _, o := range opts {
o(config)
}
return v.sendMessage(message, to, config)
}
func (v *VTwilio) sendMessage(message, to string, config *sendConfiguration) (*Message, error) {
from := v.twilioNumber
if config.From != "" {
from = config.From
}
values := url.Values{}
values.Set("To", to)
values.Set("From", from)
values.Set("Body", message)
if config.MediaURL != "" {
values.Set("MediaUrl", config.MediaURL)
}
if config.CallbackURL != "" && config.CallbackMethod != "" {
values.Set("StatusCallback", config.CallbackURL)
values.Set("StatusCallbackMethod", config.CallbackMethod.String())
}
en := values.Encode()
urlStr := fmt.Sprintf("%s%s%s.json", v.baseAPI, v.accountSID, messageAPI)
req, err := http.NewRequest("POST", urlStr, strings.NewReader(en))
if err != nil {
return nil, err
}
setUpRequest(req, v.accountSID, v.authToken)
return handleMessage(req)
}