-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedin-function.js
123 lines (117 loc) · 3.52 KB
/
linkedin-function.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
const fli = require('fli-webtask');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const express = fli.npm.express;
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
const recordAlarm = (req) => (name) => {
let context = req.webtaskContext;
// Record alarm
return loader({
method: 'post',
url: `${context.secrets.alarmFunction}/${req.db}/record`,
qs: {
token: context.secrets.token,
name: name
},
}, () => {});
};
const validateMiddleware = (req, res, next) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
const errMsgToken = 'No token.';
responseHandler(errMsgToken, res);
return next(errMsgToken);
}
if(!_.get(req, 'body._id')) {
const errMsgId = 'No _id provided.';
responseHandler(errMsgId, res);
return next(errMsgId);
}
var lUrl = _.get(req, 'body.payload.shortUrl') || _.get(req, 'body.payload.url');
if(!lUrl) {
const errMsgUrl = 'No url provided.';
responseHandler(errMsgUrl, res);
return next(errMsgUrl);
}
req.lUrl = lUrl;
var db = _.get(req, 'body.db');
var linkedinPublisherUrl = req.webtaskContext.secrets[`${db}-ln-url`];
var linkedinPublisherId = req.webtaskContext.secrets[`${db}-ln-id`];
var linkedinPublisherToken = req.webtaskContext.secrets[`${db}-ln-token`];
if(!linkedinPublisherUrl || !linkedinPublisherId || !linkedinPublisherToken) {
const errMsgFb = 'No LinkedIn publisher.';
responseHandler(errMsgFb, res);
return next(errMsgFb);
}
req.linkedinPublisherUrl = linkedinPublisherUrl;
req.linkedinPublisherId = linkedinPublisherId;
req.linkedinPublisherToken = linkedinPublisherToken;
req.db = db;
return next();
};
router
.all('/publish', function (req, res) {
const url = req.lUrl;
const promoText = _.get(req, 'body.payload.promoText') || _.get(req, 'body.payload.info.title');
const imgUrl = _.get(req, 'body.payload.promoImg') || _.get(req, 'body.payload.info.image');
const hashTags = [''].concat(
_.get(req, 'body.payload.info.labels', [])
.slice(2)
.map(h => h.replace(/[^\w\d]/mig, ''))
.filter(h => h && h.length < 33)
).concat('bestdeals').join(' #').trim();
const body = {
owner: 'urn:li:organization:' + req.linkedinPublisherId,
subject: promoText,
text: {
text: `
${promoText}
${hashTags}
`
},
content: {
contentEntities: [{
entityLocation: url,
thumbnails: [{
resolvedUrl: imgUrl
}]
}],
title: promoText
},
distribution: {
linkedInDistributionTarget: {}
}
};
const headers = {
'Authorization': 'Bearer ' + req.linkedinPublisherToken,
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0',
'x-li-format': 'json'
};
console.log(`-- linkedin published: ${promoText} ${url}`);
as.waterfall([
(next) => {
request.post({
url: req.linkedinPublisherUrl,
json: body,
headers: headers
}, (err, response, body) => next(err, body));
}
],
(err, info) => {
if(!!err) {
// recordAlarm(req)('linkedin_publish_error');
console.log(err);
}
responseHandler(null, res, info);
});
});
app
.use(bodyParser.json())
.use('/', validateMiddleware, router);
module.exports = wt.fromExpress(app);