forked from slack-go/slack
/
slash.go
49 lines (42 loc) · 987 Bytes
/
slash.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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"github.com/nlopes/slack"
)
func main() {
var (
verificationToken string
)
flag.StringVar(&verificationToken, "token", "YOUR_VERIFICATION_TOKEN_HERE", "Your Slash Verification Token")
flag.Parse()
http.HandleFunc("/slash", func(w http.ResponseWriter, r *http.Request) {
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !s.ValidateToken(verificationToken) {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch s.Command {
case "/echo":
params := &slack.Msg{Text: s.Text}
b, err := json.Marshal(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
})
fmt.Println("[INFO] Server listening")
http.ListenAndServe(":3000", nil)
}