-
Notifications
You must be signed in to change notification settings - Fork 56
/
msg-tools.js
96 lines (86 loc) · 2.84 KB
/
msg-tools.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
const dlVars = require('./download_tools/vars.js');
const constants = require('./.constants.js');
const http = require('http');
const ariaTools = require('./download_tools/aria-tools.js');
async function deleteMsg (bot, msg, delay) {
if (delay) await sleep(delay);
bot.deleteMessage(msg.chat.id, msg.message_id)
.catch(ignored => {});
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function isAuthorized (msg) {
for (var i = 0; i < constants.SUDO_USERS.length; i++) {
if (constants.SUDO_USERS[i] === msg.from.id) return 0;
}
if (dlVars.isDownloading && msg.from.id === dlVars.tgFromId) return 1;
if (constants.AUTHORIZED_CHATS.indexOf(msg.chat.id) > -1 &&
msg.chat.all_members_are_administrators) return 2;
if (constants.AUTHORIZED_CHATS.indexOf(msg.chat.id) > -1) return 3;
return -1;
}
function isAdmin (bot, msg, callback) {
bot.getChatAdministrators(msg.chat.id)
.then(members => {
for (var i = 0; i < members.length; i++) {
if (members[i].user.id === msg.from.id) {
callback(null, true);
return;
}
}
callback(null, false);
})
.catch(() => {
callback(null, false);
});
}
/**
* Notifies an external webserver once a download is complete.
* @param {Boolean} successful True is the download completed successfully
* @param {String} gid The GID of the downloaded file
* @param {Number} originGroup The Telegram chat ID of the group where the download started
* @param {String} driveURL The URL of the uploaded file
*/
function notifyExternal (successful, gid, originGroup, driveURL) {
if (!constants.DOWNLOAD_NOTIFY_TARGET || !constants.DOWNLOAD_NOTIFY_TARGET.enabled) return;
ariaTools.getStatus(gid, (err, message, filename, filesize) => {
var name;
var size;
if (!err) {
if (filename !== 'Metadata') name = filename;
if (filesize !== '0B') size = filesize;
}
// TODO: Check which vars are undefined and make those null
const data = JSON.stringify({
successful: successful,
file: {
name: name,
driveURL: driveURL,
size: size
},
originGroup: originGroup
});
const options = {
host: constants.DOWNLOAD_NOTIFY_TARGET.host,
port: constants.DOWNLOAD_NOTIFY_TARGET.port,
path: constants.DOWNLOAD_NOTIFY_TARGET.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
var req = http.request(options);
req.on('error', (e) => {
console.error(`notifyExternal failed: ${e.message}`);
});
req.write(data);
req.end();
});
}
module.exports.deleteMsg = deleteMsg;
module.exports.sleep = sleep;
module.exports.isAuthorized = isAuthorized;
module.exports.isAdmin = isAdmin;
module.exports.notifyExternal = notifyExternal;