ntfy/server/server_twilio.go

180 lines
5.8 KiB
Go
Raw Normal View History

2023-05-05 20:22:54 +00:00
package server
import (
2023-05-06 00:14:46 +00:00
"bytes"
"encoding/xml"
2023-05-05 20:22:54 +00:00
"fmt"
2023-05-06 18:23:48 +00:00
"github.com/prometheus/client_golang/prometheus"
2023-05-05 20:22:54 +00:00
"heckel.io/ntfy/log"
"heckel.io/ntfy/user"
2023-05-05 20:22:54 +00:00
"heckel.io/ntfy/util"
"io"
"net/http"
"net/url"
"strings"
)
const (
2023-05-06 00:14:46 +00:00
twilioMessageFooterFormat = "This message was sent by %s via %s"
twilioCallEndpoint = "Calls.json"
twilioCallFormat = `
2023-05-05 20:22:54 +00:00
<Response>
<Pause length="1"/>
<Say>You have a message from notify on topic %s. Message:</Say>
<Pause length="1"/>
<Say>%s</Say>
<Pause length="1"/>
<Say>End message.</Say>
<Pause length="1"/>
<Say>%s</Say>
<Pause length="1"/>
</Response>`
)
func (s *Server) callPhone(v *visitor, r *http.Request, m *message, to string) {
body := fmt.Sprintf(twilioCallFormat, xmlEscapeText(m.Topic), xmlEscapeText(m.Message), xmlEscapeText(s.messageFooter(v.User(), m)))
2023-05-05 20:22:54 +00:00
data := url.Values{}
data.Set("From", s.config.TwilioFromNumber)
data.Set("To", to)
data.Set("Twiml", body)
2023-05-11 17:50:10 +00:00
s.twilioMessagingRequest(v, r, m, metricCallsMadeSuccess, metricCallsMadeFailure, twilioCallEndpoint, to, body, data)
2023-05-05 20:22:54 +00:00
}
2023-05-11 17:50:10 +00:00
func (s *Server) verifyPhone(v *visitor, r *http.Request, phoneNumber string) error {
logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Debug("Sending phone verification")
data := url.Values{}
data.Set("To", phoneNumber)
data.Set("Channel", "sms")
requestURL := fmt.Sprintf("%s/v2/Services/%s/Verifications", s.config.TwilioVerifyBaseURL, s.config.TwilioVerifyService)
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
response, err := io.ReadAll(resp.Body)
ev := logvr(v, r).Tag(tagTwilio)
if err != nil {
ev.Err(err).Warn("Error sending Twilio phone verification request")
return err
}
if ev.IsTrace() {
ev.Field("twilio_response", string(response)).Trace("Received successful Twilio phone verification response")
} else if ev.IsDebug() {
ev.Debug("Received successful Twilio phone verification response")
}
return nil
}
func (s *Server) checkVerifyPhone(v *visitor, r *http.Request, phoneNumber, code string) error {
logvr(v, r).Tag(tagTwilio).Field("twilio_to", phoneNumber).Debug("Checking phone verification")
data := url.Values{}
data.Set("To", phoneNumber)
data.Set("Code", code)
2023-05-13 00:01:12 +00:00
requestURL := fmt.Sprintf("%s/v2/Services/%s/VerificationCheck", s.config.TwilioVerifyBaseURL, s.config.TwilioVerifyService)
2023-05-11 17:50:10 +00:00
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
if err != nil {
return err
}
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
2023-05-13 00:01:12 +00:00
log.Fields(httpContext(req)).Field("http_body", data.Encode()).Info("Twilio call")
ev := logvr(v, r).
Tag(tagTwilio).
Field("twilio_to", phoneNumber)
2023-05-11 17:50:10 +00:00
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
} else if resp.StatusCode != http.StatusOK {
2023-05-13 00:01:12 +00:00
if ev.IsTrace() {
response, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
ev.Field("twilio_response", string(response))
}
ev.Warn("Twilio phone verification failed with status code %d", resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
return errHTTPGonePhoneVerificationExpired
}
return errHTTPInternalError
2023-05-11 17:50:10 +00:00
}
response, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if ev.IsTrace() {
ev.Field("twilio_response", string(response)).Trace("Received successful Twilio phone verification response")
} else if ev.IsDebug() {
ev.Debug("Received successful Twilio phone verification response")
}
return nil
}
func (s *Server) twilioMessagingRequest(v *visitor, r *http.Request, m *message, msuccess, mfailure prometheus.Counter, endpoint, to, body string, data url.Values) {
2023-05-05 20:22:54 +00:00
logContext := log.Context{
"twilio_from": s.config.TwilioFromNumber,
"twilio_to": to,
}
ev := logvrm(v, r, m).Tag(tagTwilio).Fields(logContext)
if ev.IsTrace() {
ev.Field("twilio_body", body).Trace("Sending Twilio request")
} else if ev.IsDebug() {
ev.Debug("Sending Twilio request")
}
2023-05-11 17:50:10 +00:00
response, err := s.performTwilioMessagingRequestInternal(endpoint, data)
2023-05-05 20:22:54 +00:00
if err != nil {
ev.
Field("twilio_body", body).
Field("twilio_response", response).
Err(err).
Warn("Error sending Twilio request")
2023-05-06 18:23:48 +00:00
minc(mfailure)
2023-05-05 20:22:54 +00:00
return
}
if ev.IsTrace() {
ev.Field("twilio_response", response).Trace("Received successful Twilio response")
} else if ev.IsDebug() {
ev.Debug("Received successful Twilio response")
}
2023-05-06 18:23:48 +00:00
minc(msuccess)
2023-05-05 20:22:54 +00:00
}
2023-05-11 17:50:10 +00:00
func (s *Server) performTwilioMessagingRequestInternal(endpoint string, data url.Values) (string, error) {
requestURL := fmt.Sprintf("%s/2010-04-01/Accounts/%s/%s", s.config.TwilioMessagingBaseURL, s.config.TwilioAccount, endpoint)
2023-05-05 20:22:54 +00:00
req, err := http.NewRequest(http.MethodPost, requestURL, strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
req.Header.Set("Authorization", util.BasicAuth(s.config.TwilioAccount, s.config.TwilioAuthToken))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
response, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(response), nil
}
func (s *Server) messageFooter(u *user.User, m *message) string { // u may be nil!
2023-05-05 20:22:54 +00:00
topicURL := s.config.BaseURL + "/" + m.Topic
sender := m.Sender.String()
if u != nil {
sender = fmt.Sprintf("%s (%s)", u.Name, m.Sender)
2023-05-05 20:22:54 +00:00
}
2023-05-06 00:14:46 +00:00
return fmt.Sprintf(twilioMessageFooterFormat, sender, util.ShortTopicURL(topicURL))
}
func xmlEscapeText(text string) string {
var buf bytes.Buffer
_ = xml.EscapeText(&buf, []byte(text))
return buf.String()
2023-05-05 20:22:54 +00:00
}