-
Notifications
You must be signed in to change notification settings - Fork 27
/
profile.go
61 lines (56 loc) · 1.97 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package messenger
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
// Profile struct holds data associated with Facebook profile
type Profile struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ProfilePicture string `json:"profile_pic,omitempty"`
Locale string `json:"locale,omitempty"`
Timezone float64 `json:"timezone,omitempty"`
Gender string `json:"gender,omitempty"`
}
// GetProfile fetches the recipient's profile from facebook platform
// Non empty UserID has to be specified in order to receive the information
func (m *Messenger) GetProfile(userID string) (*Profile, error) {
resp, err := m.doRequest("GET", fmt.Sprintf(GraphAPI+"/v2.6/%s?fields=first_name,last_name,profile_pic,locale,timezone,gender", userID), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
read, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
er := new(rawError)
json.Unmarshal(read, er)
return nil, errors.New("Error occured: " + er.Error.Message)
}
profile := new(Profile)
return profile, json.Unmarshal(read, profile)
}
type accountLinking struct {
//Recipient is Page Scoped ID
Recipient string `json:"recipient"`
}
// GetPSID fetches user's page scoped id during authentication flow
// one must supply a valid and not expired authentication token provided by facebook
// https://developers.facebook.com/docs/messenger-platform/account-linking/authentication
func (m *Messenger) GetPSID(token string) (*string, error) {
resp, err := m.doRequest("GET", fmt.Sprintf(GraphAPI+"/v2.6/me?fields=recipient&account_linking_token=%s", token), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
read, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
er := new(rawError)
json.Unmarshal(read, er)
return nil, errors.New("Error occured: " + er.Error.Message)
}
acc := new(accountLinking)
return &acc.Recipient, json.Unmarshal(read, acc)
}