소스 검색

Working initial version 1.0

Okynos 8 년 전
부모
커밋
9d4a2bfc52
1개의 변경된 파일47개의 추가작업 그리고 0개의 파일을 삭제
  1. 47 0
      downloadBot.js

+ 47 - 0
downloadBot.js

@@ -0,0 +1,47 @@
+const Telegram = require('telegraf/telegram')
+const Telegraf = require('telegraf')
+const fs = require('fs')
+const https = require('https');
+const telegram = new Telegram(process.env.BOT_TOKEN)
+const bot = new Telegraf(process.env.BOT_TOKEN)
+
+
+bot.start( (ctx) => {
+  console.log('started:', ctx.from.id)
+  return ctx.reply('Welcome!')
+})
+bot.command('help', (ctx) => ctx.reply('Try send a sticker!'))
+bot.hears('hi', (ctx) => ctx.reply('Hey there!'))
+bot.hears(/buy/i, (ctx) => ctx.reply('Buy-buy!'))
+bot.on('sticker', (ctx) => ctx.reply('👍'))
+bot.on('audio', (ctx) => {
+	console.log('message received: ', 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 audio = ctx.message.audio
+	var filename = audio.performer + " - " + audio.title
+	for (var i = 0; i < specialChars.length; i++) {
+		filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
+	}
+	filename = filename + ".mp3"
+	const downloadDir = "/var/music/bot/"
+	var file = fs.createWriteStream(downloadDir+filename);
+        
+	const promise = telegram.getFile(audio.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)
+        			return ctx.reply('Downloaded "'+ filename + '"')
+			}
+        	});
+	})
+})
+
+bot.startPolling()