downloadBot.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 downloadPrefix = process.env.DOWNLOAD_PREFIX;
  8. const musicDir = 'music'
  9. const videoDir = 'video'
  10. const admin = 170276072;
  11. const langData=fs.readFileSync('./string/lang.json', 'utf8');
  12. const language = JSON.parse(langData);
  13. var lang = language.en;
  14. bot.start( (ctx) => {
  15. console.log('started:', ctx.from.id)
  16. return ctx.reply(lang.welcome)
  17. })
  18. bot.command('spanish', (ctx) => {
  19. lang = language.es;
  20. ctx.reply(lang.langChanged)
  21. })
  22. bot.command('english', (ctx) => {
  23. lang = language.en;
  24. ctx.reply(lang.langChanged)
  25. })
  26. bot.command('donate', (ctx) => ctx.reply(lang.donate))
  27. bot.command('help', (ctx) => ctx.reply(lang.help))
  28. bot.on('audio', (ctx) => {
  29. console.log('Audio message received: ', ctx.from.id)
  30. telegram.getFile( ctx.message.audio.file_id, (ctx) => {
  31. console.log('audio_id sent to TG: ', ctx.message.audio.file_id)
  32. })
  33. const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
  34. const audio = ctx.message.audio
  35. var filename = audio.performer + " - " + audio.title
  36. var performer = audio.performer
  37. for (var i = 0; i < specialChars.length; i++) {
  38. filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
  39. performer = performer.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
  40. }
  41. filename = filename + ".mp3"
  42. const downloadDir = downloadPrefix + musicDir + '/' + performer + '/';
  43. if (!fs.existsSync(downloadDir)){
  44. fs.mkdirSync(downloadDir, true);
  45. }
  46. const file = fs.createWriteStream(downloadDir+filename);
  47. const promise = telegram.getFile(audio.file_id)
  48. promise.then(function(result){
  49. const requestFile = https.get("https://api.telegram.org/file/bot" +
  50. process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
  51. if(response.statusCode != 200){
  52. return ctx.reply('Error downloading file: ' + response.statuscode)
  53. }else{
  54. response.pipe(file);
  55. console.log('Download finished stored at: ' + downloadDir + filename)
  56. return ctx.reply('Downloaded "'+ filename + '"')
  57. }
  58. });
  59. })
  60. })
  61. bot.on('video', (ctx) => {
  62. console.log('Video message received: ', ctx.from.id)
  63. telegram.getFile( ctx.message.video.file_id, (ctx) => {
  64. console.log('file_id sent to TG: ', ctx.message.video.file_id)
  65. })
  66. const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,. "
  67. const video = ctx.message.video
  68. var filename = video.file_id
  69. var extension = '.mp4'
  70. if (video.mime_type != undefined){
  71. extension = '.' + video.mime_type.split('/')[1]
  72. }
  73. filename = filename + extension
  74. const downloadDir = downloadPrefix + videoDir + '/';
  75. if (!fs.existsSync(downloadDir)){
  76. fs.mkdirSync(downloadDir, true);
  77. }
  78. const file = fs.createWriteStream(downloadDir + filename);
  79. const promise = telegram.getFile(video.file_id)
  80. promise.then(function(result){
  81. const requestFile = https.get("https://api.telegram.org/file/bot" +
  82. process.env.BOT_TOKEN + "/" + result.file_path, function(response) {
  83. if(response.statusCode != 200){
  84. return ctx.reply('Error downloading file: '+response.statuscode)
  85. }else{
  86. response.pipe(file);
  87. console.log('Download finished stored at: ' + downloadDir + filename)
  88. return ctx.reply('Downloaded "'+ filename + '"')
  89. }
  90. });
  91. })
  92. })
  93. bot.startPolling()