forked from ippy04/messengerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
41 lines (34 loc) · 962 Bytes
/
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
package messengerbot
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
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 int `json:"timezone,omitempty"`
Gender string `json:"gender,omitempty"`
}
func (bot *MessengerBot) GetProfile(userID string) (*Profile, error) {
resp, err := bot.Client.Get(fmt.Sprintf(ProfileAPIEndpoint, userID, bot.AccessToken))
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
fmt.Println(resp.Body)
read, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
er := new(rawError)
json.Unmarshal(read, er)
return nil, errors.New("Error occured fetching profile: " + er.Error.Message)
}
profile := new(Profile)
return profile, json.Unmarshal(read, profile)
}