-
Notifications
You must be signed in to change notification settings - Fork 9
/
options.go
180 lines (154 loc) · 4.72 KB
/
options.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
package slack
import (
"crypto/tls"
"errors"
"net/http"
"time"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
// An Option is used to configure the slack adapter.
type Option func(*Config) error
// Config contains the configuration of a BotAdapter.
type Config struct {
Token string
VerificationToken string
Name string
Debug bool
Logger *zap.Logger
SlackAPIURL string // defaults to github.com/slack-go/slack.APIURL but can be changed for unit tests
// SendMsgParams contains settings that are applied to all messages sent
// by the BotAdapter.
SendMsgParams slack.PostMessageParameters
// Log unknown message types as error message for debugging. This option is
// disabled by default.
LogUnknownMessageTypes bool
// Listen and respond to all messages not just those directed at the Bot User.
ListenPassive bool
// Options if you want to use the Slack Events API. Ignored on the normal RTM adapter.
EventsAPI EventsAPIConfig
}
// EventsAPIConfig contains the configuration of an EventsAPIServer.
type EventsAPIConfig struct {
Middleware func(next http.Handler) http.Handler
ShutdownTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
TLSConf *tls.Config
CertFile, KeyFile string
}
func (conf Config) slackOptions() []slack.Option {
if conf.Logger == nil {
conf.Logger = zap.NewNop()
}
if conf.SlackAPIURL == "" {
conf.SlackAPIURL = slack.APIURL
}
if conf.SlackAPIURL[len(conf.SlackAPIURL)-1] != '/' {
conf.SlackAPIURL += "/"
}
opts := []slack.Option{
slack.OptionAPIURL(conf.SlackAPIURL),
}
if conf.Debug {
opts = append(opts,
slack.OptionDebug(conf.Debug),
slack.OptionLog(zap.NewStdLog(conf.Logger)),
)
}
return opts
}
// WithLogger can be used to inject a different logger for the slack adapater.
func WithLogger(logger *zap.Logger) Option {
return func(conf *Config) error {
conf.Logger = logger
return nil
}
}
// WithDebug enables debug messages of the slack client.
func WithDebug(debug bool) Option {
return func(conf *Config) error {
conf.Debug = debug
return nil
}
}
// WithMessageParams overrides the default parameters that are used when sending
// any message to slack.
func WithMessageParams(params slack.PostMessageParameters) Option {
return func(conf *Config) error {
conf.SendMsgParams = params
return nil
}
}
// WithLogUnknownMessageTypes makes the adapter log unknown message types as
// error message for debugging. This option is disabled by default.
func WithLogUnknownMessageTypes() Option {
return func(conf *Config) error {
conf.LogUnknownMessageTypes = true
return nil
}
}
// WithListenPassive makes the adapter listen and respond to all messages not
// just those directed at it
func WithListenPassive() Option {
return func(conf *Config) error {
conf.ListenPassive = true
return nil
}
}
// WithTLS is an option for the EventsAPIServer that enables serving HTTP
// requests via TLS.
func WithTLS(certFile, keyFile string) Option {
return func(conf *Config) error {
if certFile == "" {
return errors.New("path to certificate file cannot be empty")
}
if keyFile == "" {
return errors.New("path to private key file cannot be empty")
}
conf.EventsAPI.CertFile = certFile
conf.EventsAPI.KeyFile = keyFile
return nil
}
}
// WithTLSConfig is an option for the EventsAPIServer that can be used in
// combination with the WithTLS(…) option to configure the HTTPS server.
func WithTLSConfig(tlsConf *tls.Config) Option {
return func(conf *Config) error {
conf.EventsAPI.TLSConf = tlsConf
return nil
}
}
// WithTimeouts is an option for the EventsAPIServer that sets both the read
// and write timeout of the HTTP server to the same given value.
func WithTimeouts(d time.Duration) Option {
return func(conf *Config) error {
conf.EventsAPI.ReadTimeout = d
conf.EventsAPI.WriteTimeout = d
return nil
}
}
// WithReadTimeout is an option for the EventsAPIServer that sets the servers
// maximum duration for reading the entire HTTP request, including the body.
func WithReadTimeout(d time.Duration) Option {
return func(conf *Config) error {
conf.EventsAPI.ReadTimeout = d
return nil
}
}
// WithWriteTimeout is an option for the EventsAPIServer that sets the
// servers maximum duration before timing out writes of the HTTP response.
func WithWriteTimeout(d time.Duration) Option {
return func(conf *Config) error {
conf.EventsAPI.WriteTimeout = d
return nil
}
}
// WithMiddleware is an option for the EventsAPIServer that allows the user to
// inject an HTTP middleware to the HTTP server.
func WithMiddleware(mw func(next http.Handler) http.Handler) Option {
return func(conf *Config) error {
conf.EventsAPI.Middleware = mw
return nil
}
}