downloadBot.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const Telegram = require('telegraf/telegram');
  2. const Telegraf = require('telegraf');
  3. const fs = require('fs');
  4. const https = require('https');
  5. const telegram = new Telegram(process.env.BOT_TOKEN);
  6. const bot = new Telegraf(process.env.BOT_TOKEN);
  7. const admin = 170276072;
  8. const langData=fs.readFileSync('./string/lang.json', 'utf8');
  9. const language = JSON.parse(langData);
  10. var lang = language.en;
  11. bot.start( (ctx) => {
  12. console.log('started:', ctx.from.id)
  13. return ctx.reply(lang.welcome)
  14. })
  15. bot.command('spanish', (ctx) => {
  16. lang = language.es;
  17. ctx.reply(lang.langChanged)
  18. })
  19. bot.command('english', (ctx) => {
  20. lang = language.en;
  21. ctx.reply(lang.langChanged)
  22. })
  23. bot.command('donate', (ctx) => ctx.reply(lang.donate))
  24. bot.command('help', (ctx) => ctx.reply(lang.help))
  25. bot.on('audio', (ctx) => {
  26. console.log('message received: ', ctx.from.id)
  27. telegram.getFile( ctx.message.audio.file_id, (ctx) => {
  28. console.log('audio_id sent to TG: ', ctx.message.audio.file_id)
  29. })
  30. const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,."
  31. const audio = ctx.message.audio
  32. var filename = audio.performer + " - " + audio.title
  33. for (var i = 0; i < specialChars.length; i++) {
  34. filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
  35. }
  36. filename = filename + ".mp3"
  37. const downloadDir = "/var/music/bot/" + audio.performer + "/";
  38. if (!fs.existsSync(downloadDir)){
  39. fs.mkdirSync(downloadDir);
  40. }
  41. const file = fs.createWriteStream(downloadDir+filename);
  42. const promise = telegram.getFile(audio.file_id)
  43. promise.then(function(result){
  44. const requestFile = https.get("https://api.telegram.org/file/bot"+process.env.BOT_TOKEN+"/"+result.file_path, function(response) {
  45. if(response.statusCode != 200){
  46. return ctx.reply('Error downloading file: '+response.statuscode)
  47. }else{
  48. response.pipe(file);
  49. console.log('Download finished stored at: ' + downloadDir + filename)
  50. return ctx.reply('Downloaded "'+ filename + '"')
  51. }
  52. });
  53. })
  54. })
  55. bot.startPolling()