diff --git a/messenger.go b/messenger.go index 112e4e4..8f39017 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,11 @@ const ( MessengerProfileURL = "https://graph.facebook.com/v2.6/me/messenger_profile" ) +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"} +) + // 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 +154,17 @@ 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 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) @@ -161,7 +173,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)