-
Notifications
You must be signed in to change notification settings - Fork 135
/
twiml_sms.go
107 lines (92 loc) · 2.47 KB
/
twiml_sms.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
package gotwilio
import (
"encoding/xml"
"errors"
"strings"
)
const (
// opt-out keywords
SMSKeywordEnd = "end"
SMSKeywordQuit = "quit"
SMSKeywordStop = "stop"
SMSKeywordCancel = "cancel"
SMSKeywordStopAll = "stopall"
SMSKeywordUnsubscribe = "unsubscribe"
// opt-in keywords
SMSKeywordYes = "yes"
SMSKeywordStart = "start"
SMSKeywordUnstop = "unstop"
)
var (
// SMSOptOutKeywords Twilio support for opt-out keywords
SMSOptOutKeywords = []string{
SMSKeywordEnd,
SMSKeywordQuit,
SMSKeywordStop,
SMSKeywordCancel,
SMSKeywordStopAll,
SMSKeywordUnsubscribe,
}
// SMSOptInKeywords Twilio support for opt-in keywords
SMSOptInKeywords = []string{
SMSKeywordYes,
SMSKeywordStart,
SMSKeywordUnstop,
}
)
// IsSMSOptOutKeyword check if given keyword is an opt-out keyword supported by Twilio
func IsSMSOptOutKeyword(s string) bool {
s = strings.ToLower(s)
for _, k := range SMSOptOutKeywords {
if k == s {
return true
}
}
return false
}
// IsSMSOptInKeyword check if given keyword is an opt-in keyword supported by Twilio
func IsSMSOptInKeyword(s string) bool {
s = strings.ToLower(s)
for _, k := range SMSOptInKeywords {
if k == s {
return true
}
}
return false
}
// MessagingResponse Twilio's TWiML sms response
type MessagingResponse struct {
XMLName xml.Name `xml:"Response"`
Messages []*TWiMLSmsMessage `xml:"Message"`
}
// TWiMLSmsMessage response content
type TWiMLSmsMessage struct {
Message string `xml:",chardata"`
// nouns
Body *string `xml:"Body,omitempty"`
Media *string `xml:"Media,omitempty"`
Redirect *string `xml:"Redirect,omitempty"`
// verbs - xml attributes
Action *string `xml:"Action,attr,omitempty"`
Method *string `xml:"Method,attr,omitempty"`
}
// Message add message to TMiML response
func (r *MessagingResponse) Message(msg *TWiMLSmsMessage) (*MessagingResponse, error) {
if (msg.Body != nil || msg.Media != nil) && (msg.Action != nil || msg.Method != nil) {
return r, errors.New("can't nest verbs within Message and can't net Message in any other verb")
}
// twilio doesn't allow message when body is set
if msg.Body != nil && msg.Message != "" {
msg.Message = ""
}
r.Messages = append(r.Messages, msg)
return r, nil
}
// TWiMLSmsRender render XML response to send to Twilio
func (r *MessagingResponse) TWiMLSmsRender() (string, error) {
output, err := xml.MarshalIndent(r, " ", " ")
if err != nil {
return "", err
}
return xml.Header + string(output), nil
}