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

Rewrite handlers management

Having everything under the one hood was not working. Doing this we can
parse more information in a neater fashion
This commit is contained in:
Harrison Shoebridge 2016-04-13 19:26:31 +10:00
parent d63af22da1
commit ce641cb233
3 changed files with 34 additions and 31 deletions

View File

@ -29,13 +29,13 @@ func main() {
Token: *pageToken, Token: *pageToken,
}) })
m.Handle(messenger.TextAction, func(m messenger.Message, r *messenger.Response) { m.HandleMessage(func(m messenger.Message, r *messenger.Response) {
fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate)) fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate))
fmt.Println(r.Text("Hello, World!")) fmt.Println(r.Text("Hello, World!"))
}) })
m.Handle(messenger.DeliveryAction, func(m messenger.Message, r *messenger.Response) { m.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) {
fmt.Println(m.Delivery.Watermark.Format(time.UnixDate)) fmt.Println(d.Watermark.Format(time.UnixDate))
}) })
fmt.Println("Serving messenger bot on localhost:8080") fmt.Println("Serving messenger bot on localhost:8080")

View File

@ -8,7 +8,6 @@ type Message struct {
Time time.Time Time time.Time
Text string Text string
Seq int Seq int
Delivery *Delivery
} }
type Delivery struct { type Delivery struct {

View File

@ -18,18 +18,19 @@ type MessengerOptions struct {
} }
type MessageHandler func(Message, *Response) type MessageHandler func(Message, *Response)
type DeliveryHandler func(Delivery, *Response)
type Messenger struct { type Messenger struct {
mux *http.ServeMux mux *http.ServeMux
handlers map[Action]MessageHandler messageHandlers []MessageHandler
token string deliveryHandlers []DeliveryHandler
token string
} }
func New(mo MessengerOptions) *Messenger { func New(mo MessengerOptions) *Messenger {
m := &Messenger{ m := &Messenger{
mux: http.NewServeMux(), mux: http.NewServeMux(),
handlers: make(map[Action]MessageHandler), token: mo.Token,
token: mo.Token,
} }
if mo.Verify { if mo.Verify {
@ -41,8 +42,12 @@ func New(mo MessengerOptions) *Messenger {
return m return m
} }
func (m *Messenger) Handle(a Action, f MessageHandler) { func (m *Messenger) HandleMessage(f MessageHandler) {
m.handlers[a] = f m.messageHandlers = append(m.messageHandlers, f)
}
func (m *Messenger) HandleDelivery(f DeliveryHandler) {
m.deliveryHandlers = append(m.deliveryHandlers, f)
} }
func (m *Messenger) Handler() http.Handler { func (m *Messenger) Handler() http.Handler {
@ -78,30 +83,29 @@ func (m *Messenger) dispatch(r Receive) {
continue continue
} }
if f := m.handlers[a]; f != nil { resp := &Response{
message := Message{ to: Recipient{info.Sender.ID},
Sender: info.Sender, token: m.token,
Recipient: info.Recipient, }
Time: time.Unix(info.Timestamp, 0),
}
switch a { switch a {
case TextAction: case TextAction:
message.Text = info.Message.Text for _, f := range m.messageHandlers {
case DeliveryAction: f(Message{
message.Delivery = &Delivery{ 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{
Mids: info.Delivery.Mids, Mids: info.Delivery.Mids,
Seq: info.Delivery.Seq, Seq: info.Delivery.Seq,
Watermark: time.Unix(info.Delivery.Watermark, 0), Watermark: time.Unix(info.Delivery.Watermark, 0),
} }, resp)
} }
response := &Response{
to: Recipient{info.Sender.ID},
token: m.token,
}
f(message, response)
} }
} }
} }