forked from szpp-dev-team/slack-szppi-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (62 loc) · 1.99 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
// "context"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/slack-go/slack"
"github.com/szpp-dev-team/szpp-slack-bot/commands"
// "google.golang.org/api/customsearch/v1"
// "google.golang.org/api/option"
)
func main() {
botUserOauthToken := os.Getenv("BOT_USER_OAUTH_TOKEN")
signingSecret := os.Getenv("SIGNING_SECRET")
// customsearchApiKey := os.Getenv("CUSTOM_SEARCH_API_KEY")
port := getenvOr("PORT", "8080")
client := slack.New(botUserOauthToken)
// customsearchService, err := customsearch.NewService(context.Background(), option.WithAPIKey(customsearchApiKey))
// if err != nil {
// log.Fatal(err)
// }
slashHandler := NewSlashHandler(client)
// カスタムコマンドの追加はここで行う(インスタンスを引数に渡せばおk)
slashHandler.RegisterSubHandlers(
commands.NewSubHandlerOmikuji(client),
// commands.NewSubHandlerImage(client, customsearchService),
commands.NewSubHandlerOhgiri(client),
)
http.HandleFunc("/slack/events", func(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
if err != nil {
log.Println("failed to verify secrets:", err)
http.Error(w, "failed to verify secrets", http.StatusInternalServerError)
return
}
r.Body = io.NopCloser(io.TeeReader(r.Body, &verifier))
slashCmd, err := slack.SlashCommandParse(r)
if err != nil {
log.Println("failed to parse slash command:", err)
http.Error(w, "failed to parse slash command", http.StatusInternalServerError)
return
}
if err := verifier.Ensure(); err != nil {
log.Println("failed to ensure compares the signature:", err)
http.Error(w, "failed to ensure compares the signature", http.StatusUnauthorized)
return
}
slashHandler.Handle(w, &slashCmd)
})
if err := http.ListenAndServe(fmt.Sprintf(":%v", port), nil); err != nil {
log.Fatal(err)
}
}
func getenvOr(key, altValue string) string {
value := os.Getenv(key)
if value == "" {
value = altValue
}
return value
}