-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio.go
50 lines (37 loc) · 970 Bytes
/
twilio.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
package main
import (
"bytes"
"errors"
"net/http"
"net/url"
log "github.com/Sirupsen/logrus"
)
func (tc *TwilioClient) SensSMS(to, body string) error {
apiUrl := "https://api.twilio.com/2010-04-01/Accounts/" + tc.AccountSID + "/Messages.json"
v := url.Values{}
v.Set("From", tc.FromNumber)
v.Set("To", to)
v.Set("Body", body)
req, err := http.NewRequest("POST", apiUrl, bytes.NewBufferString(v.Encode()))
if err != nil {
return err
}
req.SetBasicAuth(tc.AccountSID, tc.AuthToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 201 {
log.WithFields(log.Fields{
"status": resp.StatusCode,
"to": to,
}).Error("Error while sending SMS")
return errors.New("Unable to send SMS to " + to)
}
log.WithFields(log.Fields{
"to": to,
}).Info("SMS sent")
return nil
}