-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
51 lines (42 loc) · 1.64 KB
/
app.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
var TelegramBot = require('node-telegram-bot-api');
var fs = require('fs');
var Transmission = require('transmission');
var config = require('config');
// replace the value below with the Telegram token you receive from @BotFather
var token = config.get('telegram.token');
var downloadFolder = config.get('downloadFolder');
var transmission = new Transmission({
'host': config.get('transmission.host'),
'port': config.get('transmission.port'),
'username': config.get('transmission.username'),
'password': config.get('transmission.password'),
});
// Create a bot that uses 'polling' to fetch new updates
var bot = new TelegramBot(token, { polling: true });
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('document', function (msg) {
var chatId = msg.chat.id;
console.log(msg);
bot.downloadFile(msg.document.file_id, downloadFolder).then(function (filePath) {
var absoluteFile = downloadFolder + '/' + msg.document.file_name;
fs.rename(filePath, absoluteFile, function (err, response) {
if (err) {
return console.log(err);
}
console.log('File has been renamed');
if (msg.document.mime_type === 'application/x-bittorrent') {
transmission.addFile(absoluteFile, function (err, arg) {
if (err) {
bot.sendMessage(chatId, "An error occured when trying to add torrent to transmission %j");
console.log(err.message);
} else {
bot.sendMessage(chatId, "File added to transmission");
}
})
} else {
bot.sendMessage(chatId, "Files has been downloaded");
}
});
});
});