-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (56 loc) · 1.52 KB
/
Copy pathmain.go
File metadata and controls
69 lines (56 loc) · 1.52 KB
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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/nlopes/slack"
)
// Customize the slashCommand to anything
const slashCommand = "/hello"
func slashCommandHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if !s.ValidateToken(os.Getenv("SLACK_VERIFICATION_TOKEN")) {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch s.Command {
case slashCommand:
log.Printf("%s slack command is issued\n", slashCommand)
// Add code here!
response := "Hello World!"
// Create a JSON response
w.Header().Set("Content-Type", "application/json")
jsonResponse := make(map[string]string)
jsonResponse["response_type"] = "in_channel"
jsonResponse["text"] = response
jsonResponseMarshal, err := json.Marshal(jsonResponse)
if err != nil {
log.Fatalf("error while JSON marshalling response: %s", err)
}
w.Write([]byte(jsonResponseMarshal))
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
func main() {
http.HandleFunc("/receive", slashCommandHandler())
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("PORT environment variable not found defaulting to port %s", port)
}
log.Printf("Server listening on port %s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}