/
handlers.go
46 lines (39 loc) · 930 Bytes
/
handlers.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
package main
import (
"github.com/gorilla/schema"
"io"
"net/http"
)
var commands = map[string](func(*SlackCommand, *User) error){
"help": helpCommand,
"version": versionCommand,
}
func commandHandler(res http.ResponseWriter, req *http.Request) {
req.ParseForm()
command := new(SlackCommand)
decoder := schema.NewDecoder()
err := decoder.Decode(command, req.PostForm)
if err != nil {
res.WriteHeader(400)
io.WriteString(res, "Invalid form data\n")
return
}
if command.Token != config.IncomingToken {
res.WriteHeader(403)
io.WriteString(res, "Invalid token\n")
return
}
user, created := userStore.GetOrAdd(command.UserID)
if created {
user.UserName = command.UserName
}
commandFunc, exists := commands[command.getSubCommand()]
if !exists {
commandFunc = commands["help"]
}
err = commandFunc(command, user)
if err != nil {
res.WriteHeader(400)
io.WriteString(res, err.Error())
}
}