-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
182 lines (167 loc) · 5.68 KB
/
index.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const _ = require('lodash');
const request = require('request');
const ERR_NO_PAGE_TOKEN = "Cannot find page_token, make sure that you've use init(page_id, access_token, options)";
class PsidToFbid {
/**
* Constructor
*
* @param {String} page_id The array to iterate over.
* @param {String} [options.page_token] Page's token (optional). If it is included, you won't have to use fetchPageToken().
* @param {Boolean} [options.cache_enable] Enable cache? Default is true.
*/
constructor(page_id, options) {
this.initState();
this.state.page_id = page_id;
if (options)
_.merge(this.state, options);
}
/**
* Fetch and save page_token
*
* @param {String} access_token Android or iOS access_token.
* @returns {Promise} Returns Promise resolve(page_token) if success.
*/
fetchPageToken(access_token) {
var self = this;
return new Promise((resolve, reject) => {
request({
url: 'https://graph.facebook.com/me/accounts',
qs: {
access_token: access_token
},
method: 'GET'
}, function(error, response, body) {
if (error) {
console.error("Cannot find page. Please check your page_id and access_token");
console.error("Make sure you are using Android or iOS access_token, read guide here: https://github.com/ngxson/psid-to-fbid#getstarted");
console.error(error);
reject();
} else {
var data = JSON.parse(body);
data.data.forEach(page => {
if (page.id == self.state.page_id) {
self.state.page_token = page.access_token;
}
});
if (self.state.page_token) {
resolve(self.state.page_token);
} else {
console.error("Cannot find page. Please check your page_id and access_token");
console.error("Make sure you are using Android or iOS access_token, read guide here: https://github.com/ngxson/psid-to-fbid#getstarted");
reject();
}
}
})
})
}
initState() {
this.state = {
page_id: null,
page_token: null,
cache_enable: true,
cache: {}
}
}
/**
* Get fbid from messaging_event of webhook
* This does NOT work with postback from buttons / Get Started button
*
* @param {Object} messaging_event messaging_event from webhook.
* @returns {Promise} Returns Promise resolve(fbid), with fbid = null if there's an error.
*/
getFromWebhookEvent(messaging_event) {
var self = this;
return new Promise((resolve, reject) => {
if (!messaging_event.sender) {
console.error("Invalid messaging_event object");
return reject();
} else if (!self.state.page_token) {
console.error(ERR_NO_PAGE_TOKEN);
return resolve(null);
} else if (!messaging_event.message || !messaging_event.message.mid) {
return resolve(null);
}
var mid = messaging_event.message.mid,
psid = messaging_event.sender.id;
if (self.state.cache_enable && self.state.cache[psid]) {
return resolve(self.state.cache[psid]);
}
self.getFromMid(mid, psid).then((fbid) => {
resolve(fbid);
}).catch(() => {
reject();
})
})
}
/**
* Get fbid from mid (message_id)
*
* @param {String} mid message_id.
* @param {String} psid Optional, user's psid.
* @returns {Promise} Returns Promise resolve(fbid), with fbid = null if there's an error.
*/
getFromMid(mid, psid) {
var self = this;
return new Promise((resolve, reject) => {
if (!self.state.page_token) {
console.error(ERR_NO_PAGE_TOKEN);
return resolve(null);
} else if (!mid) {
return resolve(null);
}
if (psid && self.state.cache_enable && self.state.cache[psid]) {
return resolve(self.state.cache[psid]);
}
if (!mid.startsWith('m_')) {
mid = 'm_' + mid;
}
request({
url: 'https://graph.facebook.com/' + mid,
qs: {
access_token: self.state.page_token,
fields: "from,to"
},
method: 'GET'
}, function(error, response, body) {
if (error) {
console.error(error);
resolve(null);
} else {
var res = JSON.parse(body);
if (res.error) {
console.error("getFromMid " + mid + " ERR: " + res);
resolve(null);
} else if (res["from"].id != self.state.page_id) {
if (self.state.cache_enable) {
self.state.cache[psid] = res["from"].id;
}
resolve(res["from"].id);
} else if (res.to.data[0].id != self.state.page_id) {
if (self.state.cache_enable) {
self.state.cache[psid] = res.to.data[0].id;
}
resolve(res.to.data[0].id);
} else {
console.error("getFromMid " + mid + " ERR: " + res);
resolve(null);
}
}
})
})
}
/**
* Generate Login URL to obtain the iOS's access_token
* WARNING: This should be done only once, on client side to prevent checkpoint from facebook
*
* @param {String} email Your email / phone number / username on facebook.
* @param {String} psid Your facebook's password.
* @returns {String} An URL to be accessed on a browser.
*/
static genLoginURLiOS(email, pass) {
var crypto = require('crypto');
var sig_content = "api_key=3e7c78e35a76a9299309885393b02d97email="+email+"format=JSONgenerate_session_cookies=1locale=en_usmethod=auth.loginpassword="+pass+"return_ssl_resources=0v=1.0c1e620fa708a1d5696fb991c1bde5662";
var sig = crypto.createHash('md5').update(sig_content).digest('hex');
return "https://api.facebook.com/restserver.php?api_key=3e7c78e35a76a9299309885393b02d97&email="+encodeURIComponent(email)+"&format=JSON&generate_session_cookies=1&locale=en_us&method=auth.login&password="+encodeURIComponent(pass)+"&return_ssl_resources=0&v=1.0&sig=" + sig;
}
}
module.exports = PsidToFbid