downloadBot.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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('help', (ctx) => ctx.reply(lang.help))
  24. //bot.hears('hi', (ctx) => ctx.reply('Hey there!'))
  25. //bot.hears(/buy/i, (ctx) => ctx.reply('Buy-buy!'))
  26. //bot.on('sticker', (ctx) => ctx.reply('👍'))
  27. bot.on('audio', (ctx) => {
  28. console.log('message received: ', ctx.from.id)
  29. telegram.getFile( ctx.message.audio.file_id, (ctx) => {
  30. console.log('audio_id sent to TG: ', ctx.message.audio.file_id)
  31. })
  32. const specialChars = "!@#$^&%*()+=[]\/{}|:<>?,."
  33. const audio = ctx.message.audio
  34. var filename = audio.performer + " - " + audio.title
  35. for (var i = 0; i < specialChars.length; i++) {
  36. filename = filename.replace(new RegExp("\\" + specialChars[i], 'gi'), '')
  37. }
  38. filename = filename + ".mp3"
  39. const downloadDir = "/var/music/bot/"
  40. var file = fs.createWriteStream(downloadDir+filename);
  41. const promise = telegram.getFile(audio.file_id)
  42. promise.then(function(result){
  43. const requestFile = https.get("https://api.telegram.org/file/bot"+process.env.BOT_TOKEN+"/"+result.file_path, function(response) {
  44. if(response.statusCode != 200){
  45. return ctx.reply('Error downloading file: '+response.statuscode)
  46. }else{
  47. response.pipe(file);
  48. console.log('Download finished stored at: ' + downloadDir + filename)
  49. return ctx.reply('Downloaded "'+ filename + '"')
  50. }
  51. });
  52. })
  53. })
  54. bot.startPolling()