1
0
mirror of synced 2024-11-22 04:46:05 +03:00

Add auto-verify

This commit is contained in:
Harrison 2016-04-18 20:03:57 +10:00
parent f8da84d8c9
commit 494b974cb2

View File

@ -40,6 +40,7 @@ type Messenger struct {
messageHandlers []MessageHandler
deliveryHandlers []DeliveryHandler
token string
verifyHandler func(http.ResponseWriter, *http.Request)
}
// New creates a new Messenger. You pass in Options in order to affect settings.
@ -49,11 +50,8 @@ func New(mo Options) *Messenger {
token: mo.Token,
}
if mo.Verify {
m.mux.HandleFunc(WebhookURL, newVerifyHandler(mo.VerifyToken))
} else {
m.mux.HandleFunc(WebhookURL, m.handle)
}
m.verifyHandler = newVerifyHandler(mo.VerifyToken)
m.mux.HandleFunc(WebhookURL, m.handle)
return m
}
@ -98,6 +96,11 @@ func (m *Messenger) ProfileByID(id int64) (Profile, error) {
// handle is the internal HTTP handler for the webhooks.
func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
m.verifyHandler(w, r)
return
}
var rec Receive
err := json.NewDecoder(r.Body).Decode(&rec)