From d5a010c7364a20baf99094737bd0e18aa59de774 Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 15 Apr 2016 10:16:52 +1000 Subject: [PATCH] Add profile retrieving --- cmd/bot/main.go | 17 +++++++++++------ messenger.go | 25 +++++++++++++++++++++++++ profile.go | 8 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 profile.go diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 5661186..5da0571 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -23,23 +23,28 @@ func main() { conf.Parse() - m := messenger.New(messenger.Options{ + client := messenger.New(messenger.Options{ Verify: *verify, VerifyToken: *verifyToken, Token: *pageToken, }) - m.HandleMessage(func(m messenger.Message, r *messenger.Response) { + client.HandleMessage(func(m messenger.Message, r *messenger.Response) { fmt.Printf("%v (Sent, %v)\n", m.Text, m.Time.Format(time.UnixDate)) - fmt.Println(r.Text("Hello, World!")) - fmt.Println(m.Attachments) + + p, err := client.ProfileByID(m.Sender.ID) + if err != nil { + fmt.Println("Something went wrong!", err) + } + + r.Text(fmt.Sprintf("Hello, %v!", p.FirstName)) }) - m.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) { + client.HandleDelivery(func(d messenger.Delivery, r *messenger.Response) { fmt.Println(d.Watermark().Format(time.UnixDate)) }) fmt.Println("Serving messenger bot on localhost:8080") - http.ListenAndServe("localhost:8080", m.Handler()) + http.ListenAndServe("localhost:8080", client.Handler()) } diff --git a/messenger.go b/messenger.go index c783fac..f801396 100644 --- a/messenger.go +++ b/messenger.go @@ -10,6 +10,10 @@ import ( const ( // WebhookURL is where the Messenger client should listen for webhook events. WebhookURL = "/webhook" + + // ProfileURL is the API endpoint used for retrieving profiles. + // Used in the form: https://graph.facebook.com/v2.6/?fields=first_name,last_name,profile_pic&access_token= + ProfileURL = "https://graph.facebook.com/v2.6/" ) // Options are the settings used when creating a Messenger client. @@ -71,6 +75,27 @@ 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) { + p := Profile{} + url := fmt.Sprintf("%v%v", ProfileURL, id) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return p, err + } + + req.URL.RawQuery = "fields=first_name,last_name,profile_pic&access_token=" + m.token + + client := &http.Client{} + resp, err := client.Do(req) + defer resp.Body.Close() + + err = json.NewDecoder(resp.Body).Decode(&p) + + return p, err +} + // handle is the internal HTTP handler for the webhooks. func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) { var rec Receive diff --git a/profile.go b/profile.go new file mode 100644 index 0000000..0cf093c --- /dev/null +++ b/profile.go @@ -0,0 +1,8 @@ +package messenger + +// Profile is the public information of a Facebook user +type Profile struct { + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + ProfilePicURL string `json:"profile_pic"` +}