-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_api.go
88 lines (75 loc) · 2.51 KB
/
graph_api.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package fbmbotapi
import (
"bytes"
"context"
"fmt"
"github.com/pkg/errors"
"github.com/pquerna/ffjson/ffjson"
"github.com/strongo/log"
"io/ioutil"
"net/http"
)
const contentTypeApplicationJSON = "application/json"
const (
endpointMeMessage = "me/messages"
endpointMeMessengerProfile = "me/messenger_profile"
)
// GraphAPI is Facebook API client
type GraphAPI struct {
httpClient *http.Client
AccessToken string
}
// NewGraphAPI creates new API client
func NewGraphAPI(httpClient *http.Client, accessToken string) GraphAPI {
return GraphAPI{
httpClient: httpClient,
AccessToken: accessToken,
}
}
func (graphAPI GraphAPI) apiURL(endpoint string) string {
return fmt.Sprintf("https://graph.facebook.com/v2.6/%v/?access_token=%v", endpoint, graphAPI.AccessToken)
}
// SetGetStarted sets get started message
func (graphAPI GraphAPI) SetGetStarted(c context.Context, message GetStartedMessage) error {
return graphAPI.postMessage(c, endpointMeMessengerProfile, &message)
}
// SetPersistentMenu sets persistent menu
func (graphAPI GraphAPI) SetPersistentMenu(c context.Context, message PersistentMenuMessage) error {
return graphAPI.postMessage(c, endpointMeMessengerProfile, &message)
}
// SetWhitelistedDomains sets whitelisted domains
func (graphAPI GraphAPI) SetWhitelistedDomains(c context.Context, message WhitelistedDomainsMessage) error {
return graphAPI.postMessage(c, endpointMeMessengerProfile, &message)
}
// SendMessage sends message
func (graphAPI GraphAPI) SendMessage(c context.Context, request Request) error {
return graphAPI.postMessage(c, endpointMeMessage, &request)
}
func (graphAPI GraphAPI) postMessage(c context.Context, endpoint string, message interface{}) error {
content, err := ffjson.MarshalFast(message)
if err != nil {
ffjson.Pool(content)
return err
}
apiURL := graphAPI.apiURL(endpoint)
log.Debugf(c, "Posting to FB API: %v\n%v", apiURL, string(content))
resp, err := graphAPI.httpClient.Post(apiURL, contentTypeApplicationJSON, bytes.NewReader(content))
ffjson.Pool(content)
if err != nil {
return err
}
var respData []byte
if resp.Body != nil {
defer resp.Body.Close()
if respData, err = ioutil.ReadAll(resp.Body); err != nil {
err = errors.WithMessage(err, "Failed to read response body")
return err
}
}
log.Debugf(c, "Response from FB API (status=%v): %v", resp.Status, string(respData))
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = fmt.Errorf("FB API returned unexpected HTTP status code: %d", resp.StatusCode)
return err
}
return nil
}