-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (53 loc) · 1.6 KB
/
Copy pathindex.js
File metadata and controls
61 lines (53 loc) · 1.6 KB
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
const request = require('request');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Setup hooks in fttt or <a href="/github">on github</a>');
});
app.get('/github/', (req, res) => {
console.log('github route called: ', req.query);
postToWP(req.query.url, req.query.title, req.query.description)
.then(() => res.status(200).send('OK'))
.catch((err) => res.status(500).send(err));
});
const server = app.listen(8080, () => {
const host = server.address().address;
const port = server.address().port;
console.log(`listening at http://${host}:${port}`);
});
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
const TARGET_GROUP = process.env.TARGET_GROUP;
var graphapi = request.defaults({
baseUrl: 'https://graph.facebook.com',
auth: {
'bearer' : ACCESS_TOKEN
}
});
function postToWP(url, title, description) {
console.log('postToWP', url, title, description);
return new Promise((resolve, reject) => {
if(!url || !title || !description) {
console.error('trying to post', url, description, title);
reject('a param is missing');
}
graphapi({
method: 'POST',
url: '/' + TARGET_GROUP + '/feed',
qs: {
'message': `${title}: ${description}`,
'link': url
}
}, function(error,response,bodyStr) {
const body = typeof(bodyStr) === 'string' ? JSON.parse(bodyStr) : bodyStr;
const err = error || body.error;
if(err) {
console.error(err);
reject(err);
} else {
var post_id = body.id;
console.log('Published "' + title + '": ' + post_id, body);
resolve(post_id);
}
});
});
}