| 1 |
package twilio |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/xml" |
| 6 |
"errors" |
| 7 |
"text/template" |
| 8 |
) |
| 9 |
|
| 10 |
// ErrVerificationExpired is returned by CheckVerify if the verification code has |
| 11 |
// expired, or if it never existed in the first place |
| 12 |
var ErrVerificationExpired = errors.New("phone number verification expired or does not exist") |
| 13 |
|
| 14 |
// Config holds the Twilio configuration for the client |
| 15 |
type Config struct { |
| 16 |
Account string // Twilio account SID, e.g. AC123... |
| 17 |
AuthToken string // Twilio auth token |
| 18 |
PhoneNumber string // Twilio number to use for outgoing calls |
| 19 |
CallsBaseURL string // Base URL of the Twilio Calls API |
| 20 |
VerifyBaseURL string // Base URL of the Twilio Verify API |
| 21 |
VerifyService string // Twilio Verify service ID, e.g. VA123... |
| 22 |
CallFormat *template.Template // TwiML template for calls; if nil, defaultCallFormatTemplate is used |
| 23 |
BuildVersion string // ntfy version, used for the User-Agent header |
| 24 |
} |
| 25 |
|
| 26 |
// defaultCallFormatTemplate is the default TwiML template used for Twilio calls. |
| 27 |
// It can be overridden in the server configuration's twilio-call-format field. |
| 28 |
// |
| 29 |
// The format uses Go template syntax with the following fields: |
| 30 |
// {{.Topic}}, {{.Title}}, {{.Message}}, {{.Priority}}, {{.Tags}}, {{.Sender}} |
| 31 |
// String fields are automatically XML-escaped. |
| 32 |
var defaultCallFormatTemplate = template.Must(template.New("twiml").Parse(` |
| 33 |
<Response> |
| 34 |
<Pause length="1"/> |
| 35 |
<Say loop="3"> |
| 36 |
You have a message from notify on topic {{.Topic}}. Message: |
| 37 |
<break time="1s"/> |
| 38 |
{{.Message}} |
| 39 |
<break time="1s"/> |
| 40 |
End of message. |
| 41 |
<break time="1s"/> |
| 42 |
This message was sent by user {{.Sender}}. It will be repeated three times. |
| 43 |
To unsubscribe from calls like this, remove your phone number in the notify web app. |
| 44 |
<break time="3s"/> |
| 45 |
</Say> |
| 46 |
<Say>Goodbye.</Say> |
| 47 |
</Response>`)) |
| 48 |
|
| 49 |
// CallData holds the data passed to the Twilio call format template. String fields are |
| 50 |
// XML-escaped before the template is executed, so callers pass them unescaped. |
| 51 |
type CallData struct { |
| 52 |
Topic string |
| 53 |
Title string |
| 54 |
Message string |
| 55 |
Priority int |
| 56 |
Tags []string |
| 57 |
Sender string |
| 58 |
} |
| 59 |
|
| 60 |
// escaped returns a copy of the call data with all string fields XML-escaped |
| 61 |
func (d *CallData) escaped() *CallData { |
| 62 |
tags := make([]string, len(d.Tags)) |
| 63 |
for i, tag := range d.Tags { |
| 64 |
tags[i] = xmlEscapeText(tag) |
| 65 |
} |
| 66 |
return &CallData{ |
| 67 |
Topic: xmlEscapeText(d.Topic), |
| 68 |
Title: xmlEscapeText(d.Title), |
| 69 |
Message: xmlEscapeText(d.Message), |
| 70 |
Priority: d.Priority, |
| 71 |
Tags: tags, |
| 72 |
Sender: xmlEscapeText(d.Sender), |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func xmlEscapeText(text string) string { |
| 77 |
var buf bytes.Buffer |
| 78 |
_ = xml.EscapeText(&buf, []byte(text)) |
| 79 |
return buf.String() |
| 80 |
} |