-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (62 loc) · 2.28 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
const fs = require('fs');
const https = require('https');
const TelegramBot = require('node-telegram-bot-api');
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
const token = config.token;
const downloadDir = config.download_dir;
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});
let now = function () {
return Math.floor(new Date() / 1000);
};
let downloadFile = function (fileId) {
bot.getFileLink(fileId).then((fileUri) => {
let time = process.hrtime();
let extension = fileUri.split('.').pop();
let newName = `${time[0]}${time[1]}.${extension}`;
let file = fs.createWriteStream(`${downloadDir}/${newName}`);
let request = https.get(fileUri, (response) => {
response.pipe(file);
});
console.log(fileUri);
});
};
let statusUpdater = {
lastTick: Math.floor(new Date() / 1000),
nextNotification: false,
fileCount: 0,
chats: null
};
bot.on('message', (msg) => {
const chatId = msg.chat.id;
let fileId = null;
if (typeof msg.photo !== 'undefined') {
let maxRes = 0, selected = 0;
for (let i in msg.photo) {
let resolution = msg.photo[i].width * msg.photo[i].height;
if (resolution > maxRes) {
selected = i;
maxRes = resolution;
}
}
fileId = msg.photo[selected].file_id;
} else if (typeof msg.document !== 'undefined') {
fileId = msg.document.file_id;
}
if (fileId !== null) {
statusUpdater.nextNotification = now() + 1;
statusUpdater.fileCount += 1;
statusUpdater.chat = chatId;
downloadFile(fileId);
}
});
let clock = setInterval(() => {
if (statusUpdater.nextNotification !== false && statusUpdater.chat !== null
&& statusUpdater.nextNotification < now()) {
let message = statusUpdater.fileCount > 1 ? config.notification_plural : config.notification_single;
bot.sendMessage(statusUpdater.chat, message.replace(/%d/, statusUpdater.fileCount));
statusUpdater.nextNotification = false;
statusUpdater.fileCount = 0;
statusUpdater.chat = null;
}
}, 1000);