1
0
mirror of synced 2024-11-22 21:06:06 +03:00

Add Messenger bot capable of verification

The only ability of this current bot is to verify the webhook on the
Facebook developer panel.
This commit is contained in:
Harrison Shoebridge 2016-04-13 12:03:26 +10:00
parent db34107d5b
commit 8f272bbf30
2 changed files with 77 additions and 0 deletions

31
cmd/bot/main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"fmt"
"net/http"
"github.com/paked/configure"
"github.com/paked/messenger"
)
var (
conf = configure.New()
verifyToken = conf.String("verify-token", "mad-skrilla", "The token used to verify facebook")
verify = conf.Bool("should-verify", false, "Whether or not the app should verify itself")
)
func main() {
conf.Use(configure.NewFlag())
conf.Use(configure.NewEnvironment())
conf.Use(configure.NewJSONFromFile("config.json"))
conf.Parse()
m := messenger.New(messenger.MessengerOptions{
Verify: *verify,
VerifyToken: *verifyToken,
})
fmt.Println("Serving messenger bot on localhost:8080")
http.ListenAndServe("localhost:8080", m.Handler())
}

46
messenger.go Normal file
View File

@ -0,0 +1,46 @@
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.")
}
}