-
Notifications
You must be signed in to change notification settings - Fork 67
/
main.go
66 lines (53 loc) · 1.66 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
package main
import (
"log"
"net/http"
"os"
"github.com/kelseyhightower/envconfig"
"github.com/nlopes/slack"
)
// https://api.slack.com/slack-apps
// https://api.slack.com/internal-integrations
type envConfig struct {
// Port is server port to be listened.
Port string `envconfig:"PORT" default:"3000"`
// BotToken is bot user token to access to slack API.
BotToken string `envconfig:"BOT_TOKEN" required:"true"`
// VerificationToken is used to validate interactive messages from slack.
VerificationToken string `envconfig:"VERIFICATION_TOKEN" required:"true"`
// BotID is bot user ID.
BotID string `envconfig:"BOT_ID" required:"true"`
// ChannelID is slack channel ID where bot is working.
// Bot responses to the mention in this channel.
ChannelID string `envconfig:"CHANNEL_ID" required:"true"`
}
func main() {
os.Exit(_main(os.Args[1:]))
}
func _main(args []string) int {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Printf("[ERROR] Failed to process env var: %s", err)
return 1
}
// Listening slack event and response
log.Printf("[INFO] Start slack event listening")
client := slack.New(env.BotToken)
slackListener := &SlackListener{
client: client,
botID: env.BotID,
channelID: env.ChannelID,
}
go slackListener.ListenAndResponse()
// Register handler to receive interactive message
// responses from slack (kicked by user action)
http.Handle("/interaction", interactionHandler{
verificationToken: env.VerificationToken,
})
log.Printf("[INFO] Server listening on :%s", env.Port)
if err := http.ListenAndServe(":"+env.Port, nil); err != nil {
log.Printf("[ERROR] %s", err)
return 1
}
return 0
}