Add profile retrieving
This commit is contained in:
parent
9618f8790f
commit
d5a010c736
@ -23,23 +23,28 @@ func main() {
|
|||||||
|
|
||||||
conf.Parse()
|
conf.Parse()
|
||||||
|
|
||||||
m := messenger.New(messenger.Options{
|
client := messenger.New(messenger.Options{
|
||||||
Verify: *verify,
|
Verify: *verify,
|
||||||
VerifyToken: *verifyToken,
|
VerifyToken: *verifyToken,
|
||||||
Token: *pageToken,
|
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.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(d.Watermark().Format(time.UnixDate))
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println("Serving messenger bot on localhost:8080")
|
fmt.Println("Serving messenger bot on localhost:8080")
|
||||||
|
|
||||||
http.ListenAndServe("localhost:8080", m.Handler())
|
http.ListenAndServe("localhost:8080", client.Handler())
|
||||||
}
|
}
|
||||||
|
25
messenger.go
25
messenger.go
@ -10,6 +10,10 @@ import (
|
|||||||
const (
|
const (
|
||||||
// WebhookURL is where the Messenger client should listen for webhook events.
|
// WebhookURL is where the Messenger client should listen for webhook events.
|
||||||
WebhookURL = "/webhook"
|
WebhookURL = "/webhook"
|
||||||
|
|
||||||
|
// ProfileURL is the API endpoint used for retrieving profiles.
|
||||||
|
// Used in the form: https://graph.facebook.com/v2.6/<USER_ID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>
|
||||||
|
ProfileURL = "https://graph.facebook.com/v2.6/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options are the settings used when creating a Messenger client.
|
// Options are the settings used when creating a Messenger client.
|
||||||
@ -71,6 +75,27 @@ func (m *Messenger) Handler() http.Handler {
|
|||||||
return m.mux
|
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.
|
// handle is the internal HTTP handler for the webhooks.
|
||||||
func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
func (m *Messenger) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
var rec Receive
|
var rec Receive
|
||||||
|
8
profile.go
Normal file
8
profile.go
Normal file
@ -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"`
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user