From 47da36229c29075044cea7b9ff9326a59b418591 Mon Sep 17 00:00:00 2001 From: Pranas Kiziela Date: Wed, 28 Nov 2018 15:28:08 +0200 Subject: [PATCH 1/2] Allow specifying which profile fields to pull --- messenger.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/messenger.go b/messenger.go index 112e4e4..03e578f 100644 --- a/messenger.go +++ b/messenger.go @@ -16,8 +16,6 @@ const ( // ProfileURL is the API endpoint used for retrieving profiles. // Used in the form: https://graph.facebook.com/v2.6/?fields=&access_token= ProfileURL = "https://graph.facebook.com/v2.6/" - // ProfileFields is a list of JSON field names which will be populated by the profile query. - ProfileFields = "first_name,last_name,profile_pic,locale,timezone,gender" // SendSettingsURL is API endpoint for saving settings. SendSettingsURL = "https://graph.facebook.com/v2.6/me/thread_settings" @@ -27,6 +25,10 @@ const ( MessengerProfileURL = "https://graph.facebook.com/v2.6/me/messenger_profile" ) +var ( + defaultProfileFields = []string{"first_name", "last_name", "profile_pic", "locale", "timezone", "gender"} +) + // Options are the settings used when creating a Messenger client. type Options struct { // Verify sets whether or not to be in the "verify" mode. Used for @@ -151,8 +153,9 @@ func (m *Messenger) Handler() http.Handler { return m.mux } -// ProfileByID retrieves the Facebook user associated with that ID -func (m *Messenger) ProfileByID(id int64) (Profile, error) { +// ProfileByID retrieves the Facebook user profile associated with that ID +// when no profile fields are specified it defaults to defaultProfileFields +func (m *Messenger) ProfileByID(id int64, profileFields ...string) (Profile, error) { p := Profile{} url := fmt.Sprintf("%v%v", ProfileURL, id) @@ -161,7 +164,13 @@ func (m *Messenger) ProfileByID(id int64) (Profile, error) { return p, err } - req.URL.RawQuery = "fields=" + ProfileFields + "&access_token=" + m.token + if len(profileFields) == 0 { + profileFields = defaultProfileFields + } + + fields := strings.Join(profileFields, ",") + + req.URL.RawQuery = "fields=" + fields + "&access_token=" + m.token client := &http.Client{} resp, err := client.Do(req) From 8b09ea69e9003ef356a43482a1fcac8f35e6514b Mon Sep 17 00:00:00 2001 From: Harrison Shoebridge Date: Sun, 16 Dec 2018 08:43:53 +1100 Subject: [PATCH 2/2] Update comments --- messenger.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/messenger.go b/messenger.go index 03e578f..8f39017 100644 --- a/messenger.go +++ b/messenger.go @@ -26,6 +26,7 @@ const ( ) var ( + // NOTE: If you change this slice you should update the comment on the ProfileByID function below too. defaultProfileFields = []string{"first_name", "last_name", "profile_pic", "locale", "timezone", "gender"} ) @@ -154,7 +155,15 @@ func (m *Messenger) Handler() http.Handler { } // ProfileByID retrieves the Facebook user profile associated with that ID -// when no profile fields are specified it defaults to defaultProfileFields +// when no profile fields are specified it uses some sane defaults. +// +// These default fields are: +// - First name +// - Last name +// - Profile picture +// - Locale +// - Timezone +// - Gender func (m *Messenger) ProfileByID(id int64, profileFields ...string) (Profile, error) { p := Profile{} url := fmt.Sprintf("%v%v", ProfileURL, id)