This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
twilio.go
147 lines (123 loc) · 4.09 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2020 the Exposure Notifications Verification Server authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sms
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/sethvargo/go-retry"
)
// TwilioMessagingServiceSidPrefix is the prefix for a 34 character messaging service identifier
const TwilioMessagingServiceSidPrefix = "MG"
var _ Provider = (*Twilio)(nil)
// Twilio sends messages via the Twilio API.
type Twilio struct {
client *http.Client
from string
}
// NewTwilio creates a new Twilio SMS sender with the given auth.
func NewTwilio(ctx context.Context, accountSid, authToken, from string) (Provider, error) {
transport := project.DefaultHTTPTransport()
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &twilioAuthRoundTripper{transport, accountSid, authToken},
}
return &Twilio{
client: client,
from: from,
}, nil
}
// SendSMS sends a message using the Twilio API.
func (p *Twilio) SendSMS(ctx context.Context, to, message string) error {
b := retry.NewFibonacci(250 * time.Millisecond)
b = retry.WithMaxRetries(4, b)
return retry.Do(ctx, b, func(ctx context.Context) error {
params := url.Values{}
params.Set("To", to)
if strings.HasPrefix(p.from, TwilioMessagingServiceSidPrefix) {
params.Set("MessagingServiceSid", p.from)
} else {
params.Set("From", p.from)
}
params.Set("Body", message)
body := strings.NewReader(params.Encode())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/Messages.json", body)
if err != nil {
return fmt.Errorf("failed to build request: %w", err)
}
req.Close = true
resp, err := p.client.Do(req)
if err != nil {
return retry.RetryableError(fmt.Errorf("failed to make request: %w", err))
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
if code := resp.StatusCode; code < http.StatusOK || code >= http.StatusMultipleChoices {
var terr TwilioError
if err := json.Unmarshal(respBody, &terr); err != nil {
return fmt.Errorf("twilio error %d: %s", code, respBody)
}
return &terr
}
return nil
})
}
// twilioAuthRoundTripper is an http.RoundTripper that updates the
// authentication and headers to match Twilio's API.
type twilioAuthRoundTripper struct {
transport *http.Transport
accountSid, authToken string
}
// RoundTrip implements http.RoundTripper.
func (rt *twilioAuthRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.URL = &url.URL{
Scheme: "https",
Host: "api.twilio.com",
Path: "/2010-04-01/Accounts/" + rt.accountSid + "/" + strings.Trim(r.URL.Path, "/"),
}
r.SetBasicAuth(rt.accountSid, rt.authToken)
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return rt.transport.RoundTrip(r)
}
// TwilioError represents an error returned from the Twilio API.
type TwilioError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *TwilioError) Error() string {
return e.Message
}
// IsSMSQueueFull returns if the given error is Twilio's message queue full error.
func IsSMSQueueFull(err error) bool {
return IsTwilioCode(err, 21611) // https://www.twilio.com/docs/api/errors/21611
}
// IsTwilioCode returns if the given error matches a Twilio error code.
func IsTwilioCode(err error, code int) bool {
var tErr *TwilioError
if errors.As(err, &tErr) {
return tErr.Code == code
}
return false
}