-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
75 lines (57 loc) · 1.47 KB
/
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
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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type smsSender interface {
SendSMS(to []phoneNumber, from phoneNumber, body io.Reader) error
}
type twilioSmsSender struct {
baseURL string
sid string
token string
}
func (t twilioSmsSender) constructRequest(to, from, msgBody string) (*http.Request, error) {
twilioURL := t.baseURL + t.sid + "/Messages.json"
form := url.Values{}
form.Add("To", string(to))
form.Add("From", string(from))
form.Add("Body", string(msgBody))
req, err := http.NewRequest(http.MethodPost, twilioURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, err
}
req.SetBasicAuth(t.sid, t.token)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
func (t twilioSmsSender) SendSMS(to []phoneNumber, from phoneNumber, body io.Reader) error {
msgBody, err := ioutil.ReadAll(body)
if err != nil {
return err
}
for _, number := range to {
req, err := t.constructRequest(string(number), string(from), string(msgBody))
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
respBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode >= 300 || resp.StatusCode < 200 {
return fmt.Errorf("sms: Error sending sms, status code %d, body %v", resp.StatusCode, string(respBody))
}
}
return nil
}