| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "io/ioutil" |
| 9 | "net/http" |
| 10 | |
| 11 | "github.com/slack-go/slack" |
| 12 | ) |
| 13 | |
| 14 | func main() { |
| 15 | var ( |
| 16 | signingSecret string |
| 17 | ) |
| 18 | |
| 19 | flag.StringVar(&signingSecret, "secret", "YOUR_SIGNING_SECRET_HERE", "Your Slack app's signing secret") |
| 20 | flag.Parse() |
| 21 | |
| 22 | http.HandleFunc("/slash", func(w http.ResponseWriter, r *http.Request) { |
| 23 | |
| 24 | verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret) |
| 25 | if err != nil { |
| 26 | w.WriteHeader(http.StatusInternalServerError) |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | r.Body = ioutil.NopCloser(io.TeeReader(r.Body, &verifier)) |
| 31 | s, err := slack.SlashCommandParse(r) |
| 32 | if err != nil { |
| 33 | w.WriteHeader(http.StatusInternalServerError) |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | if err = verifier.Ensure(); err != nil { |
| 38 | w.WriteHeader(http.StatusUnauthorized) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | switch s.Command { |
| 43 | case "/echo": |
| 44 | params := &slack.Msg{Text: s.Text} |
| 45 | b, err := json.Marshal(params) |
| 46 | if err != nil { |
| 47 | w.WriteHeader(http.StatusInternalServerError) |
| 48 | return |
| 49 | } |
| 50 | w.Header().Set("Content-Type", "application/json") |
| 51 | w.Write(b) |
| 52 | default: |
| 53 | w.WriteHeader(http.StatusInternalServerError) |
| 54 | return |
| 55 | } |
| 56 | }) |
| 57 | fmt.Println("[INFO] Server listening") |
| 58 | http.ListenAndServe(":3000", nil) |
| 59 | } |