Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit eb0e72a

Browse files
committed
Api update
Postback action handler Dynamic WebHook Button Template Message Generic Template Message
1 parent 494b974 commit eb0e72a

File tree

5 files changed

+172
-20
lines changed

5 files changed

+172
-20
lines changed

actions.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ const (
1111
// DeliveryAction means that the event was a previous recipient reading their respective
1212
// messages.
1313
DeliveryAction
14+
// PostBackAction represents post call back
15+
PostBackAction
1416
)

message.go

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ type Delivery struct {
3232
Seq int `json:"seq"`
3333
}
3434

35+
// PostBack represents postback callback
36+
type PostBack struct {
37+
// Sender is who the message was sent from.
38+
Sender Sender `json:"-"`
39+
// Recipient is who the message was sent to.
40+
Recipient Recipient `json:"-"`
41+
// Time is when the message was sent.
42+
Time time.Time `json:"-"`
43+
// PostBack ID
44+
Payload string `json:"payload"`
45+
}
46+
3547
// Watermark is the RawWatermark timestamp rendered as a time.Time.
3648
func (d Delivery) Watermark() time.Time {
3749
return time.Unix(d.RawWatermark, 0)

messenger.go

+34-20
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
)
99

1010
const (
11-
// WebhookURL is where the Messenger client should listen for webhook events.
12-
WebhookURL = "/webhook"
13-
1411
// ProfileURL is the API endpoint used for retrieving profiles.
1512
// Used in the form: https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>
1613
ProfileURL = "https://graph.facebook.com/v2.6/"
@@ -26,6 +23,8 @@ type Options struct {
2623
VerifyToken string
2724
// Token is the access token of the Facebook page to send messages from.
2825
Token string
26+
// WebhookURL is where the Messenger client should listen for webhook events.
27+
WebhookURL string
2928
}
3029

3130
// MessageHandler is a handler used for responding to a message containing text.
@@ -34,11 +33,15 @@ type MessageHandler func(Message, *Response)
3433
// DeliveryHandler is a handler used for responding to a read receipt.
3534
type DeliveryHandler func(Delivery, *Response)
3635

36+
// PostBackHandler is a handler used postback callbacks.
37+
type PostBackHandler func(PostBack, *Response)
38+
3739
// Messenger is the client which manages communication with the Messenger Platform API.
3840
type Messenger struct {
3941
mux *http.ServeMux
4042
messageHandlers []MessageHandler
4143
deliveryHandlers []DeliveryHandler
44+
postBackHandlers []PostBackHandler
4245
token string
4346
verifyHandler func(http.ResponseWriter, *http.Request)
4447
}
@@ -51,7 +54,7 @@ func New(mo Options) *Messenger {
5154
}
5255

5356
m.verifyHandler = newVerifyHandler(mo.VerifyToken)
54-
m.mux.HandleFunc(WebhookURL, m.handle)
57+
m.mux.HandleFunc(mo.WebhookURL, m.handle)
5558

5659
return m
5760
}
@@ -68,6 +71,11 @@ func (m *Messenger) HandleDelivery(f DeliveryHandler) {
6871
m.deliveryHandlers = append(m.deliveryHandlers, f)
6972
}
7073

74+
// HandlePostBack adds a new PostBackHandler to the Messenger
75+
func (m *Messenger) HandlePostBack(f PostBackHandler) {
76+
m.postBackHandlers = append(m.postBackHandlers, f)
77+
}
78+
7179
// Handler returns the Messenger in HTTP client form.
7280
func (m *Messenger) Handler() http.Handler {
7381
return m.mux
@@ -106,7 +114,6 @@ func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
106114
err := json.NewDecoder(r.Body).Decode(&rec)
107115
if err != nil {
108116
fmt.Println(err)
109-
110117
fmt.Fprintln(w, `{status: 'not ok'}`)
111118
return
112119
}
@@ -136,19 +143,26 @@ func (m *Messenger) dispatch(r Receive) {
136143
}
137144

138145
switch a {
139-
case TextAction:
140-
for _, f := range m.messageHandlers {
141-
message := *info.Message
142-
message.Sender = info.Sender
143-
message.Recipient = info.Recipient
144-
message.Time = time.Unix(info.Timestamp, 0)
145-
146-
f(message, resp)
147-
}
148-
case DeliveryAction:
149-
for _, f := range m.deliveryHandlers {
150-
f(*info.Delivery, resp)
151-
}
146+
case TextAction:
147+
for _, f := range m.messageHandlers {
148+
message := *info.Message
149+
message.Sender = info.Sender
150+
message.Recipient = info.Recipient
151+
message.Time = time.Unix(info.Timestamp, 0)
152+
f(message, resp)
153+
}
154+
case DeliveryAction:
155+
for _, f := range m.deliveryHandlers {
156+
f(*info.Delivery, resp)
157+
}
158+
case PostBackAction:
159+
for _, f := range m.postBackHandlers {
160+
message := *info.PostBack
161+
message.Sender = info.Sender
162+
message.Recipient = info.Recipient
163+
message.Time = time.Unix(info.Timestamp, 0)
164+
f(message, resp)
165+
}
152166
}
153167
}
154168
}
@@ -160,8 +174,9 @@ func (m *Messenger) classify(info MessageInfo, e Entry) Action {
160174
return TextAction
161175
} else if info.Delivery != nil {
162176
return DeliveryAction
177+
} else if info.PostBack != nil {
178+
return PostBackAction
163179
}
164-
165180
return UnknownAction
166181
}
167182

@@ -172,7 +187,6 @@ func newVerifyHandler(token string) func(w http.ResponseWriter, r *http.Request)
172187
fmt.Fprintln(w, r.FormValue("hub.challenge"))
173188
return
174189
}
175-
176190
fmt.Fprintln(w, "Incorrect verify token.")
177191
}
178192
}

receiving.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type MessageInfo struct {
3333
// Delivery is the contents of a message if it is a DeliveryAction.
3434
// Nil if it is not a DeliveryAction.
3535
Delivery *Delivery `json:"delivery"`
36+
37+
PostBack *PostBack `json:"postback"`
3638
}
3739

3840
// Sender is who the message was sent from.

response.go

+122
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,85 @@ func (r *Response) Image(im image.Image) error {
9797
return nil
9898
}
9999

100+
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
101+
m := SendStructuredMessage {
102+
Recipient: r.to,
103+
Message: StructuredMessageData {
104+
Attachment: StructuredMessageAttachment {
105+
Type: "template",
106+
Payload: StructuredMessagePayload {
107+
TemplateType: "button",
108+
Text: text,
109+
Buttons: buttons,
110+
Elements: nil,
111+
},
112+
},
113+
},
114+
}
115+
116+
fmt.Println(m)
117+
118+
data, err := json.Marshal(m)
119+
if err != nil {
120+
return nil
121+
}
122+
123+
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
124+
if err != nil {
125+
return err
126+
}
127+
128+
req.Header.Set("Content-Type", "application/json")
129+
req.URL.RawQuery = "access_token=" + r.token
130+
131+
client := &http.Client{}
132+
133+
resp, err := client.Do(req)
134+
fmt.Println(resp)
135+
defer resp.Body.Close()
136+
137+
return err
138+
}
139+
140+
func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error {
141+
m := SendStructuredMessage {
142+
Recipient: r.to,
143+
Message: StructuredMessageData {
144+
Attachment: StructuredMessageAttachment {
145+
Type: "template",
146+
Payload: StructuredMessagePayload {
147+
TemplateType: "generic",
148+
Buttons: nil,
149+
Elements: elements,
150+
},
151+
},
152+
},
153+
}
154+
155+
fmt.Println(m)
156+
157+
data, err := json.Marshal(m)
158+
if err != nil {
159+
return nil
160+
}
161+
162+
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
163+
if err != nil {
164+
return err
165+
}
166+
167+
req.Header.Set("Content-Type", "application/json")
168+
req.URL.RawQuery = "access_token=" + r.token
169+
170+
client := &http.Client{}
171+
172+
resp, err := client.Do(req)
173+
fmt.Println(resp)
174+
defer resp.Body.Close()
175+
176+
return err
177+
}
178+
100179
// SendMessage is the information sent in an API request to Facebook.
101180
type SendMessage struct {
102181
Recipient Recipient `json:"recipient"`
@@ -107,3 +186,46 @@ type SendMessage struct {
107186
type MessageData struct {
108187
Text string `json:"text,omitempty"`
109188
}
189+
190+
// SendStructuredMessage is a structured message template
191+
type SendStructuredMessage struct {
192+
Recipient Recipient `json:"recipient"`
193+
Message StructuredMessageData `json:"message"`
194+
}
195+
196+
type StructuredMessageData struct {
197+
Attachment StructuredMessageAttachment `json:"attachment"`
198+
}
199+
200+
type StructuredMessageAttachment struct {
201+
// Template allways
202+
Type string `json:"type"`
203+
// Payload is the information for the file which was sent in the attachment.
204+
Payload StructuredMessagePayload `json:"payload"`
205+
}
206+
207+
type StructuredMessagePayload struct {
208+
// button, generic, receipt
209+
TemplateType string `json:"template_type"`
210+
Text string `json:"text,omitempty"`
211+
Elements *[]StructuredMessageElement `json:"elements,omitempty"`
212+
Buttons *[]StructuredMessageButton `json:"buttons,omitempty"`
213+
}
214+
215+
// StructuredMessageElement - Generic Template
216+
type StructuredMessageElement struct {
217+
Title string `json:"title"`
218+
ImageURL string `json:"image_url"`
219+
Subtitle string `json:"subtitle"`
220+
Buttons []StructuredMessageButton `json:"buttons"`
221+
}
222+
223+
// StructuredMessageButton - Button Template
224+
type StructuredMessageButton struct {
225+
Type string `json:"type"`
226+
URL string `json:"url"`
227+
Title string `json:"title"`
228+
}
229+
230+
231+

0 commit comments

Comments
 (0)