forked from tejado/telegram-nearby-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (52 loc) · 1.61 KB
/
server.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
const express = require('express');
const addRequestId = require('express-request-id')();
const log = require('./lib/logger.js');
const TelegramNearby = require('./lib/telegram-nearby.js');
const config = require('./config.js')
if (
config.telegramApiId === undefined ||
config.telegramApiHash === undefined ||
config.hostname === undefined ||
config.port === undefined
) {
throw 'Missing configuration...';
}
const tg = new TelegramNearby(config.telegramApiId, config.telegramApiHash);
const app = express();
app.use(addRequestId);
app.use(express.static('./web-build/'));
app.use('/photos/', express.static('./_td_database/profile_photos/'));
app.use(function (error, req, res, next) {
if (error instanceof SyntaxError) {
log.warn(`${req.id} - Body Parsing: ${error} (${req.rawBody})`);
res.status(400).send({ request: req.id });
} else {
next();
}
});
app.use(express.urlencoded({
verify: (req, res, buf) => {
req.rawBody = buf
},
extended: false
}));
app.use(express.json({
verify: (req, res, buf) => {
req.rawBody = buf
}
}));
app.post('/getNearby', (req, res) => {
log.info(`${req.id} - POST /getNearby`);
log.info(JSON.stringify(req.body));
tg.getNearby(req.body).then((chat) => {
log.info(`${req.id} - sending 200`);
res.send(chat);
}).catch((error) => {
console.log(error.stack)
log.crit(`${req.id} - ${error}`);
res.status(500).send(error);
});;
});
app.listen(config.port, config.hostname, () => {
console.log(`Listening at https://${config.hostname}:${config.port}`);
});