1
0
mirror of synced 2024-11-26 06:46:04 +03:00

Add method to handle OptIn messages (#21)

This commit is contained in:
Machiel 2016-12-08 21:52:21 +01:00 committed by Harrison Shoebridge
parent fc8cdbb86f
commit a5faf5be5c
3 changed files with 37 additions and 0 deletions

View File

@ -16,4 +16,6 @@ const (
ReadAction ReadAction
// PostBackAction represents post call back // PostBackAction represents post call back
PostBackAction PostBackAction
// OptInAction represents opting in through the Send to Messenger button
OptInAction
) )

View File

@ -47,6 +47,9 @@ type ReadHandler func(Read, *Response)
// PostBackHandler is a handler used postback callbacks. // PostBackHandler is a handler used postback callbacks.
type PostBackHandler func(PostBack, *Response) type PostBackHandler func(PostBack, *Response)
// OptInHandler is a handler used to handle opt-ins.
type OptInHandler func(OptIn, *Response)
// Messenger is the client which manages communication with the Messenger Platform API. // Messenger is the client which manages communication with the Messenger Platform API.
type Messenger struct { type Messenger struct {
mux *http.ServeMux mux *http.ServeMux
@ -54,6 +57,7 @@ type Messenger struct {
deliveryHandlers []DeliveryHandler deliveryHandlers []DeliveryHandler
readHandlers []ReadHandler readHandlers []ReadHandler
postBackHandlers []PostBackHandler postBackHandlers []PostBackHandler
optInHandlers []OptInHandler
token string token string
verifyHandler func(http.ResponseWriter, *http.Request) verifyHandler func(http.ResponseWriter, *http.Request)
} }
@ -91,6 +95,12 @@ func (m *Messenger) HandleDelivery(f DeliveryHandler) {
m.deliveryHandlers = append(m.deliveryHandlers, f) m.deliveryHandlers = append(m.deliveryHandlers, f)
} }
// HandleOptIn adds a new OptInHandler to the Messenger which will be triggered
// once a user opts in to communicate with the bot.
func (m *Messenger) HandleOptIn(f OptInHandler) {
m.optInHandlers = append(m.optInHandlers, f)
}
// HandleRead adds a new DeliveryHandler to the Messenger which will be triggered // HandleRead adds a new DeliveryHandler to the Messenger which will be triggered
// when a previously sent message is read by the recipient. // when a previously sent message is read by the recipient.
func (m *Messenger) HandleRead(f ReadHandler) { func (m *Messenger) HandleRead(f ReadHandler) {
@ -277,6 +287,14 @@ func (m *Messenger) dispatch(r Receive) {
message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0) message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0)
f(message, resp) f(message, resp)
} }
case OptInAction:
for _, f := range m.optInHandlers {
message := *info.OptIn
message.Sender = info.Sender
message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0)
f(message, resp)
}
} }
} }
} }
@ -325,6 +343,8 @@ func (m *Messenger) classify(info MessageInfo, e Entry) Action {
return ReadAction return ReadAction
} else if info.PostBack != nil { } else if info.PostBack != nil {
return PostBackAction return PostBackAction
} else if info.OptIn != nil {
return OptInAction
} }
return UnknownAction return UnknownAction
} }

View File

@ -1,5 +1,7 @@
package messenger package messenger
import "time"
// Receive is the format in which webhook events are sent. // Receive is the format in which webhook events are sent.
type Receive struct { type Receive struct {
// Object should always be `page`. (I don't quite understand why) // Object should always be `page`. (I don't quite understand why)
@ -37,6 +39,19 @@ type MessageInfo struct {
PostBack *PostBack `json:"postback"` PostBack *PostBack `json:"postback"`
Read *Read `json:"read"` Read *Read `json:"read"`
OptIn *OptIn `json:"optin"`
}
type OptIn struct {
// Sender is the sender of the message
Sender Sender `json:"-"`
// Recipient is who the message was sent to.
Recipient Recipient `json:"-"`
// Time is when the message was sent.
Time time.Time `json:"-"`
// Ref is the reference as given
Ref string `json:"ref"`
} }
// Sender is who the message was sent from. // Sender is who the message was sent from.