diff --git a/actions.go b/actions.go index 6f122fc..45beea1 100644 --- a/actions.go +++ b/actions.go @@ -16,4 +16,6 @@ const ( ReadAction // PostBackAction represents post call back PostBackAction + // OptInAction represents opting in through the Send to Messenger button + OptInAction ) diff --git a/messenger.go b/messenger.go index 36445ec..e1dd158 100644 --- a/messenger.go +++ b/messenger.go @@ -47,6 +47,9 @@ type ReadHandler func(Read, *Response) // PostBackHandler is a handler used postback callbacks. 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. type Messenger struct { mux *http.ServeMux @@ -54,6 +57,7 @@ type Messenger struct { deliveryHandlers []DeliveryHandler readHandlers []ReadHandler postBackHandlers []PostBackHandler + optInHandlers []OptInHandler token string verifyHandler func(http.ResponseWriter, *http.Request) } @@ -91,6 +95,12 @@ func (m *Messenger) HandleDelivery(f DeliveryHandler) { 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 // when a previously sent message is read by the recipient. 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) 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 } else if info.PostBack != nil { return PostBackAction + } else if info.OptIn != nil { + return OptInAction } return UnknownAction } diff --git a/receiving.go b/receiving.go index b8565d7..a068a71 100644 --- a/receiving.go +++ b/receiving.go @@ -1,5 +1,7 @@ package messenger +import "time" + // Receive is the format in which webhook events are sent. type Receive struct { // Object should always be `page`. (I don't quite understand why) @@ -37,6 +39,19 @@ type MessageInfo struct { PostBack *PostBack `json:"postback"` 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.