forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slash.go
59 lines (50 loc) · 1.19 KB
/
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
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/MadFaill/slackv2"
)
func main() {
var (
signingSecret string
)
flag.StringVar(&signingSecret, "secret", "YOUR_SIGNING_SECRET_HERE", "Your Slack app's signing secret")
flag.Parse()
http.HandleFunc("/slash", func(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
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)
}