1
0
mirror of synced 2024-11-28 15:56:02 +03:00

Adds the Referral webhook and PostBacks's referral structure (#26)

This commit is contained in:
Michele Gargiulo 2017-02-17 03:29:12 +01:00 committed by Harrison Shoebridge
parent 998c23c3e9
commit bbd304b464
4 changed files with 47 additions and 0 deletions

View File

@ -18,4 +18,6 @@ const (
PostBackAction
// OptInAction represents opting in through the Send to Messenger button
OptInAction
// ReferralAction represents ?ref parameter in m.me URLs
ReferralAction
)

View File

@ -56,6 +56,8 @@ type PostBack struct {
Time time.Time `json:"-"`
// PostBack ID
Payload string `json:"payload"`
// Optional referral info
Referral Referral `json:"referral"`
}
// Watermark is the RawWatermark timestamp rendered as a time.Time.

View File

@ -50,6 +50,9 @@ type PostBackHandler func(PostBack, *Response)
// OptInHandler is a handler used to handle opt-ins.
type OptInHandler func(OptIn, *Response)
// ReferralHandler is a handler used postback callbacks.
type ReferralHandler func(ReferralMessage, *Response)
// Messenger is the client which manages communication with the Messenger Platform API.
type Messenger struct {
mux *http.ServeMux
@ -58,6 +61,7 @@ type Messenger struct {
readHandlers []ReadHandler
postBackHandlers []PostBackHandler
optInHandlers []OptInHandler
referralHandlers []ReferralHandler
token string
verifyHandler func(http.ResponseWriter, *http.Request)
}
@ -112,6 +116,11 @@ func (m *Messenger) HandlePostBack(f PostBackHandler) {
m.postBackHandlers = append(m.postBackHandlers, f)
}
// HandleReferral adds a new ReferralHandler to the Messenger
func (m *Messenger) HandleReferral(f ReferralHandler) {
m.referralHandlers = append(m.referralHandlers, f)
}
// Handler returns the Messenger in HTTP client form.
func (m *Messenger) Handler() http.Handler {
return m.mux
@ -295,6 +304,14 @@ func (m *Messenger) dispatch(r Receive) {
message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0)
f(message, resp)
}
case ReferralAction:
for _, f := range m.referralHandlers {
message := *info.ReferralMessage
message.Sender = info.Sender
message.Recipient = info.Recipient
message.Time = time.Unix(info.Timestamp/int64(time.Microsecond), 0)
f(message, resp)
}
}
}
}
@ -345,6 +362,8 @@ func (m *Messenger) classify(info MessageInfo, e Entry) Action {
return PostBackAction
} else if info.OptIn != nil {
return OptInAction
} else if info.ReferralMessage != nil {
return ReferralAction
}
return UnknownAction
}

View File

@ -41,6 +41,8 @@ type MessageInfo struct {
Read *Read `json:"read"`
OptIn *OptIn `json:"optin"`
ReferralMessage *ReferralMessage `json:"referral"`
}
type OptIn struct {
@ -54,6 +56,28 @@ type OptIn struct {
Ref string `json:"ref"`
}
// ReferralMessage represents referral endpoint
type ReferralMessage struct {
*Referral
// 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:"-"`
}
// Referral represents referral info
type Referral struct {
// Data originally passed in the ref param
Ref string `json:"ref"`
// Source type
Source string `json:"source"`
// The identifier dor the referral
Type string `json:"type"`
}
// Sender is who the message was sent from.
type Sender struct {
ID int64 `json:"id,string"`