downloadBot.js 5.1 KB

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