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

Add handler for AccountLinking process

This commit is contained in:
Samuel El-Borai 2018-03-10 10:12:35 +01:00
parent 88d6aeb77e
commit 0f9e821889
4 changed files with 49 additions and 11 deletions

View File

@ -20,4 +20,7 @@ const (
OptInAction OptInAction
// ReferralAction represents ?ref parameter in m.me URLs // ReferralAction represents ?ref parameter in m.me URLs
ReferralAction ReferralAction
// AccountLinkingAction means that the event concerns changes in account linking
// status.
AccountLinkingAction
) )

View File

@ -60,6 +60,19 @@ type PostBack struct {
Referral Referral `json:"referral"` Referral Referral `json:"referral"`
} }
type AccountLinking struct {
// Sender is who the message was sent from.
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:"-"`
// Status represents the new account linking status.
Status string `json:"status"`
// AuthorizationCode is a pass-through code set during the linking process.
AuthorizationCode string `json:"authorization_code"`
}
// Watermark is the RawWatermark timestamp rendered as a time.Time. // Watermark is the RawWatermark timestamp rendered as a time.Time.
func (d Delivery) Watermark() time.Time { func (d Delivery) Watermark() time.Time {
return time.Unix(d.RawWatermark/int64(time.Microsecond), 0) return time.Unix(d.RawWatermark/int64(time.Microsecond), 0)

View File

@ -59,19 +59,24 @@ type OptInHandler func(OptIn, *Response)
// ReferralHandler is a handler used postback callbacks. // ReferralHandler is a handler used postback callbacks.
type ReferralHandler func(ReferralMessage, *Response) type ReferralHandler func(ReferralMessage, *Response)
// AccountLinkingHandler is a handler used to react to an account
// being linked or unlinked.
type AccountLinkingHandler func(AccountLinking, *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
messageHandlers []MessageHandler messageHandlers []MessageHandler
deliveryHandlers []DeliveryHandler deliveryHandlers []DeliveryHandler
readHandlers []ReadHandler readHandlers []ReadHandler
postBackHandlers []PostBackHandler postBackHandlers []PostBackHandler
optInHandlers []OptInHandler optInHandlers []OptInHandler
referralHandlers []ReferralHandler referralHandlers []ReferralHandler
token string accountLinkingHandlers []AccountLinkingHandler
verifyHandler func(http.ResponseWriter, *http.Request) token string
verify bool verifyHandler func(http.ResponseWriter, *http.Request)
appSecret string verify bool
appSecret string
} }
// New creates a new Messenger. You pass in Options in order to affect settings. // New creates a new Messenger. You pass in Options in order to affect settings.
@ -131,6 +136,11 @@ func (m *Messenger) HandleReferral(f ReferralHandler) {
m.referralHandlers = append(m.referralHandlers, f) m.referralHandlers = append(m.referralHandlers, f)
} }
// HandleAccountLinking adds a new AccountLinkingHandler to the Messenger
func (m *Messenger) HandleAccountLinking(f AccountLinkingHandler) {
m.accountLinkingHandlers = append(m.accountLinkingHandlers, f)
}
// Handler returns the Messenger in HTTP client form. // Handler returns the Messenger in HTTP client form.
func (m *Messenger) Handler() http.Handler { func (m *Messenger) Handler() http.Handler {
return m.mux return m.mux
@ -370,6 +380,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 AccountLinkingAction:
for _, f := range m.accountLinkingHandlers {
message := *info.AccountLinking
message.Sender = info.Sender
message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0)
f(message, resp)
}
} }
} }
} }
@ -431,6 +449,8 @@ func (m *Messenger) classify(info MessageInfo, e Entry) Action {
return OptInAction return OptInAction
} else if info.ReferralMessage != nil { } else if info.ReferralMessage != nil {
return ReferralAction return ReferralAction
} else if info.AccountLinking != nil {
return AccountLinkingAction
} }
return UnknownAction return UnknownAction
} }

View File

@ -43,6 +43,8 @@ type MessageInfo struct {
OptIn *OptIn `json:"optin"` OptIn *OptIn `json:"optin"`
ReferralMessage *ReferralMessage `json:"referral"` ReferralMessage *ReferralMessage `json:"referral"`
AccountLinking *AccountLinking `json:"account_linking"`
} }
type OptIn struct { type OptIn struct {