-
Notifications
You must be signed in to change notification settings - Fork 3
/
webhook.go
110 lines (81 loc) · 2.22 KB
/
webhook.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
package dbl
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type ListenerFunc func(*WebhookPayload)
type WebhookListener struct {
token string
handler ListenerFunc
mux *http.ServeMux
}
type WebhookPayload struct {
// ID of the bot that received a vote
Bot string
// ID of the user who voted
User string
// The type of the vote (should always be "upvote" except when using the test button it's "test")
Type string
// Whether the weekend multiplier is in effect, meaning users votes count as two
IsWeekend bool
// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2
Query url.Values
}
type wPayload struct {
// ID of the bot that received a vote
Bot string `json:"bot"`
// ID of the user who voted
User string `json:"user"`
// The type of the vote (should always be "upvote" except when using the test button it's "test")
Type string `json:"type"`
// Whether the weekend multiplier is in effect, meaning users votes count as two
IsWeekend bool `json:"isWeekend"`
// Query string params found on the /bot/:ID/vote page. Example: ?a=1&b=2
Query string `json:"query"`
}
// Create a new webhook listener
func NewListener(token string, handler func(*WebhookPayload)) *WebhookListener {
return &WebhookListener{
token: token,
handler: ListenerFunc(handler),
}
}
// Starts listening on specific address. A Blocking call.
// Returns non-nil error from ListenAndServe
func (wl *WebhookListener) Serve(addr string) error {
wl.mux = http.NewServeMux()
wl.mux.HandleFunc("/", wl.handlePayload)
return http.ListenAndServe(addr, wl.mux)
}
func (wl *WebhookListener) handlePayload(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if r.Header.Get("Authorization") != wl.token {
w.WriteHeader(http.StatusUnauthorized)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
p := &wPayload{}
if err = json.Unmarshal(body, p); err != nil {
return
}
m, err := url.ParseQuery(p.Query)
if err != nil {
return
}
w.WriteHeader(http.StatusNoContent)
wl.handler(&WebhookPayload{
Bot: p.Bot,
User: p.User,
Type: p.Type,
IsWeekend: p.IsWeekend,
Query: m,
})
}