1
0
mirror of synced 2024-11-22 04:46:05 +03:00
messenger/messenger.go
Harrison Shoebridge 8f272bbf30 Add Messenger bot capable of verification
The only ability of this current bot is to verify the webhook on the
Facebook developer panel.
2016-04-13 12:03:26 +10:00

47 lines
757 B
Go

package messenger
import (
"fmt"
"net/http"
)
const (
WebhookURL = "/webhook"
)
type MessengerOptions struct {
Verify bool
VerifyToken string
}
type Messenger struct {
mux *http.ServeMux
}
func New(mo MessengerOptions) *Messenger {
m := &Messenger{
mux: http.NewServeMux(),
}
if mo.Verify {
m.mux.HandleFunc(WebhookURL, newVerifyHandler(mo.VerifyToken))
}
return m
}
func (m *Messenger) Handler() http.Handler {
return m.mux
}
func newVerifyHandler(token string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("hub.verify_token") == token {
fmt.Fprintln(w, r.FormValue("hub.challenge"))
return
}
fmt.Fprintln(w, "Incorrect verify token.")
}
}