2016-04-13 05:03:26 +03:00
|
|
|
package messenger
|
|
|
|
|
|
|
|
import (
|
2016-04-13 09:14:23 +03:00
|
|
|
"encoding/json"
|
2016-04-13 05:03:26 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-04-13 09:14:23 +03:00
|
|
|
"time"
|
2016-04-13 05:03:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
WebhookURL = "/webhook"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MessengerOptions struct {
|
|
|
|
Verify bool
|
|
|
|
VerifyToken string
|
2016-04-13 10:01:42 +03:00
|
|
|
Token string
|
2016-04-13 05:03:26 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 10:01:42 +03:00
|
|
|
type MessageHandler func(Message, *Response)
|
2016-04-13 12:26:31 +03:00
|
|
|
type DeliveryHandler func(Delivery, *Response)
|
2016-04-13 09:14:23 +03:00
|
|
|
|
2016-04-13 05:03:26 +03:00
|
|
|
type Messenger struct {
|
2016-04-13 12:26:31 +03:00
|
|
|
mux *http.ServeMux
|
|
|
|
messageHandlers []MessageHandler
|
|
|
|
deliveryHandlers []DeliveryHandler
|
|
|
|
token string
|
2016-04-13 05:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(mo MessengerOptions) *Messenger {
|
|
|
|
m := &Messenger{
|
2016-04-13 12:26:31 +03:00
|
|
|
mux: http.NewServeMux(),
|
|
|
|
token: mo.Token,
|
2016-04-13 05:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if mo.Verify {
|
|
|
|
m.mux.HandleFunc(WebhookURL, newVerifyHandler(mo.VerifyToken))
|
2016-04-13 09:14:23 +03:00
|
|
|
} else {
|
|
|
|
m.mux.HandleFunc(WebhookURL, m.handle)
|
2016-04-13 05:03:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2016-04-13 12:26:31 +03:00
|
|
|
func (m *Messenger) HandleMessage(f MessageHandler) {
|
|
|
|
m.messageHandlers = append(m.messageHandlers, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) HandleDelivery(f DeliveryHandler) {
|
|
|
|
m.deliveryHandlers = append(m.deliveryHandlers, f)
|
2016-04-13 09:14:23 +03:00
|
|
|
}
|
|
|
|
|
2016-04-13 05:03:26 +03:00
|
|
|
func (m *Messenger) Handler() http.Handler {
|
|
|
|
return m.mux
|
|
|
|
}
|
|
|
|
|
2016-04-13 09:14:23 +03:00
|
|
|
func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var rec Receive
|
|
|
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&rec)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
fmt.Fprintln(w, `{status: 'not ok'}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.Object != "page" {
|
|
|
|
fmt.Println("Object is not page, undefined behaviour. Got", rec.Object)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.dispatch(rec)
|
|
|
|
|
|
|
|
fmt.Fprintln(w, `{status: 'ok'}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) dispatch(r Receive) {
|
|
|
|
for _, entry := range r.Entry {
|
|
|
|
for _, info := range entry.Messaging {
|
|
|
|
a := m.classify(info, entry)
|
|
|
|
if a == UnknownAction {
|
|
|
|
fmt.Println("Unknown action:", info)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-13 12:26:31 +03:00
|
|
|
resp := &Response{
|
|
|
|
to: Recipient{info.Sender.ID},
|
|
|
|
token: m.token,
|
|
|
|
}
|
2016-04-13 12:12:23 +03:00
|
|
|
|
2016-04-13 12:26:31 +03:00
|
|
|
switch a {
|
|
|
|
case TextAction:
|
|
|
|
for _, f := range m.messageHandlers {
|
|
|
|
f(Message{
|
|
|
|
Sender: info.Sender,
|
|
|
|
Recipient: info.Recipient,
|
|
|
|
Time: time.Unix(info.Timestamp, 0),
|
|
|
|
Text: info.Message.Text,
|
|
|
|
}, resp)
|
|
|
|
}
|
|
|
|
case DeliveryAction:
|
|
|
|
for _, f := range m.deliveryHandlers {
|
|
|
|
f(Delivery{
|
2016-04-13 12:12:23 +03:00
|
|
|
Mids: info.Delivery.Mids,
|
|
|
|
Seq: info.Delivery.Seq,
|
|
|
|
Watermark: time.Unix(info.Delivery.Watermark, 0),
|
2016-04-13 12:26:31 +03:00
|
|
|
}, resp)
|
2016-04-13 10:01:42 +03:00
|
|
|
}
|
2016-04-13 09:14:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
|
|
|
|
if info.Message != nil {
|
|
|
|
return TextAction
|
2016-04-13 12:12:23 +03:00
|
|
|
} else if info.Delivery != nil {
|
|
|
|
return DeliveryAction
|
2016-04-13 09:14:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownAction
|
|
|
|
}
|
|
|
|
|
2016-04-13 05:03:26 +03:00
|
|
|
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.")
|
|
|
|
}
|
|
|
|
}
|