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

Commit 47f193f

Browse files
committed
* Added ability to set greeting setting, get started setting and persistent menu setting
* Added IsEcho in Message which is indicator of own messages * Added QuickReply in Message for sending of quick reply buttons with message * Added mux parameter in Messenger constructor. It is useful when single go app processes the several bots * Added Messenger.SenderAction which allows to show writing and reading status
1 parent 500eec9 commit 47f193f

File tree

5 files changed

+155
-11
lines changed

5 files changed

+155
-11
lines changed

message.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type Message struct {
1010
Recipient Recipient `json:"-"`
1111
// Time is when the message was sent.
1212
Time time.Time `json:"-"`
13+
// Message is mine
14+
IsEcho bool `json:"is_echo,omitempty"`
1315
// Mid is the ID of the message.
1416
Mid string `json:"mid"`
1517
// Seq is order the message was sent in relation to other messages.
@@ -19,6 +21,8 @@ type Message struct {
1921
// Attachments is the information about the attachments which were sent
2022
// with the message.
2123
Attachments []Attachment `json:"attachments"`
24+
// Selected quick reply
25+
QuickReply *QuickReply `json:"quick_reply,omitempty"`
2226
}
2327

2428
// Delivery represents a the event fired when Facebook delivers a message to the

messenger.go

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package messenger
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -11,6 +12,8 @@ const (
1112
// ProfileURL is the API endpoint used for retrieving profiles.
1213
// Used in the form: https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>
1314
ProfileURL = "https://graph.facebook.com/v2.6/"
15+
// SendSettingsURL is API endpoint for saving settings.
16+
SendSettingsURL = "https://graph.facebook.com/v2.6/me/thread_settings"
1417
)
1518

1619
// Options are the settings used when creating a Messenger client.
@@ -25,6 +28,8 @@ type Options struct {
2528
Token string
2629
// WebhookURL is where the Messenger client should listen for webhook events. Leaving the string blank implies a path of "/".
2730
WebhookURL string
31+
// Mux is shared mux between several Messenger objects
32+
Mux *http.ServeMux
2833
}
2934

3035
// MessageHandler is a handler used for responding to a message containing text.
@@ -52,8 +57,12 @@ type Messenger struct {
5257

5358
// New creates a new Messenger. You pass in Options in order to affect settings.
5459
func New(mo Options) *Messenger {
60+
if mo.Mux == nil {
61+
mo.Mux = http.NewServeMux()
62+
}
63+
5564
m := &Messenger{
56-
mux: http.NewServeMux(),
65+
mux: mo.Mux,
5766
token: mo.Token,
5867
}
5968

@@ -116,6 +125,65 @@ func (m *Messenger) ProfileByID(id int64) (Profile, error) {
116125
return p, err
117126
}
118127

128+
// GreetingSetting sends settings for greeting
129+
func (m *Messenger) GreetingSetting(text string) error {
130+
d := GreetingSetting{
131+
SettingType: "greeting",
132+
Greeting: GreetingInfo{
133+
Text: text,
134+
},
135+
}
136+
137+
data, err := json.Marshal(d)
138+
if err != nil {
139+
return err
140+
}
141+
142+
req, err := http.NewRequest("POST", SendSettingsURL, bytes.NewBuffer(data))
143+
if err != nil {
144+
return err
145+
}
146+
147+
req.Header.Set("Content-Type", "application/json")
148+
req.URL.RawQuery = "access_token=" + m.token
149+
150+
client := &http.Client{}
151+
152+
resp, err := client.Do(req)
153+
defer resp.Body.Close()
154+
155+
return err
156+
}
157+
158+
// CallToActionsSetting sends settings for Get Started or Persist Menu
159+
func (m *Messenger) CallToActionsSetting(state string, actions []CallToActionsItem) error {
160+
d := CallToActionsSetting{
161+
SettingType: "call_to_actions",
162+
ThreadState: state,
163+
CallToActions: actions,
164+
}
165+
166+
data, err := json.Marshal(d)
167+
if err != nil {
168+
return err
169+
}
170+
171+
req, err := http.NewRequest("POST", SendSettingsURL, bytes.NewBuffer(data))
172+
if err != nil {
173+
return err
174+
}
175+
176+
req.Header.Set("Content-Type", "application/json")
177+
req.URL.RawQuery = "access_token=" + m.token
178+
179+
client := &http.Client{}
180+
181+
resp, err := client.Do(req)
182+
defer resp.Body.Close()
183+
184+
return err
185+
}
186+
119187
// handle is the internal HTTP handler for the webhooks.
120188
func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
121189
if r.Method == "GET" {
@@ -186,6 +254,14 @@ func (m *Messenger) dispatch(r Receive) {
186254
}
187255
}
188256

257+
// Response returns new Response object
258+
func (m *Messenger) Response(to int64) *Response {
259+
return &Response{
260+
to: Recipient{to},
261+
token: m.token,
262+
}
263+
}
264+
189265
// classify determines what type of message a webhook event is.
190266
func (m *Messenger) classify(info MessageInfo, e Entry) Action {
191267
if info.Message != nil {

receiving.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ type Attachment struct {
6060
// QuickReply is a file which used in a message.
6161
type QuickReply struct {
6262
// ContentType is the type of reply
63-
ContentType string `json:"content_type"`
63+
ContentType string `json:"content_type,omitempty"`
6464
// Title is the reply title
65-
Title string `json:"title"`
65+
Title string `json:"title,omitempty"`
6666
// Payload is the reply information
6767
Payload string `json:"payload"`
6868
}

response.go

+45-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
3939

4040
data, err := json.Marshal(m)
4141
if err != nil {
42-
return nil
42+
return err
4343
}
4444

4545
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
@@ -122,7 +122,7 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
122122

123123
data, err := json.Marshal(m)
124124
if err != nil {
125-
return nil
125+
return err
126126
}
127127

128128
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
@@ -142,7 +142,7 @@ func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButto
142142
}
143143

144144
// GenericTemplate is a message which allows for structural elements to be sent
145-
func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageElement) error {
145+
func (r *Response) GenericTemplate(elements *[]StructuredMessageElement) error {
146146
m := SendStructuredMessage{
147147
Recipient: r.to,
148148
Message: StructuredMessageData{
@@ -159,7 +159,35 @@ func (r *Response) GenericTemplate(text string, elements *[]StructuredMessageEle
159159

160160
data, err := json.Marshal(m)
161161
if err != nil {
162-
return nil
162+
return err
163+
}
164+
165+
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
166+
if err != nil {
167+
return err
168+
}
169+
170+
req.Header.Set("Content-Type", "application/json")
171+
req.URL.RawQuery = "access_token=" + r.token
172+
173+
client := &http.Client{}
174+
175+
resp, err := client.Do(req)
176+
defer resp.Body.Close()
177+
178+
return err
179+
}
180+
181+
// SenderAction sends a info about sender action
182+
func (r *Response) SenderAction(action string) error {
183+
m := SendSenderAction{
184+
Recipient: r.to,
185+
SenderAction: action,
186+
}
187+
188+
data, err := json.Marshal(m)
189+
if err != nil {
190+
return err
163191
}
164192

165193
req, err := http.NewRequest("POST", SendMessageURL, bytes.NewBuffer(data))
@@ -212,23 +240,32 @@ type StructuredMessageAttachment struct {
212240
// StructuredMessagePayload is the actual payload of an attachment
213241
type StructuredMessagePayload struct {
214242
// TemplateType must be button, generic or receipt
215-
TemplateType string `json:"template_type"`
243+
TemplateType string `json:"template_type,omitempty"`
216244
Text string `json:"text,omitempty"`
217245
Elements *[]StructuredMessageElement `json:"elements,omitempty"`
218246
Buttons *[]StructuredMessageButton `json:"buttons,omitempty"`
247+
Url string `json:"url,omitempty"`
219248
}
220249

221250
// StructuredMessageElement is a response containing structural elements
222251
type StructuredMessageElement struct {
223252
Title string `json:"title"`
224253
ImageURL string `json:"image_url"`
254+
ItemURL string `json:"item_url"`
225255
Subtitle string `json:"subtitle"`
226256
Buttons []StructuredMessageButton `json:"buttons"`
227257
}
228258

229259
// StructuredMessageButton is a response containing buttons
230260
type StructuredMessageButton struct {
231-
Type string `json:"type"`
232-
URL string `json:"url"`
233-
Title string `json:"title"`
261+
Type string `json:"type"`
262+
URL string `json:"url,omitempty"`
263+
Title string `json:"title"`
264+
Payload string `json:"payload,omitempty"`
265+
}
266+
267+
// SendSenderAction is the information about sender action
268+
type SendSenderAction struct {
269+
Recipient Recipient `json:"recipient"`
270+
SenderAction string `json:"sender_action"`
234271
}

settings.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package messenger
2+
3+
// GreetingSetting is the setting for greeting message
4+
type GreetingSetting struct {
5+
SettingType string `json:"setting_type"`
6+
Greeting GreetingInfo `json:"greeting"`
7+
}
8+
9+
// GreetingInfo contains greeting message
10+
type GreetingInfo struct {
11+
Text string `json:"text"`
12+
}
13+
14+
// CallToActionsSetting is the settings for Get Started and Persist Menu
15+
type CallToActionsSetting struct {
16+
SettingType string `json:"setting_type"`
17+
ThreadState string `json:"thread_state"`
18+
CallToActions []CallToActionsItem `json:"call_to_actions"`
19+
}
20+
21+
// CallToActionsItem contains Get Started button or item of Persist Menu
22+
type CallToActionsItem struct {
23+
Type string `json:"type,omitempty"`
24+
Title string `json:"title,omitempty"`
25+
Payload string `json:"payload,omitempty"`
26+
URL string `json:"url,omitempty"`
27+
}

0 commit comments

Comments
 (0)