Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Options struct {
WebhookURL string
// Mux is shared mux between several Messenger objects
Mux *http.ServeMux
// Client is custom http.Client used for API requests. Leaving nil uses http.DefaultClient.
Client *http.Client
}

// MessageHandler is a handler used for responding to a message containing text.
Expand Down Expand Up @@ -71,6 +73,7 @@ type AccountLinkingHandler func(AccountLinking, *Response)
// Messenger is the client which manages communication with the Messenger Platform API.
type Messenger struct {
mux *http.ServeMux
client *http.Client
messageHandlers []MessageHandler
deliveryHandlers []DeliveryHandler
readHandlers []ReadHandler
Expand All @@ -90,8 +93,13 @@ func New(mo Options) *Messenger {
mo.Mux = http.NewServeMux()
}

if mo.Client == nil {
mo.Client = http.DefaultClient
}

m := &Messenger{
mux: mo.Mux,
client: mo.Client,
token: mo.Token,
verify: mo.Verify,
appSecret: mo.AppSecret,
Expand Down Expand Up @@ -173,8 +181,7 @@ func (m *Messenger) ProfileByID(id int64, profileFields []string) (Profile, erro

req.URL.RawQuery = "fields=" + fields + "&access_token=" + m.token

client := &http.Client{}
resp, err := client.Do(req)
resp, err := m.client.Do(req)
if err != nil {
return p, err
}
Expand Down Expand Up @@ -359,8 +366,9 @@ func (m *Messenger) dispatch(r Receive) {
}

resp := &Response{
to: Recipient{info.Sender.ID},
token: m.token,
client: m.client,
token: m.token,
to: Recipient{info.Sender.ID},
}

switch a {
Expand Down Expand Up @@ -420,8 +428,9 @@ func (m *Messenger) dispatch(r Receive) {
// Response returns new Response object
func (m *Messenger) Response(to int64) *Response {
return &Response{
to: Recipient{to},
token: m.token,
client: m.client,
token: m.token,
to: Recipient{to},
}
}

Expand All @@ -433,17 +442,19 @@ func (m *Messenger) Send(to Recipient, message string, messagingType MessagingTy
// SendGeneralMessage will send the GenericTemplate message
func (m *Messenger) SendGeneralMessage(to Recipient, elements *[]StructuredMessageElement, messagingType MessagingType, tags ...string) error {
r := &Response{
token: m.token,
to: to,
client: m.client,
token: m.token,
to: to,
}
return r.GenericTemplate(elements, messagingType, tags...)
}

// SendWithReplies sends a textual message to a user, but gives them the option of numerous quick response options.
func (m *Messenger) SendWithReplies(to Recipient, message string, replies []QuickReply, messagingType MessagingType, tags ...string) error {
response := &Response{
token: m.token,
to: to,
client: m.client,
token: m.token,
to: to,
}

return response.TextWithReplies(message, replies, messagingType, tags...)
Expand All @@ -452,8 +463,9 @@ func (m *Messenger) SendWithReplies(to Recipient, message string, replies []Quic
// Attachment sends an image, sound, video or a regular file to a given recipient.
func (m *Messenger) Attachment(to Recipient, dataType AttachmentType, url string, messagingType MessagingType, tags ...string) error {
response := &Response{
token: m.token,
to: to,
client: m.client,
token: m.token,
to: to,
}

return response.Attachment(dataType, url, messagingType, tags...)
Expand Down
17 changes: 13 additions & 4 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ func checkFacebookError(r io.Reader) error {

// Response is used for responding to events with messages.
type Response struct {
token string
to Recipient
client *http.Client
token string
to Recipient
}

// SetToken is for using DispatchMessage from outside.
Expand Down Expand Up @@ -348,7 +349,11 @@ func (r *Response) DispatchMessage(m interface{}) error {
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token

resp, err := http.DefaultClient.Do(req)
client := r.client
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(req)
if err != nil {
return err
}
Expand Down Expand Up @@ -381,7 +386,11 @@ func (r *Response) PassThreadToInbox() error {
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = "access_token=" + r.token

resp, err := http.DefaultClient.Do(req)
client := r.client
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(req)
if err != nil {
return err
}
Expand Down