-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (165 loc) · 4.46 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"bytes"
"context"
"encoding/json"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/bmf-san/go-dotenv"
"github.com/chromedp/chromedp"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
)
// Draw is data set for drawing.
type Draw struct {
Color string
BgColor string
Line1 string
Line2 string // optional
}
func handlerGenerator(w http.ResponseWriter, r *http.Request) {
tpl := template.Must(template.ParseFiles("tpl/multiline.tpl"))
if r.URL.Query().Get("line2") == "" {
tpl = template.Must(template.ParseFiles("tpl/oneline.tpl"))
}
tpl.Execute(w, Draw{
Color: r.URL.Query().Get("color"),
BgColor: r.URL.Query().Get("bgColor"),
Line1: r.URL.Query().Get("line1"),
Line2: r.URL.Query().Get("line2"),
})
}
func handlerEvents(w http.ResponseWriter, r *http.Request) {
api := slack.New(os.Getenv("SLACK_BOT_TOKEN"))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
eventsAPIEvent, err := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionNoVerifyToken())
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
switch eventsAPIEvent.Type {
case slackevents.URLVerification:
var res *slackevents.ChallengeResponse
if err := json.Unmarshal(body, &res); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
if _, err := w.Write([]byte(res.Challenge)); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
case slackevents.CallbackEvent:
innerEvent := eventsAPIEvent.InnerEvent
switch event := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
message := strings.Split(event.Text, " ")
if len(message) < 2 {
w.WriteHeader(http.StatusBadRequest)
return
}
color := message[1]
bgColor := message[2]
line1 := message[3]
// line2 is optional.
line2 := ""
if len(message) > 4 {
line2 = message[4]
}
query := url.Values{
"color": []string{color},
"bgColor": []string{bgColor},
"line1": []string{line1},
"line2": []string{line2},
}
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// FIXME: Images are posted multiple times by running chromedp.
var buf []byte
if err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Navigate(`http://localhost:9999/generator?` + query.Encode()),
chromedp.Sleep(2 * time.Second),
chromedp.WaitVisible(`#target`, chromedp.ByID),
chromedp.Screenshot(`#target`, &buf, chromedp.NodeVisible, chromedp.ByID),
}); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to take a screen shot."))
return
}
if err := ioutil.WriteFile("result.png", buf, 0644); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to take a screen shot."))
return
}
r := bytes.NewReader(buf)
_, err = api.UploadFile(
slack.FileUploadParameters{
Reader: r,
Filename: "upload file name",
Channels: []string{event.Channel},
})
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to post a image."))
return
}
}
}
}
func middlewareVerification(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, os.Getenv("SLACK_SIGNING_SECRET"))
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
bodyReader := io.TeeReader(r.Body, &verifier)
body, err := ioutil.ReadAll(bodyReader)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := verifier.Ensure(); err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
next.ServeHTTP(w, r)
}
}
func main() {
if err := dotenv.LoadEnv(); err != nil {
log.Println(err)
}
mux := http.NewServeMux()
mux.HandleFunc("/generator", handlerGenerator)
mux.HandleFunc("/slack/events", middlewareVerification(handlerEvents))
srv := &http.Server{
Addr: ":9999",
Handler: mux,
}
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}