Просмотр исходного кода

Added document download and improved audio download

¨José 5 лет назад
Родитель
Сommit
f7c6ca4523
1 измененных файлов с 57 добавлено и 12 удалено
  1. 57 12
      downloadBot.js

+ 57 - 12
downloadBot.js

@@ -7,6 +7,7 @@ const bot = new Telegraf(process.env.BOT_TOKEN);
 const downloadPrefix = process.env.DOWNLOAD_PREFIX;
 const musicDir = 'music'
 const videoDir = 'video'
+const documentDir = 'downloads'
 const admin = 170276072;
 const langData=fs.readFileSync('./string/lang.json', 'utf8');
 const language = JSON.parse(langData);
@@ -18,46 +19,49 @@ bot.start( (ctx) => {
   return ctx.reply(lang.welcome)
 })
 
+// ----------------------------------------------------------------------------
 
 bot.command('spanish', (ctx) => {
   lang = language.es;
   ctx.reply(lang.langChanged)
 })
 
+// ----------------------------------------------------------------------------
 
 bot.command('english', (ctx) => {
   lang = language.en;
   ctx.reply(lang.langChanged)
 })
 
+// ----------------------------------------------------------------------------
 
 bot.command('donate', (ctx) => ctx.reply(lang.donate))
 
+// ----------------------------------------------------------------------------
 
 bot.command('help', (ctx) => ctx.reply(lang.help))
 
+// ----------------------------------------------------------------------------
 
 bot.on('audio', (ctx) => {
-	console.log('Audio message received: ', ctx.from.id)
+	console.log('Audio message received from: ', ctx.from.id)
 	telegram.getFile( ctx.message.audio.file_id, (ctx) => { 
 		console.log('audio_id sent to TG: ', ctx.message.audio.file_id)
 	})
 
-	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
+	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,.' "
 	const audio = ctx.message.audio
-	var filename = audio.performer + " - " + audio.title
-	var performer = audio.performer
+	var filename = audio.performer + "-" + audio.title
 	for (var i = 0; i < specialChars.length; i++) {
-		filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
-		performer = performer.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
+		filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '_')
 	}
 	filename = filename + ".mp3"
-	const downloadDir = downloadPrefix + musicDir + '/' + performer + '/';
+	const downloadDir = downloadPrefix + musicDir + '/';
 
 	if (!fs.existsSync(downloadDir)){
 		 fs.mkdirSync(downloadDir, true);
 	}
-	const file = fs.createWriteStream(downloadDir+filename);
+	const file = fs.createWriteStream(downloadDir + filename);
         
 	const promise = telegram.getFile(audio.file_id)	
 	promise.then(function(result){
@@ -68,19 +72,20 @@ bot.on('audio', (ctx) => {
 			}else{
 				response.pipe(file);
 				console.log('Download finished stored at: ' + downloadDir + filename)
-        			return ctx.reply('Downloaded "'+ filename + '"')
+        			return ctx.reply('Downloaded "' + filename + '"')
 			}
         	});
 	})
 })
 
+// ----------------------------------------------------------------------------
+
 bot.on('video', (ctx) => {
-	console.log('Video message received: ', ctx.from.id)
+	console.log('Video message received from: ', ctx.from.id)
 	telegram.getFile( ctx.message.video.file_id, (ctx) => { 
 		console.log('file_id sent to TG: ', ctx.message.video.file_id)
 	})
 
-	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
 	const video = ctx.message.video
 	var filename = video.file_id
 	var extension = '.mp4'
@@ -100,7 +105,45 @@ bot.on('video', (ctx) => {
 		const requestFile = https.get("https://api.telegram.org/file/bot" +
 		process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
            	if(response.statusCode != 200){
-				return ctx.reply('Error downloading file: '+response.statuscode)
+				return ctx.reply('Error downloading file: ' + response.statuscode)
+			}else{
+				response.pipe(file);
+				console.log('Download finished stored at: ' + downloadDir + filename)
+        			return ctx.reply('Downloaded "'+ filename + '"')
+			}
+        	});
+	})
+})
+
+// ----------------------------------------------------------------------------
+
+bot.on('document', (ctx) => {
+	console.log('Document message received from: ', ctx.from.id)
+	telegram.getFile( ctx.message.document.file_id, (ctx) => { 
+		console.log('file_id sent to TG: ', ctx.message.document.file_id)
+	})
+
+	const specialChars = "!@#$^&%*()+=[]\/{}|:<>?, "
+	const document = ctx.message.document
+	var filename = document.file_name
+
+	for (var i = 0; i < specialChars.length; i++) {
+		filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
+	}
+
+	const downloadDir = downloadPrefix + documentDir + '/';
+
+	if (!fs.existsSync(downloadDir)){
+		 fs.mkdirSync(downloadDir, true);
+	}
+	const file = fs.createWriteStream(downloadDir + filename);
+        
+	const promise = telegram.getFile(document.file_id)	
+	promise.then(function(result){
+		const requestFile = https.get("https://api.telegram.org/file/bot" +
+		process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
+           	if(response.statusCode != 200){
+				return ctx.reply('Error downloading file: ' + response.statuscode)
 			}else{
 				response.pipe(file);
 				console.log('Download finished stored at: ' + downloadDir + filename)
@@ -110,4 +153,6 @@ bot.on('video', (ctx) => {
 	})
 })
 
+// ----------------------------------------------------------------------------
+
 bot.startPolling()