/
messenger-client.js
48 lines (42 loc) · 1.03 KB
/
messenger-client.js
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
import request from 'request';
import Promise from 'bluebird';
Promise.promisifyAll(request);
class MessengerClient {
constructor(context) {
this.context = context;
}
getUserData(entry) {
return request.getAsync({
uri: `https://graph.facebook.com/v2.6/${entry.sender.id}`,
qs: {
fields: 'first_name,last_name,profile_pic',
access_token: this.context.pageAccessToken
},
json: true
}).then((response) => {
return response.body;
});
}
sendData(payload, entry) {
return request.postAsync({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: this.context.pageAccessToken
},
json: {
recipient: { id: entry.sender.id },
message: payload
}
});
}
subscribeAppToPage() {
return request.postAsync({
uri: `https://graph.facebook.com/v2.6/me/subscribed_apps`,
qs: {
access_token: this.context.pageAccessToken
},
json: true
});
}
}
export default MessengerClient;