mirror of
https://github.com/multipleof4/ntfy.git
synced 2026-10-17 00:47:54 +00:00
Make twilioClient
This commit is contained in:
+4
-2
@@ -58,6 +58,7 @@ type Server struct {
|
|||||||
topics map[string]*topic
|
topics map[string]*topic
|
||||||
visitors map[string]*visitor // ip:<ip> or user:<user>
|
visitors map[string]*visitor // ip:<ip> or user:<user>
|
||||||
firebaseClient *firebaseClient
|
firebaseClient *firebaseClient
|
||||||
|
twilio *twilioClient
|
||||||
messages int64 // Total number of messages (persisted if messageCache enabled)
|
messages int64 // Total number of messages (persisted if messageCache enabled)
|
||||||
messagesHistory []int64 // Last n values of the messages counter, used to determine rate
|
messagesHistory []int64 // Last n values of the messages counter, used to determine rate
|
||||||
userManager *user.Manager // Might be nil!
|
userManager *user.Manager // Might be nil!
|
||||||
@@ -295,6 +296,7 @@ func New(conf *Config) (*Server, error) {
|
|||||||
webPush: wp,
|
webPush: wp,
|
||||||
attachment: attachmentStore,
|
attachment: attachmentStore,
|
||||||
firebaseClient: firebaseClient,
|
firebaseClient: firebaseClient,
|
||||||
|
twilio: newTwilioClient(conf, userManager),
|
||||||
mailer: sender,
|
mailer: sender,
|
||||||
topics: topics,
|
topics: topics,
|
||||||
userManager: userManager,
|
userManager: userManager,
|
||||||
@@ -838,7 +840,7 @@ func (s *Server) handlePublishInternal(r *http.Request, v *visitor) (*model.Mess
|
|||||||
}
|
}
|
||||||
if call != "" {
|
if call != "" {
|
||||||
var httpErr *errHTTP
|
var httpErr *errHTTP
|
||||||
call, httpErr = s.convertPhoneNumber(v.User(), call)
|
call, httpErr = s.twilio.convertPhoneNumber(v.User(), call)
|
||||||
if httpErr != nil {
|
if httpErr != nil {
|
||||||
return nil, httpErr.With(t)
|
return nil, httpErr.With(t)
|
||||||
} else if !vrate.CallAllowed() {
|
} else if !vrate.CallAllowed() {
|
||||||
@@ -887,7 +889,7 @@ func (s *Server) handlePublishInternal(r *http.Request, v *visitor) (*model.Mess
|
|||||||
go s.sendEmail(v, m, email)
|
go s.sendEmail(v, m, email)
|
||||||
}
|
}
|
||||||
if s.config.TwilioAccount != "" && call != "" {
|
if s.config.TwilioAccount != "" && call != "" {
|
||||||
go s.callPhone(v, r, m, call)
|
go s.twilio.callPhone(v, r, m, call)
|
||||||
}
|
}
|
||||||
if s.config.UpstreamBaseURL != "" && !unifiedpush { // UP messages are not sent to upstream
|
if s.config.UpstreamBaseURL != "" && !unifiedpush { // UP messages are not sent to upstream
|
||||||
go s.forwardPollRequest(v, m)
|
go s.forwardPollRequest(v, m)
|
||||||
|
|||||||
@@ -613,7 +613,7 @@ func (s *Server) handleAccountPhoneNumberVerify(w http.ResponseWriter, r *http.R
|
|||||||
}
|
}
|
||||||
// Actually add the unverified number, and send verification
|
// Actually add the unverified number, and send verification
|
||||||
logvr(v, r).Tag(tagAccount).Field("phone_number", req.Number).Debug("Sending phone number verification")
|
logvr(v, r).Tag(tagAccount).Field("phone_number", req.Number).Debug("Sending phone number verification")
|
||||||
if err := s.verifyPhoneNumber(v, r, req.Number, req.Channel); err != nil {
|
if err := s.twilio.verifyPhoneNumber(v, r, req.Number, req.Channel); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.writeJSON(w, newSuccessResponse())
|
return s.writeJSON(w, newSuccessResponse())
|
||||||
@@ -628,7 +628,7 @@ func (s *Server) handleAccountPhoneNumberAdd(w http.ResponseWriter, r *http.Requ
|
|||||||
if !phoneNumberRegex.MatchString(req.Number) {
|
if !phoneNumberRegex.MatchString(req.Number) {
|
||||||
return errHTTPBadRequestPhoneNumberInvalid
|
return errHTTPBadRequestPhoneNumberInvalid
|
||||||
}
|
}
|
||||||
if err := s.verifyPhoneNumberCheck(v, r, req.Number, req.Code); err != nil {
|
if err := s.twilio.verifyPhoneNumberCheck(v, r, req.Number, req.Code); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logvr(v, r).Tag(tagAccount).Field("phone_number", req.Number).Debug("Adding phone number as verified")
|
logvr(v, r).Tag(tagAccount).Field("phone_number", req.Number).Debug("Adding phone number as verified")
|
||||||
|
|||||||
+34
-19
@@ -16,6 +16,21 @@ import (
|
|||||||
"heckel.io/ntfy/v2/util"
|
"heckel.io/ntfy/v2/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// twilioClient talks to the Twilio API to make phone calls (for the "Call" feature) and to verify
|
||||||
|
// phone numbers. It holds the Twilio configuration and the user manager (used to look up a user's
|
||||||
|
// verified phone numbers), so that this functionality is decoupled from the main Server.
|
||||||
|
type twilioClient struct {
|
||||||
|
config *Config
|
||||||
|
userManager *user.Manager // May be nil!
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTwilioClient(conf *Config, userManager *user.Manager) *twilioClient {
|
||||||
|
return &twilioClient{
|
||||||
|
config: conf,
|
||||||
|
userManager: userManager,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// defaultTwilioCallFormatTemplate is the default TwiML template used for Twilio calls.
|
// defaultTwilioCallFormatTemplate is the default TwiML template used for Twilio calls.
|
||||||
// It can be overridden in the server configuration's twilio-call-format field.
|
// It can be overridden in the server configuration's twilio-call-format field.
|
||||||
//
|
//
|
||||||
@@ -52,11 +67,11 @@ type twilioCallData struct {
|
|||||||
// convertPhoneNumber checks if the given phone number is verified for the given user, and if so, returns the verified
|
// convertPhoneNumber checks if the given phone number is verified for the given user, and if so, returns the verified
|
||||||
// phone number. It also converts a boolean string ("yes", "1", "true") to the first verified phone number.
|
// phone number. It also converts a boolean string ("yes", "1", "true") to the first verified phone number.
|
||||||
// If the user is anonymous, it will return an error.
|
// If the user is anonymous, it will return an error.
|
||||||
func (s *Server) convertPhoneNumber(u *user.User, phoneNumber string) (string, *errHTTP) {
|
func (c *twilioClient) convertPhoneNumber(u *user.User, phoneNumber string) (string, *errHTTP) {
|
||||||
if u == nil {
|
if u == nil {
|
||||||
return "", errHTTPBadRequestAnonymousCallsNotAllowed
|
return "", errHTTPBadRequestAnonymousCallsNotAllowed
|
||||||
}
|
}
|
||||||
phoneNumbers, err := s.userManager.PhoneNumbers(u.ID)
|
phoneNumbers, err := c.userManager.PhoneNumbers(u.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errHTTPInternalError
|
return "", errHTTPInternalError
|
||||||
} else if len(phoneNumbers) == 0 {
|
} else if len(phoneNumbers) == 0 {
|
||||||
@@ -72,14 +87,14 @@ func (s *Server) convertPhoneNumber(u *user.User, phoneNumber string) (string, *
|
|||||||
|
|
||||||
// callPhone calls the Twilio API to make a phone call to the given phone number, using the given message.
|
// callPhone calls the Twilio API to make a phone call to the given phone number, using the given message.
|
||||||
// Failures will be logged, but not returned to the caller.
|
// Failures will be logged, but not returned to the caller.
|
||||||
func (s *Server) callPhone(v *visitor, r *http.Request, m *model.Message, to string) {
|
func (c *twilioClient) callPhone(v *visitor, r *http.Request, m *model.Message, to string) {
|
||||||
u, sender := v.User(), m.Sender.String()
|
u, sender := v.User(), m.Sender.String()
|
||||||
if u != nil {
|
if u != nil {
|
||||||
sender = u.Name
|
sender = u.Name
|
||||||
}
|
}
|
||||||
tmpl := defaultTwilioCallFormatTemplate
|
tmpl := defaultTwilioCallFormatTemplate
|
||||||
if s.config.TwilioCallFormat != nil {
|
if c.config.TwilioCallFormat != nil {
|
||||||
tmpl = s.config.TwilioCallFormat
|
tmpl = c.config.TwilioCallFormat
|
||||||
}
|
}
|
||||||
tags := make([]string, len(m.Tags))
|
tags := make([]string, len(m.Tags))
|
||||||
for i, tag := range m.Tags {
|
for i, tag := range m.Tags {
|
||||||
@@ -101,11 +116,11 @@ func (s *Server) callPhone(v *visitor, r *http.Request, m *model.Message, to str
|
|||||||
}
|
}
|
||||||
body := bodyBuf.String()
|
body := bodyBuf.String()
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("From", s.config.TwilioPhoneNumber)
|
data.Set("From", c.config.TwilioPhoneNumber)
|
||||||
data.Set("To", to)
|
data.Set("To", to)
|
||||||
data.Set("Twiml", body)
|
data.Set("Twiml", body)
|
||||||
ev := logvrm(v, r, m).Tag(tagTwilio).Field("twilio_to", to).FieldIf("twilio_body", body, log.TraceLevel).Debug("Sending Twilio request")
|
ev := logvrm(v, r, m).Tag(tagTwilio).Field("twilio_to", to).FieldIf("twilio_body", body, log.TraceLevel).Debug("Sending Twilio request")
|
||||||
response, err := s.callPhoneInternal(data)
|
response, err := c.callPhoneInternal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ev.Field("twilio_response", response).Err(err).Warn("Error sending Twilio request")
|
ev.Field("twilio_response", response).Err(err).Warn("Error sending Twilio request")
|
||||||
minc(metricCallsMadeFailure)
|
minc(metricCallsMadeFailure)
|
||||||
@@ -115,15 +130,15 @@ func (s *Server) callPhone(v *visitor, r *http.Request, m *model.Message, to str
|
|||||||
minc(metricCallsMadeSuccess)
|
minc(metricCallsMadeSuccess)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) callPhoneInternal(data url.Values) (string, error) {
|
func (c *twilioClient) callPhoneInternal(data url.Values) (string, error) {
|
||||||
requestURL := fmt.Sprintf("%s/2010-04-01/Accounts/%s/Calls.json", s.config.TwilioCallsBaseURL, s.config.TwilioAccount)
|
requestURL := fmt.Sprintf("%s/2010-04-01/Accounts/%s/Calls.json", c.config.TwilioCallsBaseURL, c.config.TwilioAccount)
|
||||||
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "ntfy/"+s.config.BuildVersion)
|
req.Header.Set("User-Agent", "ntfy/"+c.config.BuildVersion)
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
|
req.Header.Set("Authorization", util.BasicAuth(c.config.TwilioAccount, c.config.TwilioAuthToken))
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -135,19 +150,19 @@ func (s *Server) callPhoneInternal(data url.Values) (string, error) {
|
|||||||
return string(response), nil
|
return string(response), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) verifyPhoneNumber(v *visitor, r *http.Request, phoneNumber, channel string) error {
|
func (c *twilioClient) verifyPhoneNumber(v *visitor, r *http.Request, phoneNumber, channel string) error {
|
||||||
ev := logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Field("twilio_channel", channel).Debug("Sending phone verification")
|
ev := logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Field("twilio_channel", channel).Debug("Sending phone verification")
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("To", phoneNumber)
|
data.Set("To", phoneNumber)
|
||||||
data.Set("Channel", channel)
|
data.Set("Channel", channel)
|
||||||
requestURL := fmt.Sprintf("%s/v2/Services/%s/Verifications", s.config.TwilioVerifyBaseURL, s.config.TwilioVerifyService)
|
requestURL := fmt.Sprintf("%s/v2/Services/%s/Verifications", c.config.TwilioVerifyBaseURL, c.config.TwilioVerifyService)
|
||||||
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "ntfy/"+s.config.BuildVersion)
|
req.Header.Set("User-Agent", "ntfy/"+c.config.BuildVersion)
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
|
req.Header.Set("Authorization", util.BasicAuth(c.config.TwilioAccount, c.config.TwilioAuthToken))
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -161,19 +176,19 @@ func (s *Server) verifyPhoneNumber(v *visitor, r *http.Request, phoneNumber, cha
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) verifyPhoneNumberCheck(v *visitor, r *http.Request, phoneNumber, code string) error {
|
func (c *twilioClient) verifyPhoneNumberCheck(v *visitor, r *http.Request, phoneNumber, code string) error {
|
||||||
ev := logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Debug("Checking phone verification")
|
ev := logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Debug("Checking phone verification")
|
||||||
data := url.Values{}
|
data := url.Values{}
|
||||||
data.Set("To", phoneNumber)
|
data.Set("To", phoneNumber)
|
||||||
data.Set("Code", code)
|
data.Set("Code", code)
|
||||||
requestURL := fmt.Sprintf("%s/v2/Services/%s/VerificationCheck", s.config.TwilioVerifyBaseURL, s.config.TwilioVerifyService)
|
requestURL := fmt.Sprintf("%s/v2/Services/%s/VerificationCheck", c.config.TwilioVerifyBaseURL, c.config.TwilioVerifyService)
|
||||||
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "ntfy/"+s.config.BuildVersion)
|
req.Header.Set("User-Agent", "ntfy/"+c.config.BuildVersion)
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
|
req.Header.Set("Authorization", util.BasicAuth(c.config.TwilioAccount, c.config.TwilioAuthToken))
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user