-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
300 lines (250 loc) · 9.72 KB
/
handler.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
package bot
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/matthieudolci/hatcher/common"
log "github.com/Sirupsen/logrus"
"github.com/julienschmidt/httprouter"
"github.com/matthieudolci/hatcher/scheduler"
"github.com/matthieudolci/hatcher/setup"
"github.com/slack-go/slack"
)
const isManagerYes = "isManagerYes"
// SlackPostHandler listen on /slack for answer from the questions asked in setup.go
// and dispatch to the good functions
func SlackPostHandler(s *common.Slack) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.URL.Path != "/slack" {
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte(fmt.Sprintf("incorrect path: %s", r.URL.Path)))
if err != nil {
log.WithError(err).Error("Could not post the message incorrect path")
}
log.WithFields(log.Fields{
"urlpath": r.URL.Path,
}).Error("incorrect path")
return
}
if r.Body == nil {
w.WriteHeader(http.StatusNotAcceptable)
_, err := w.Write([]byte("empty body"))
if err != nil {
log.WithError(err).Error("empty body")
}
return
}
defer r.Body.Close()
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusGone)
_, err = w.Write([]byte("could not parse body"))
if err != nil {
log.WithError(err).Error("could not parse body")
}
return
}
// slack API calls the data POST a 'payload'
reply := r.PostFormValue("payload")
if len(reply) == 0 {
w.WriteHeader(http.StatusNoContent)
_, err = w.Write([]byte("could not find payload"))
if err != nil {
log.WithError(err).Error("Could not find payload")
}
return
}
var payload slack.InteractionCallback
err = json.NewDecoder(strings.NewReader(reply)).Decode(&payload)
if err != nil {
w.WriteHeader(http.StatusGone)
_, err = w.Write([]byte("could not process payload"))
if err != nil {
log.WithError(err).Error("Could not process payload")
}
return
}
value := payload.ActionCallback.AttachmentActions[0].Value
name := payload.ActionCallback.AttachmentActions[0].Name
api := slack.New(s.Token)
userid := fmt.Sprintf(payload.User.ID)
user, err := api.GetUserInfo(userid)
if err != nil {
log.WithError(err).Error("Could not get user info")
return
}
fullname := fmt.Sprintf(user.Profile.RealName)
displayname := fmt.Sprintf(user.Profile.DisplayName)
email := fmt.Sprintf(user.Profile.Email)
channelid := fmt.Sprintf(payload.Channel.ID)
switch value {
case "SetupYes":
_, err := w.Write([]byte(":white_check_mark: - Starting the setup of your user."))
if err != nil {
log.WithError(err).Error("Could not post the message Starting the setup of your user")
}
err = setup.InitBot(userid, email, fullname, displayname)
if err != nil {
log.WithError(err).Error("Could not start initBot for value SetupYes")
}
log.Info("Started initBot for value SetupYes")
err = setup.AskWhoIsManager(s, channelid, userid)
if err != nil {
log.WithError(err).Error("Could not start askWhoIsManager")
}
log.Info("Start askWhoIsManager")
case "SetupNo":
_, err := w.Write([]byte("No worries, let me know if you want to later on!"))
if err != nil {
log.WithError(err).Error("Could not post the message No worries, let me know if you want to later on")
}
case "RemoveYes":
err = setup.RemoveBot(userid, fullname)
if err != nil {
log.WithError(err).Error("Could not start removeBot")
}
log.Info("Started removeBot")
err := scheduler.GetTimeAndUsersForScheduler(s)
if err != nil {
log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for value RemoveYes")
}
log.Info("Started GetTimeAndUsersForScheduler for value RemoveYes")
_, err = w.Write([]byte("Sorry to see you go. Your user was deleted."))
if err != nil {
log.WithError(err).Error("Could not post the message Sorry to see you go")
}
case "RemoveNo":
_, err := w.Write([]byte("Glad you decided to stay :smiley:"))
if err != nil {
log.WithError(err).Error("Could not post the message Glad you decided to stay")
}
case isManagerYes:
answer := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].Value)
if answer == isManagerYes {
value := fmt.Sprintf("true")
err := setup.IsManager(userid, fullname, value)
if err != nil {
log.WithError(err).Error("Could not start IsManager for value isManagerYes")
}
log.Info("Start IsManager for value isManagerYes")
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - You are setup as a manager.")))
if err != nil {
log.WithError(err).Error("Could not post You are setup as a manager message")
}
}
case "isManagerNo":
answer := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].Value)
if answer == "isManagerNo" {
value := fmt.Sprintf("false")
err := setup.IsManager(userid, fullname, value)
if err != nil {
log.WithError(err).Error("Could not start IsManager for value isManagerNo")
}
log.Info("Start IsManager for value isManagerNo")
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s your user is now setup!\n", displayname)))
if err != nil {
log.WithError(err).Error("Could not post the final message of the setup for none manager user")
}
}
}
switch name {
case "ManagerChosen":
managerid := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
manager, err := api.GetUserInfo(managerid)
if err != nil {
log.WithError(err).Error("Could not start GetUserInfo for name ManagerChosen")
}
log.Info("Getting user informations for name ManagerChosen")
managername := fmt.Sprintf(manager.RealName)
err = setup.InitManager(userid, fullname, managerid, managername)
if err != nil {
log.WithError(err).Error("Could not start initManager for name ManagerChosen")
}
log.Info("Started initManager for name ManagerChosen")
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s was setup as your manager.", managername)))
if err != nil {
log.WithError(err).Error("Could not post the you are setup as a manager")
}
timestandup := setup.GetStandupTimeFromManager(managerid)
channel := setup.GetStandupChannelFromManager(managerid)
if len(timestandup) > 0 {
err = setup.UpdateTimeStandup(managerid, userid, timestandup)
if err != nil {
log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
}
log.Info("Started updateTimeStandup for name ManagerChosen")
} else {
err = setup.UpdateTimeStandup(managerid, userid, "NULL")
if err != nil {
log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
}
log.Info("Started updateTimeStandup for name ManagerChosen")
}
err = setup.UpdateChannelStandup(managerid, userid, channel)
if err != nil {
log.WithError(err).Error("Could not start updateTimeStandup for name ManagerChosen")
}
log.Info("Started updateTimeStandup for name ManagerChosen")
err = scheduler.GetTimeAndUsersForScheduler(s)
if err != nil {
log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
}
log.Info("Start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
err = setup.AskIfManager(s, channelid, userid)
if err != nil {
log.WithError(err).Error("Could not start askIfManager for name ManagerChosen")
}
log.Info("Started askIfManager for name ManagerChosen")
case "NoManagerChosen":
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - No manager selected")))
if err != nil {
log.WithError(err).Error("Could not post the you are setup as a manager")
}
err = setup.AskIfManager(s, channelid, userid)
if err != nil {
log.WithError(err).Error("Could not start askIfManager for name NoManagerChosen")
}
log.Info("Started askIfManager for name NoManagerChosen")
case isManagerYes:
err = setup.AskTimeStandup(s, channelid, userid)
if err != nil {
log.WithError(err).Error("Could not start askTimeStandup")
}
log.Info("Start askTimeStandup")
case "StandupTime":
time := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
err := setup.InsertTimeStandup(userid, fullname, time)
if err != nil {
log.WithError(err).Error("Could not start insertTimeStandup for name StandupTime")
}
log.Info("Start insertTimeStandup for name StandupTime")
err = setup.AskWhichChannelStandup(s, channelid, userid)
if err != nil {
log.WithError(err).Error("Could not start askWhichChannelStandup for name StandupTime")
}
log.Info("Start askWhichChannelStandup for name StandupTime")
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - Team standup setup")))
if err != nil {
log.WithError(err).Error("Could not post the message Team standup setup")
}
case "ChannelStandupChosen":
channel := fmt.Sprintf(payload.ActionCallback.AttachmentActions[0].SelectedOptions[0].Value)
err := setup.InsertChannelStandup(userid, fullname, channel)
if err != nil {
log.WithError(err).Error("Could not start insertChannelStandup for name ChannelStandupChosen")
}
log.Info("Start insertChannelStandup for name ChannelStandupChosen")
err = scheduler.GetTimeAndUsersForScheduler(s)
if err != nil {
log.WithError(err).Error("Could not start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
}
log.Info("Start GetTimeAndUsersForScheduler for name ChannelStandupChosen")
_, err = w.Write([]byte(fmt.Sprintf(":white_check_mark: - %s your user is now setup!", displayname)))
if err != nil {
log.WithError(err).Error("Could not post the message Your user is now setup")
}
}
w.WriteHeader(http.StatusOK)
}
}