-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
102 lines (81 loc) · 2.58 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"io"
"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 {
// URL at which the server is reachable
ServerUrl string `envconfig:"SERVER_URL" required:"true"`
// 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"`
// CloudMQTT URL to connect to
MqttUrl string `envconfig:"CLOUDMQTT_URL" required:"true"`
// Redis URL to connect to
RedisUrl string `envconfig:"REDIS_URL" required:"true"`
// Turn off low-level Slack API debugging
Debug bool `envconfig:"DEBUG"`
}
var env envConfig
func main() {
os.Exit(_main(os.Args[1:]))
}
func _main(args []string) int {
if err := envconfig.Process("", &env); err != nil {
log.Printf("[ERROR] Failed to process env var: %s", err)
return 1
}
imgcache, err := NewImageCache(env.RedisUrl)
if err != nil {
log.Printf("[ERROR] Failed to connect to Redis: %s", err)
return 1
}
// Listening slack event and response
log.Printf("[INFO] Start slack event listening")
client := slack.New(env.BotToken)
client.SetDebug(env.Debug)
slackListener := &SlackListener{
token: env.BotToken,
verftoken: env.VerificationToken,
client: client,
botID: env.BotID,
imgcache: imgcache,
}
go slackListener.ListenAndResponse()
// Register handler to receive interactive message
// responses from slack (kicked by user action)
http.Handle("/interaction", interactionHandler{
verificationToken: env.VerificationToken,
imgcache: imgcache,
})
// Register handle to use Events API; for now this is a simple workaround
// to restart the dyno if Heroku sends it to sleep
http.HandleFunc("/events", slackListener.HandleEventsAPI)
http.HandleFunc("/image/", func(rw http.ResponseWriter, req *http.Request) {
var img []byte
if err := imgcache.Get(req.URL.Path, &img); err != nil {
rw.WriteHeader(http.StatusNotFound)
return
}
rw.Header().Set("Content-type", "image/png")
io.Copy(rw, bytes.NewReader(img))
})
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
}