|
| 1 | +require('dotenv').config() |
| 2 | +const fs = require('fs'); |
| 3 | +const tough = require('tough-cookie'); |
| 4 | +const request = require('request-promise'); |
| 5 | +const Telegraf = require('telegraf'); |
| 6 | +const bot = new Telegraf(process.env.BOT_API); |
| 7 | +const uuidv4 = require('uuid/v4'); |
| 8 | + |
| 9 | + |
| 10 | +bot.on('document', ctx => { |
| 11 | + if ( |
| 12 | + ctx.message.document && |
| 13 | + ctx.message.document.mime_type === 'application/pdf' |
| 14 | + ) { |
| 15 | + console.log(ctx.message.document); |
| 16 | + let document = ctx.message.document; |
| 17 | + bot.telegram.getFileLink(document.file_id).then(data => { |
| 18 | + console.log('file', data); |
| 19 | + var options = { |
| 20 | + method: 'GET', |
| 21 | + uri: data, |
| 22 | + resolveWithFullResponse: true |
| 23 | + }; |
| 24 | + let uuid = uuidv4(); |
| 25 | + if (!fs.existsSync('./temp/' + uuid)) { |
| 26 | + fs.mkdirSync('./temp/' + uuid); |
| 27 | + } |
| 28 | + request(options) |
| 29 | + .pipe(fs.createWriteStream('./temp/' + uuid + '/' + document.file_name)) |
| 30 | + .on('finish', () => { |
| 31 | + console.log('finish downloading pdf..'); |
| 32 | + download('./temp/' + uuid, document.file_name); |
| 33 | + }) |
| 34 | + .on('error', error => { |
| 35 | + console.log('Error in creating map', error); |
| 36 | + //remove folder and tell user error. |
| 37 | + }); |
| 38 | + }); |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +const download = (path, fileName) => { |
| 43 | + request({ |
| 44 | + method: 'POST', |
| 45 | + uri: 'https://simplypdf.com/api/convert', |
| 46 | + headers: { |
| 47 | + Accept: 'application/json', |
| 48 | + 'Content-Type': 'multipart/form-data' |
| 49 | + }, |
| 50 | + formData: { |
| 51 | + DynamicFile: fs.createReadStream(path + '/' + fileName), |
| 52 | + OutputFormat: 'PowerPoint' |
| 53 | + }, |
| 54 | + resolveWithFullResponse: true |
| 55 | + }) |
| 56 | + .then(r => { |
| 57 | + console.log('response', r.body); |
| 58 | + let id = JSON.parse(r.body).id; |
| 59 | + let cookieValue = r.headers['set-cookie'][0] |
| 60 | + .split('=', 2)[1] |
| 61 | + .slice(0, 24); |
| 62 | + let cookie = new tough.Cookie({ |
| 63 | + key: 'ASP.NET_SessionId', |
| 64 | + value: cookieValue, |
| 65 | + domain: 'simplypdf.com', |
| 66 | + httpOnly: true |
| 67 | + }); |
| 68 | + let cookiejar = request.jar(); |
| 69 | + cookiejar.setCookie(cookie, 'https://simplypdf.com'); |
| 70 | + let options = { |
| 71 | + method: 'GET', |
| 72 | + uri: 'https://simplypdf.com/api/status/' + id, |
| 73 | + headers: { |
| 74 | + Accept: 'application/json' |
| 75 | + }, |
| 76 | + jar: cookiejar, |
| 77 | + resolveWithFullResponse: true |
| 78 | + }; |
| 79 | + |
| 80 | + request(options) |
| 81 | + .then(res => { |
| 82 | + console.log('status', res.body); |
| 83 | + let status = JSON.parse(res.body).status; |
| 84 | + setTimeout(() => { |
| 85 | + if (status === 'ready') { |
| 86 | + console.log('ready for download...'); |
| 87 | + let id = JSON.parse(res.body).id; |
| 88 | + let options = { |
| 89 | + method: 'GET', |
| 90 | + uri: 'https://simplypdf.com/api/download/' + id, |
| 91 | + jar: cookiejar, |
| 92 | + resolveWithFullResponse: true |
| 93 | + }; |
| 94 | + |
| 95 | + let file_name = fileName.split('.')[0]; |
| 96 | + request(options) |
| 97 | + .pipe(fs.createWriteStream(path + '/' + file_name + '.pptx')) |
| 98 | + .on('finish', function() { |
| 99 | + console.log('finish downloading pptx...'); |
| 100 | + }) |
| 101 | + .on('error', function(error) { |
| 102 | + console.log('Error in creating map', error); |
| 103 | + }); |
| 104 | + } |
| 105 | + }, 10000); |
| 106 | + }); |
| 107 | + }) |
| 108 | + |
| 109 | + .catch(err => { |
| 110 | + console.log('err', err); |
| 111 | + }); |
| 112 | +}; |
| 113 | + |
| 114 | +bot.startPolling(); |
0 commit comments