/
twilio.go
254 lines (231 loc) · 5.98 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package plugins
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/nlopes/slack"
)
var (
// TwilioPhone is the "to" phone number that's set through Slack (@slab twilio
// set)
TwilioPhone string
// TwilioFrom is the "from" phone number that's set through Slack (@slab twilio
// configure)
TwilioFrom string
// TwilioEnabled holds whether the Twilio plugin is enabled or disabled.
TwilioEnabled bool
)
func init() {
Commands["twilio"] = twilioCommands
Send["twilio"] = (*Plugins).SendTwilio
}
func twilioCommands(t []string) (attachments []slack.Attachment, message string) {
switch t[1] {
case "twilio":
if len(t) > 1 {
p := LoadPlugins()
switch t[2] {
case "set":
if len(t) > 3 {
s := TwilioSet(t[3])
attachments = []slack.Attachment{s}
message = "Plugin message"
}
case "unset":
s := TwilioUnset()
attachments = []slack.Attachment{s}
message = "Plugin message"
case "configure":
if len(t) > 3 {
s := TwilioConfigure(t[3])
attachments = []slack.Attachment{s}
message = "Plugin message"
}
case "status":
s := p.TwilioStatus()
attachments = []slack.Attachment{s}
message = "Plugin status"
case "enable":
p.EnableTwilio()
a := slack.Attachment{
Title: "Twilio Plugin",
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Enabled",
Value: ":white_check_mark:",
},
},
}
attachments = []slack.Attachment{a}
message = "Plugin Twilio has been updated"
case "disable":
p.DisableTwilio()
a := slack.Attachment{
Title: "Twilio Plugin",
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Enabled",
Value: ":x:",
},
},
}
attachments = []slack.Attachment{a}
message = "Plugin Twilio has been updated"
}
}
}
return attachments, message
}
// Twilio contains the connection details for the Twilio API:
// https://www.twilio.com/docs/api
type Twilio struct {
AccountID string
Auth string
Enabled bool
}
// EnableTwilio changes the Enabled Twilio option to true.
func (p *Plugins) EnableTwilio() (attachment slack.Attachment) {
TwilioEnabled = true
return p.checkTwilioStatus()
}
// DisableTwilio changes the Enabled Twilio option to false.
func (p *Plugins) DisableTwilio() (attachment slack.Attachment) {
TwilioEnabled = false
return p.checkTwilioStatus()
}
// TwilioSet changes the TwilioPhone to the value of the number passed to
// it.
func TwilioSet(n string) (attachment slack.Attachment) {
TwilioPhone = n
log.Info("Phone number set.", map[string]interface{}{
"module": "plugin",
"plugin": "Twilio",
"phone": TwilioPhone,
})
attachment = slack.Attachment{
Title: "Twilio 'To' Phone Number Set",
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Current Phone Number",
Value: TwilioPhone,
Short: true,
},
},
}
return attachment
}
// TwilioUnset sets the TwilioPhone to `none`
func TwilioUnset() (attachment slack.Attachment) {
TwilioPhone = ""
attachment = slack.Attachment{
Title: "Twilio 'To' Phone Number Unset",
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Current Phone Number",
Value: TwilioPhone,
Short: true,
},
},
}
return attachment
}
// TwilioStatus returns the current setting
func (p *Plugins) TwilioStatus() (attachment slack.Attachment) {
return p.checkTwilioStatus()
}
// SendTwilio sends a message to the phone number currently set
// as TwilioPhone using the connection data found in the config
func (p *Plugins) SendTwilio(message string) {
if TwilioPhone == "" {
log.Info("To phone number for Twilio not set.", map[string]interface{}{
"module": "plugin",
"plugin": "twilio",
})
}
if TwilioFrom == "" {
log.Info("From phone number for Twilio not set.", map[string]interface{}{
"module": "plugin",
"plugin": "twilio",
})
}
if (TwilioEnabled) && (TwilioPhone != "") {
log.Info("Plugin loaded. Sending Twilio message.", map[string]interface{}{
"module": "plugin",
"plugin": "twilio",
})
}
// Prep text message
msgData := url.Values{}
msgData.Set("To", TwilioPhone)
msgData.Set("From", TwilioFrom)
msgData.Set("Body", message)
msgDataReader := *strings.NewReader(msgData.Encode())
// Connect to Twilio
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + p.Twilio.AccountID + "/Messages.json"
client := &http.Client{}
req, _ := http.NewRequest("POST", urlStr, &msgDataReader)
req.SetBasicAuth(p.Twilio.AccountID, p.Twilio.Auth)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Send request
resp, _ := client.Do(req)
// Parse response
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var data map[string]interface{}
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&data)
if err == nil {
fmt.Println(data["sid"])
}
} else {
fmt.Println(resp.Status)
}
}
// TwilioConfigure sets the "from" phone number to allow for international
// numbers to be set properly
func TwilioConfigure(n string) (attachment slack.Attachment) {
TwilioFrom = n
log.Info("From phone number set.", map[string]interface{}{
"module": "plugin",
"plugin": "Twilio",
"from": TwilioFrom,
})
attachment = slack.Attachment{
Title: "Twilio 'From' Phone Number Set",
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Current Phone Number",
Value: TwilioFrom,
Short: true,
},
},
}
return attachment
}
func (p *Plugins) checkTwilioStatus() (attachment slack.Attachment) {
s := ":x:"
if p.Twilio.Enabled {
s = ":white_check_mark:"
}
attachment = slack.Attachment{
Fields: []slack.AttachmentField{
slack.AttachmentField{
Title: "Enabled",
Value: s,
},
slack.AttachmentField{
Title: "Current 'From' Phone Number",
Value: TwilioFrom,
Short: true,
},
slack.AttachmentField{
Title: "Current 'To' Phone Number",
Value: TwilioPhone,
Short: true,
},
},
}
return attachment
}