-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.go
38 lines (30 loc) · 852 Bytes
/
sms.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
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
s "strings"
)
func sendSMS(accountSID, authToken, from, to, message string) error {
endpoint := "https://api.twilio.com/2010-04-01/Accounts/" + accountSID + "/Messages.json"
params := url.Values{}
params.Set("To", to)
params.Set("From", from)
params.Set("Body", message)
paramsReader := *s.NewReader(params.Encode())
client := &http.Client{}
req, _ := http.NewRequest("POST", endpoint, ¶msReader)
req.SetBasicAuth(accountSID, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
err = errors.New(fmt.Sprintf("http status error: %d", res.StatusCode))
return err
}
return nil
}