-
Notifications
You must be signed in to change notification settings - Fork 12
/
slack.go
308 lines (274 loc) · 7.46 KB
/
slack.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Package slack is a library implementing the Slack Web and RTM API.
Written by Slavik Markovich at Demisto
*/
package slack
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
// DefaultURL points to the default Slack API
DefaultURL = "https://slack.com/api/"
)
// Error is returned when there is a known condition error in the API
type Error struct {
ID string `json:"id"`
Detail string `json:"detail"`
}
func newError(code, format string, args ...interface{}) *Error {
return &Error{code, fmt.Sprintf(format, args...)}
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.ID, e.Detail)
}
var (
// ErrBadToken is returned when a bad token is passed to the API
ErrBadToken = &Error{"bad_token", "Bad token was provided to the API"}
// ErrNoToken is returned when the token is missing
ErrNoToken = &Error{"no_token", "You must provide a Slack token to use the API"}
// ErrBadOAuth is returned when OAuth credentials are bad
ErrBadOAuth = &Error{"bad_oauth", "Bad OAuth credentials provided"}
)
// Slack is the client to the Slack API.
type Slack struct {
token string // The token to use for requests. Required.
url string // The URL for the API.
errorlog *log.Logger // Optional logger to write errors to
tracelog *log.Logger // Optional logger to write trace and debug data to
c *http.Client // The client to use for requests
ws *websocket.Conn // WS connection
mid int // WS message ID
mutex sync.Mutex // WS mutex to protect changes
}
// OptionFunc is a function that configures a Client.
// It is used in New
type OptionFunc func(*Slack) error
// errorf logs to the error log.
func (s *Slack) errorf(format string, args ...interface{}) {
if s.errorlog != nil {
s.errorlog.Printf(format, args...)
}
}
// tracef logs to the trace log.
func (s *Slack) tracef(format string, args ...interface{}) {
if s.tracelog != nil {
s.tracelog.Printf(format, args...)
}
}
// New creates a new Slack client.
//
// The caller can configure the new client by passing configuration options to the func.
//
// Example:
//
// s, err := slack.New(
// slack.SetURL("https://some.url.com:port/"),
// slack.SetErrorLog(log.New(os.Stderr, "Slack: ", log.Lshortfile),
// slack.SetToken("Your-Token"))
//
// You must provide either a token or the OAuth parameters to retrieve the token.
// If no URL is configured, Slack uses DefaultURL by default.
//
// If no HttpClient is configured, then http.DefaultClient is used.
// You can use your own http.Client with some http.Transport for advanced scenarios.
//
// An error is also returned when some configuration option is invalid.
func New(options ...OptionFunc) (*Slack, error) {
// Set up the client
s := &Slack{
url: "",
c: http.DefaultClient,
}
// Run the options on it
for _, option := range options {
if err := option(s); err != nil {
return nil, err
}
}
if s.url == "" {
s.url = DefaultURL
}
s.tracef("Using URL [%s]\n", s.url)
// If no API key was specified
if s.token == "" {
s.errorf("%s\n", ErrNoToken.Error())
return nil, ErrNoToken
}
return s, nil
}
// Initialization functions
// SetToken sets the Slack API token to use
func SetToken(token string) OptionFunc {
return func(s *Slack) error {
if token == "" {
s.errorf("%s\n", ErrBadToken.Error())
return ErrBadToken
}
s.token = token
return nil
}
}
// SetHTTPClient can be used to specify the http.Client to use when making
// requests to Slack.
func SetHTTPClient(httpClient *http.Client) OptionFunc {
return func(s *Slack) error {
if httpClient != nil {
s.c = httpClient
} else {
s.c = http.DefaultClient
}
return nil
}
}
// SetURL defines the URL endpoint for Slack
func SetURL(rawurl string) OptionFunc {
return func(s *Slack) error {
if rawurl == "" {
rawurl = DefaultURL
}
u, err := url.Parse(rawurl)
if err != nil {
e := newError("bad_url", "Invalid URL [%s] - %v\n", rawurl, err.Error())
s.errorf("%s\n", e.Error())
return e
}
if u.Scheme != "http" && u.Scheme != "https" {
err = newError("bad_url", "Invalid schema specified [%s]", rawurl)
s.errorf("%v", err)
return err
}
s.url = rawurl
if !strings.HasSuffix(s.url, "/") {
s.url += "/"
}
return nil
}
}
// SetErrorLog sets the logger for critical messages. It is nil by default.
func SetErrorLog(logger *log.Logger) func(*Slack) error {
return func(s *Slack) error {
s.errorlog = logger
return nil
}
}
// SetTraceLog specifies the logger to use for output of trace messages like
// HTTP requests and responses. It is nil by default.
func SetTraceLog(logger *log.Logger) func(*Slack) error {
return func(s *Slack) error {
s.tracelog = logger
return nil
}
}
// dumpRequest dumps a request to the debug logger if it was defined
func (s *Slack) dumpRequest(req *http.Request) {
if s.tracelog != nil {
out, err := httputil.DumpRequestOut(req, true)
if err == nil {
s.tracef("%s\n", string(out))
}
}
}
// dumpResponse dumps a response to the debug logger if it was defined
func (s *Slack) dumpResponse(resp *http.Response) {
if s.tracelog != nil {
out, err := httputil.DumpResponse(resp, true)
if err == nil {
s.tracef("%s\n", string(out))
}
}
}
// Request handling functions
// handleError will handle responses with status code different from success
func (s *Slack) handleError(resp *http.Response) error {
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if s.errorlog != nil {
out, err := httputil.DumpResponse(resp, true)
if err == nil {
s.errorf("%s\n", string(out))
}
}
e := newError("http_error", "Unexpected status code: %d (%s)", resp.StatusCode, http.StatusText(resp.StatusCode))
s.errorf("%s\n", e.Error())
return e
}
return nil
}
// do executes the API request.
// Returns the response if the status code is between 200 and 299
func (s *Slack) do(path string, params url.Values, result interface{}) error {
appendNotEmpty("token", s.token, params)
var t time.Time
if s.tracelog != nil {
t = time.Now()
s.tracef("Start request %s at %v", path, t)
}
resp, err := s.c.PostForm(s.url+path, params)
if s.tracelog != nil {
s.tracef("End request %s at %v - took %v", path, time.Now(), time.Since(t))
}
if err != nil {
return err
}
defer resp.Body.Close()
if err = s.handleError(resp); err != nil {
return err
}
s.dumpResponse(resp)
if result != nil {
switch result := result.(type) {
// Should we just dump the response body
case io.Writer:
if _, err = io.Copy(result, resp.Body); err != nil {
return err
}
case Response:
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
return err
}
// Handle ok response parameter
if !result.IsOK() {
s.errorf("%s\n", result.Error())
return result
}
default:
// Try parsing the message anyway
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
return err
}
}
}
return nil
}
// Helper functions
func appendNotEmpty(name, val string, params url.Values) {
if val != "" {
params.Add(name, val)
}
}
// Slack API types
// Response interface represents any reply from Slack with the basic ok, error methods
type Response interface {
IsOK() bool
Error() string
}
// Common response to all messages
type slackResponse struct {
OK bool `json:"ok"`
Err string `json:"error"`
}
func (r *slackResponse) IsOK() bool {
return r.OK
}
func (r *slackResponse) Error() string {
return r.Err
}