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:
parent
db34107d5b
commit
8f272bbf30
31
cmd/bot/main.go
Normal file
31
cmd/bot/main.go
Normal 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
46
messenger.go
Normal 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.")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user