forked from slack-io/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
188 lines (158 loc) · 4.32 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
181
182
183
184
185
186
187
188
package slacker
import (
"time"
"github.com/slack-go/slack"
)
// ClientOption an option for client values
type ClientOption func(*clientOptions)
// WithAPIURL sets the API URL (for testing)
func WithAPIURL(url string) ClientOption {
return func(defaults *clientOptions) {
defaults.APIURL = url
}
}
// WithDebug sets debug toggle
func WithDebug(debug bool) ClientOption {
return func(defaults *clientOptions) {
defaults.Debug = debug
}
}
// WithBotMode instructs Slacker on how to handle message events coming from a bot.
func WithBotMode(mode BotMode) ClientOption {
return func(defaults *clientOptions) {
defaults.BotMode = mode
}
}
// WithLogger sets slacker logger
func WithLogger(logger Logger) ClientOption {
return func(defaults *clientOptions) {
defaults.Logger = logger
}
}
// WithCronLocation overrides the timezone of the cron instance.
func WithCronLocation(location *time.Location) ClientOption {
return func(defaults *clientOptions) {
defaults.CronLocation = location
}
}
type clientOptions struct {
APIURL string
Debug bool
BotMode BotMode
Logger Logger
CronLocation *time.Location
}
func newClientOptions(options ...ClientOption) *clientOptions {
config := &clientOptions{
APIURL: slack.APIURL,
Debug: false,
BotMode: BotModeIgnoreAll,
CronLocation: time.Local,
}
for _, option := range options {
option(config)
}
if config.Logger == nil {
config.Logger = newBuiltinLogger(config.Debug)
}
return config
}
// ReplyOption an option for reply values
type ReplyOption func(*replyOptions)
// WithAttachments sets message attachments
func WithAttachments(attachments []slack.Attachment) ReplyOption {
return func(defaults *replyOptions) {
defaults.Attachments = attachments
}
}
// WithInThread specifies whether to reply inside a thread of the original message
func WithInThread(inThread bool) ReplyOption {
return func(defaults *replyOptions) {
defaults.InThread = &inThread
}
}
// WithReplace replaces the original message
func WithReplace(originalMessageTS string) ReplyOption {
return func(defaults *replyOptions) {
defaults.ReplaceMessageTS = originalMessageTS
}
}
// WithEphemeral sets the message as ephemeral
func WithEphemeral() ReplyOption {
return func(defaults *replyOptions) {
defaults.IsEphemeral = true
}
}
// WithSchedule sets message's schedule
func WithSchedule(timestamp time.Time) ReplyOption {
return func(defaults *replyOptions) {
defaults.ScheduleTime = ×tamp
}
}
type replyOptions struct {
Attachments []slack.Attachment
InThread *bool
ReplaceMessageTS string
IsEphemeral bool
ScheduleTime *time.Time
}
// newReplyOptions builds our ReplyOptions from zero or more ReplyOption.
func newReplyOptions(options ...ReplyOption) *replyOptions {
config := &replyOptions{
Attachments: []slack.Attachment{},
InThread: nil,
}
for _, option := range options {
option(config)
}
return config
}
// PostOption an option for post values
type PostOption func(*postOptions)
// SetAttachments sets message attachments
func SetAttachments(attachments []slack.Attachment) PostOption {
return func(defaults *postOptions) {
defaults.Attachments = attachments
}
}
// SetThreadTS specifies whether to reply inside a thread
func SetThreadTS(threadTS string) PostOption {
return func(defaults *postOptions) {
defaults.ThreadTS = threadTS
}
}
// SetReplace sets message url to be replaced
func SetReplace(originalMessageTS string) PostOption {
return func(defaults *postOptions) {
defaults.ReplaceMessageTS = originalMessageTS
}
}
// SetEphemeral sets the user who receives the ephemeral message
func SetEphemeral(userID string) PostOption {
return func(defaults *postOptions) {
defaults.EphemeralUserID = userID
}
}
// SetSchedule sets message's schedule
func SetSchedule(timestamp time.Time) PostOption {
return func(defaults *postOptions) {
defaults.ScheduleTime = ×tamp
}
}
type postOptions struct {
Attachments []slack.Attachment
ThreadTS string
ReplaceMessageTS string
EphemeralUserID string
ScheduleTime *time.Time
}
// newPostOptions builds our PostOptions from zero or more PostOption.
func newPostOptions(options ...PostOption) *postOptions {
config := &postOptions{
Attachments: []slack.Attachment{},
}
for _, option := range options {
option(config)
}
return config
}