/
messaging_response.go
70 lines (58 loc) · 1.82 KB
/
messaging_response.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
package messaging
import (
"encoding/xml"
"github.com/RJPearson94/twilio-sdk-go/twiml/messaging/verbs"
)
// MessagingResponse provides the structure and functions for generation TwiML that can be used
// on Programmable SMS. See https://www.twilio.com/docs/sms/twiml for more details
type MessagingResponse struct {
XMLName xml.Name `xml:"Response"`
Children []interface{}
}
// New create a new instance of MessagingResponse
func New() *MessagingResponse {
return &MessagingResponse{
Children: make([]interface{}, 0),
}
}
func (m *MessagingResponse) Message(body *string) *verbs.Message {
message := &verbs.Message{
Text: body,
Children: make([]interface{}, 0),
}
m.Children = append(m.Children, message)
return message
}
func (m *MessagingResponse) MessageWithAttributes(attributes verbs.MessageAttributes, body *string) *verbs.Message {
message := &verbs.Message{
MessageAttributes: attributes,
Text: body,
Children: make([]interface{}, 0),
}
m.Children = append(m.Children, message)
return message
}
func (m *MessagingResponse) Redirect(url string) {
m.Children = append(m.Children, &verbs.Redirect{
Text: url,
})
}
func (m *MessagingResponse) RedirectWithAttributes(attributes verbs.RedirectAttributes, url string) {
m.Children = append(m.Children, &verbs.Redirect{
RedirectAttributes: attributes,
Text: url,
})
}
// ToTwiML generates the TwiML string or returns an error if the response cannot be marshalled
func (m *MessagingResponse) ToTwiML() (*string, error) {
return m.ToString()
}
// ToString generates the TwiML string or returns an error if the response cannot be marshalled
func (m *MessagingResponse) ToString() (*string, error) {
output, err := xml.Marshal(m)
if err != nil {
return nil, err
}
twiML := xml.Header + string(output)
return &twiML, nil
}