-
Notifications
You must be signed in to change notification settings - Fork 0
/
twilio_send.go
executable file
·170 lines (145 loc) · 5.1 KB
/
twilio_send.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package campid
import (
"context"
"fmt"
"github.com/influx6/npkg"
"github.com/influx6/npkg/njson"
"github.com/influx6/npkg/nerror"
"github.com/influx6/npkg/ntrace"
openTracing "github.com/opentracing/opentracing-go"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/twilio/twilio-go"
openapi "github.com/twilio/twilio-go/rest/api/v2010"
)
var _ TelCode = (*TwilioTelCode)(nil)
type TwilioTelCode struct {
Logger njson.Logger
Template CodeTemplate
AccountSid string
AuthToken string
Region string
Edge string
From string
}
func NewTwilioTel(logger njson.Logger, template CodeTemplate, accountSid, authToken string, from string) *TwilioTelCode {
return NewTwilioTelWithRegionAndEdge(logger, template, accountSid, authToken, "", "", from)
}
func NewTwilioTelWithRegionAndEdge(logger njson.Logger, template CodeTemplate, accountSid, authToken string, region string, edge string, from string) *TwilioTelCode {
return &TwilioTelCode{
Logger: logger,
Template: template,
AccountSid: accountSid,
AuthToken: authToken,
Region: region,
Edge: edge,
From: from,
}
}
func (tw *TwilioTelCode) SendToPhone(ctx context.Context, phoneNumber string, code string) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var client = twilio.NewRestClient(tw.AccountSid, tw.AuthToken)
if len(tw.Edge) != 0 {
client.SetEdge(tw.Edge)
}
if len(tw.Region) != 0 {
client.SetRegion(tw.Region)
}
var createMessage, createErr = tw.Template.Format(code)
if createErr != nil {
return nerror.WrapOnly(createErr)
}
var msg openapi.CreateMessageParams
msg.SetBody(createMessage)
msg.SetTo(phoneNumber)
msg.SetFrom(tw.From)
var res, sendErr = client.ApiV2010.CreateMessage(&msg)
if sendErr != nil {
return nerror.Wrap(sendErr, "Failed to send sms to %q", phoneNumber)
}
tw.Logger.Log(njson.MJSON("Send sms message with twilio", func(encoder npkg.Encoder) {
encoder.String("phoneNumber", phoneNumber)
encoder.Int("_level", int(npkg.INFO))
encoder.ObjectFor("message", func(msgEncoder npkg.ObjectEncoder) {
msgEncoder.String("status", *res.Status)
msgEncoder.String("to", *res.To)
msgEncoder.String("from", *res.From)
msgEncoder.String("uri", *res.Uri)
msgEncoder.String("accountSid", *res.AccountSid)
msgEncoder.String("apiVersion", *res.ApiVersion)
msgEncoder.String("body", *res.Body)
msgEncoder.String("dateCreated", *res.DateCreated)
msgEncoder.String("dateSent", *res.DateSent)
msgEncoder.String("dateUpdated", *res.DateUpdated)
msgEncoder.String("direction", *res.Direction)
msgEncoder.Int32("errorCode", *res.ErrorCode)
msgEncoder.String("errorMessage", *res.ErrorMessage)
msgEncoder.String("from", *res.From)
msgEncoder.String("messagingServiceSid", *res.MessagingServiceSid)
msgEncoder.String("numMedia", *res.NumMedia)
msgEncoder.String("numSegments", *res.NumSegments)
msgEncoder.String("price", *res.Price)
msgEncoder.String("priceUnit", *res.PriceUnit)
})
}))
if 400 <= *res.ErrorCode && *res.ErrorCode <= 503 {
return nerror.New("failed to deliver message").
Add("errorCode", fmt.Sprintf("%d", *res.ErrorCode)).
Add("errorMessage", *res.ErrorMessage)
}
return nil
}
type TwilioEmailCode struct {
Logger njson.Logger
Template CodeTemplate
APIKey string
FromAddr string
Subject string
}
func NewTwilioEmailCode(logger njson.Logger, template CodeTemplate, apiKey string, fromAddr string, subject string) *TwilioEmailCode {
return &TwilioEmailCode{
Logger: logger,
Template: template,
APIKey: apiKey,
FromAddr: fromAddr,
Subject: subject,
}
}
func (tw *TwilioEmailCode) SendToEmail(ctx context.Context, toAddr string, code string) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var createMessage, createErr = tw.Template.Format(code)
if createErr != nil {
return nerror.WrapOnly(createErr)
}
var toEmail = mail.NewEmail("to", toAddr)
var fromEmail = mail.NewEmail("from", tw.FromAddr)
var message = mail.NewSingleEmail(fromEmail, tw.Subject, toEmail, "", createMessage)
var client = sendgrid.NewSendClient(tw.APIKey)
var sendResponse, sendErr = client.Send(message)
if sendErr != nil {
tw.Logger.Log(njson.MJSON("sent email message with twilio", func(encoder npkg.Encoder) {
encoder.Error("error", sendErr)
encoder.String("fromEmail", tw.FromAddr)
encoder.String("authCode", code)
encoder.String("toEmail", toAddr)
encoder.Int("_level", int(npkg.INFO))
}))
return nerror.Wrap(sendErr, "failed to send verification code")
}
tw.Logger.Log(njson.MJSON("sent email message with twilio", func(encoder npkg.Encoder) {
encoder.String("fromEmail", tw.FromAddr)
encoder.String("authCode", code)
encoder.String("toEmail", toAddr)
encoder.Int("_level", int(npkg.INFO))
encoder.String("response_body", sendResponse.Body)
encoder.Int("response_statusCode", sendResponse.StatusCode)
encoder.Object("response_headers", npkg.EncodableStringListMap(sendResponse.Headers))
}))
return nil
}