/
twilio.go
93 lines (74 loc) · 1.94 KB
/
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Package twilio exposes Twilio's REST API.
package twilio
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
)
// client is a consumer of the Twilio API.
type client struct {
baseURL string
accountSID string
authToken string
smsSender string
}
// SMS sends an SMS message to a phone number.
func (c *client) SMS(ctx context.Context, phoneNumber string, message string) error {
url := fmt.Sprintf(
"%s/Accounts/%s/Messages.json",
c.baseURL,
c.accountSID,
)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
smsTemplate := map[string]string{
"To": phoneNumber,
"From": c.smsSender,
"Body": message,
}
if err := writeFields(writer, smsTemplate); err != nil {
return err
}
if err := writer.Close(); err != nil {
return fmt.Errorf("failed to close writer: %w", err)
}
resp, err := c.request(ctx, url, body, writer)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
rBody, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("expected status %v, got %v: %s",
http.StatusCreated, resp.StatusCode, string(rBody))
}
return nil
}
func (c *client) request(ctx context.Context, url string, body io.Reader, writer *multipart.Writer) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "POST", url, body)
if err != nil {
return nil, fmt.Errorf("cannot create HTTP request: %w", err)
}
req.SetBasicAuth(c.accountSID, c.authToken)
req.Header.Set("Content-Type", writer.FormDataContentType())
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send HTTP request: %w", err)
}
return resp, nil
}
func writeFields(writer *multipart.Writer, fields map[string]string) error {
var err error
for k, v := range fields {
err = writer.WriteField(k, v)
if err != nil {
return fmt.Errorf("failed to write %s to %s: %w", v, k, err)
}
}
return nil
}