forked from pritunl/pritunl-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwilio.go
119 lines (98 loc) · 2.62 KB
/
twilio.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
108
109
110
111
112
113
114
115
116
117
118
119
package twilio
import (
"encoding/xml"
"github.com/sirupsen/logrus"
"github.com/twilio/twilio-go"
"github.com/dropbox/godropbox/errors"
"github.com/pritunl/pritunl-zero/errortypes"
"github.com/pritunl/pritunl-zero/settings"
openapi "github.com/twilio/twilio-go/rest/api/v2010"
)
type TwimlSay struct {
XMLName xml.Name `xml:"Say"`
Voice string `xml:"voice,attr"`
Loop string `xml:"loop,attr"`
Message string `xml:",chardata"`
}
type TwimlResponse struct {
XMLName xml.Name `xml:"Response"`
Say *TwimlSay `xml:"Say"`
}
func PhoneCall(number, message string) (err error) {
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: settings.System.TwilioAccount,
Password: settings.System.TwilioSecret,
})
params := &openapi.CreateCallParams{}
params.SetFrom(settings.System.TwilioNumber)
params.SetTo(number)
twiml := &TwimlResponse{
Say: &TwimlSay{
Voice: "alice",
Loop: "3",
Message: FilterStrPhone(message, 160),
},
}
twimlData, err := xml.Marshal(twiml)
if err != nil {
err = &errortypes.ParseError{
errors.Wrap(err, "twilio: Failed to marshal twiml message"),
}
return
}
params.SetTwiml(string(twimlData))
resp, err := client.Api.CreateCall(params)
if err != nil {
err = &errortypes.RequestError{
errors.Wrap(err, "twilio: Twilio call error"),
}
return
}
respSid := *resp.Sid
if respSid == "" {
err = &errortypes.RequestError{
errors.Wrap(err, "twilio: Invalid call sid"),
}
return
}
return
}
func TextMessage(number, message string) (err error) {
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: settings.System.TwilioAccount,
Password: settings.System.TwilioSecret,
})
params := &openapi.CreateMessageParams{}
params.SetFrom(settings.System.TwilioNumber)
params.SetTo(number)
params.SetBody("Pritunl Alert: " + FilterStrMessage(message, 800))
resp, err := client.Api.CreateMessage(params)
if err != nil {
err = &errortypes.RequestError{
errors.Wrap(err, "twilio: Twilio message error"),
}
return
}
respSid := *resp.Sid
if respSid == "" {
err = &errortypes.RequestError{
errors.Wrap(err, "twilio: Invalid message sid"),
}
return
}
if resp.ErrorCode != nil && resp.ErrorMessage != nil &&
*resp.ErrorMessage != "" {
logrus.WithFields(logrus.Fields{
"number": number,
"message": message,
"source_number": settings.System.TwilioNumber,
"error_code": resp.ErrorCode,
"error_message": resp.ErrorMessage,
}).Error("twilio: Text message error")
err = &errortypes.RequestError{
errors.Wrap(err, "twilio: Twilio message error"),
}
return
}
return
}