downloadBot.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 langData = fs.readFileSync('./string/lang.json', 'utf8')
  10. const language = JSON.parse(langData)
  11. const API_URL = "https://api.telegram.org/file/bot" + process.env.BOT_TOKEN
  12. const debug = require('debug')('app')
  13. const Queue = require('bull')
  14. var lang = language.en
  15. const queue = new Queue('rateLimited', {
  16. limiter: {
  17. max: 2,
  18. duration: 60000,
  19. bounceBack: false
  20. }
  21. })
  22. const options = {
  23. delay: 60000, // 1 min in ms
  24. attempts: 2
  25. }
  26. // ----------------------------------------------------------------------------
  27. queue.process(async (job) => {
  28. return await saveFile(job.data.file_id, job.data.destination,
  29. job.data.filename).then(() => {
  30. const text = 'File ' + job.data.filename + ' downloaded.'
  31. telegram.sendMessage(job.data.chat_id, text)
  32. }).catch(error => {
  33. const text = 'Error downloading file: ' + error
  34. telegram.sendMessage(job.data.chat_id, text)
  35. })
  36. })
  37. // ----------------------------------------------------------------------------
  38. function cleanString(string, additionalChars) {
  39. const chars = "!@#$^&%*()+=[]\/{}|:<>?,'–" + additionalChars
  40. var result = string.normalize('NFD').trim()
  41. .replace(/[\u0300-\u036f]/g, '')
  42. for (var i=0; i < chars.length; i++) {
  43. result = result.replace(new RegExp("\\" + chars[i], 'gi'), '')
  44. }
  45. return result
  46. }
  47. // ----------------------------------------------------------------------------
  48. function saveFile(file_id, dest, filename) {
  49. return new Promise(function(resolve, reject){
  50. if (!fs.existsSync(dest)){ fs.mkdirSync(dest, true) }
  51. const path = dest + filename
  52. const promise = telegram.getFile(file_id)
  53. promise.then(res => {
  54. const url = API_URL + '/' + res.file_path
  55. https.get(url, response => {
  56. if(response.statusCode != 200){
  57. debug('Error downloading file: %s', response.statuscode)
  58. reject()
  59. }else{
  60. const file = fs.createWriteStream(path)
  61. response.pipe(file)
  62. file.on('finish', function(){
  63. file.close()
  64. debug('File saved in: "%s"', path)
  65. resolve()
  66. }).on('error', err =>{
  67. fs.unlink(path)
  68. debug(err)
  69. reject()
  70. })
  71. }
  72. })
  73. }).catch(function(error){
  74. debug('[SaveFile,Promise][Error]: %s', error)
  75. reject()
  76. })
  77. })
  78. }
  79. // ----------------------------------------------------------------------------
  80. bot.start( (ctx) => {
  81. debug('Started: %s', ctx.from.id)
  82. return ctx.reply(lang.welcome)
  83. })
  84. // ----------------------------------------------------------------------------
  85. bot.command('spanish', (ctx) => {
  86. lang = language.es
  87. ctx.reply(lang.langChanged)
  88. })
  89. // ----------------------------------------------------------------------------
  90. bot.command('english', (ctx) => {
  91. lang = language.en
  92. ctx.reply(lang.langChanged)
  93. })
  94. // ----------------------------------------------------------------------------
  95. bot.command('donate', (ctx) => ctx.reply(lang.donate))
  96. // ----------------------------------------------------------------------------
  97. bot.command('help', (ctx) => ctx.reply(lang.help))
  98. // ----------------------------------------------------------------------------
  99. bot.on('audio', (ctx) => {
  100. const audio = ctx.message.audio
  101. const extension = audio.file_name.split('.').pop()
  102. const performer = cleanString(audio.performer, '.')
  103. const title = cleanString(audio.title, '.')
  104. var filename = performer + ' - ' + title + '.' + extension
  105. const dest = downloadPrefix + musicDir + '/' + audio.performer + '/'
  106. var data = {
  107. file_id: audio.file_id,
  108. destination: dest,
  109. filename: filename,
  110. chat_id: ctx.chat.id
  111. }
  112. queue.add(data, options)
  113. debug('------------------------------------------------------------')
  114. debug('Job added to queue')
  115. debug('file_id: %s', data.file_id)
  116. debug('destination: %s', data.destination)
  117. debug('filename: %s', data.filename)
  118. debug('chat_id: %s', data.chat_id)
  119. debug('------------------------------------------------------------')
  120. })
  121. // ----------------------------------------------------------------------------
  122. bot.startPolling()