Add method to handle OptIn messages (#21)
This commit is contained in:
parent
fc8cdbb86f
commit
a5faf5be5c
@ -16,4 +16,6 @@ const (
|
||||
ReadAction
|
||||
// PostBackAction represents post call back
|
||||
PostBackAction
|
||||
// OptInAction represents opting in through the Send to Messenger button
|
||||
OptInAction
|
||||
)
|
||||
|
20
messenger.go
20
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
|
||||
}
|
||||
|
15
receiving.go
15
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.
|
||||
|
Loading…
Reference in New Issue
Block a user