-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-function.js
100 lines (93 loc) · 2.81 KB
/
telegram-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
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 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 dealUrl = _.get(req, 'body.payload.shortUrl') || _.get(req, 'body.payload.url');
if(!dealUrl) {
const errMsgUrl = 'No url provided.';
responseHandler(errMsgUrl, res);
return next(errMsgUrl);
}
req.dealUrl = dealUrl;
var db = _.get(req, 'body.db');
var telegramPublisherUrl = req.webtaskContext.secrets[`${db}-t`];
if(!telegramPublisherUrl) {
const errMsgT = 'No Telegram publisher.';
responseHandler(errMsgT, res);
return next(errMsgT);
}
req.dealUrl = dealUrl;
req.db = db;
req.telegramPublisherUrl = telegramPublisherUrl;
return next();
};
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
},
}, () => {});
};
router
.all('/publish', function (req, res) {
const telegramPublisherUrl = (req.telegramPublisherUrl || "").replace("${method}", "sendMessage");
const url = req.dealUrl;
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', [])
.map(h => h.replace(/[^\w\dА-ЯҐЄІЇ]/mig, ''))
.filter(h => h && h.length < 33)
).join(' #').trim();
console.log(`-- telegram published: ${promoText} ${url}`);
as.waterfall([
(next) => loader({
method: 'post',
url: telegramPublisherUrl,
json: _.get(req, 'body.payload.as') === 'link' ? {
text: `${promoText}
${url}
${hashTags}`
} : {
disable_web_page_preview: false,
parse_mode: 'Markdown',
text: `[ ](${imgUrl}) ${promoText}
${url.replace(/_/mig, '\\_')}
${hashTags}`
}
}, next)
],
(err, info) => {
if(!!err) {
recordAlarm(req)('telegram_publish_error');
}
responseHandler(null, res, info);
});
});
app
.use(bodyParser.json())
.use('/', validateMiddleware, router);
module.exports = wt.fromExpress(app);