/
slappd.go
189 lines (154 loc) · 3.77 KB
/
slappd.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 (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/kpurdon/slappd/internal/slack"
"github.com/kpurdon/slappd/internal/untappd"
)
const (
callbackID = "slappd"
// MaxResults is the max number of results that are shown to the user
MaxResults = 5
)
func isAuthorized(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
st := os.Getenv("SLACK_TOKEN")
if st == "" {
log.Printf("missing environment variable: SLACK_TOKEN")
http.Error(w, http.StatusText(500), 500)
return
}
rt := r.FormValue("token")
if rt == "" {
log.Printf("missing form value: token")
http.Error(w, http.StatusText(400), 400)
return
}
var authorized bool
for _, t := range strings.Split(st, ",") {
if t == rt {
authorized = true
}
}
if !authorized {
http.Error(w, http.StatusText(403), 403)
return
}
u := r.FormValue("user_name")
if u == "" {
log.Printf("missing form value: user_name")
http.Error(w, http.StatusText(400), 400)
return
}
if u == "slackbot" {
w.WriteHeader(200)
return
}
next.ServeHTTP(w, r)
})
}
func selectHandler(w http.ResponseWriter, r *http.Request) {
payload := r.FormValue("payload")
var action slack.ActionPayload
if err := json.Unmarshal([]byte(payload), &action); err != nil {
log.Printf("invalid action json: %v", err)
http.Error(w, http.StatusText(400), 400)
return
}
if action.CallbackID != callbackID {
log.Printf("invalid callback_id: %s", action.CallbackID)
http.Error(w, http.StatusText(400), 400)
return
}
id, err := untappd.Info(action.Actions[0].Value)
if err != nil {
log.Printf("%+v", err)
http.Error(w, http.StatusText(500), 500)
return
}
msg := slack.NewMessage()
attachment := &slack.Attachment{
Title: id.Title(),
Text: id.Text(),
ImageURL: id.Response.Beer.Label,
}
msg.Attachments = append(msg.Attachments, attachment)
b, err := json.Marshal(msg)
if err != nil {
log.Printf("%+v", err)
http.Error(w, http.StatusText(500), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
searchText := r.FormValue("text")
if searchText == "" {
log.Printf("missing form value: text")
http.Error(w, http.StatusText(400), 400)
return
}
ud, err := untappd.Search(searchText)
if err != nil {
log.Printf("%+v", err)
http.Error(w, http.StatusText(500), 500)
return
}
msg := slack.NewMessage()
if len(ud.Response.Beers.Items) != 0 {
var count int
for _, item := range ud.Response.Beers.Items {
count++
attachment := &slack.Attachment{
Title: item.Title(),
CallbackID: callbackID,
Actions: []*slack.Action{slack.NewAction(item.Beer.ID)},
}
msg.Attachments = append(msg.Attachments, attachment)
if count == MaxResults {
break
}
}
} else {
msg = slack.NewEmptyMessage()
}
b, err := json.Marshal(msg)
if err != nil {
log.Printf("%+v", err)
http.Error(w, http.StatusText(500), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func main() {
stopChan := make(chan os.Signal)
signal.Notify(stopChan, os.Interrupt)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
addr := fmt.Sprintf(":%s", port)
http.Handle("/", isAuthorized(http.HandlerFunc(searchHandler)))
http.Handle("/select", http.HandlerFunc(selectHandler))
server := &http.Server{Addr: addr}
go func() {
log.Printf("slappd listening on 0.0.0.0%s", addr)
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
<-stopChan
log.Printf("slappd shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
server.Shutdown(ctx)
}