-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
166 lines (150 loc) · 4.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require('dotenv').config();
const fs = require('fs');
const tough = require('tough-cookie');
const request = require('request-promise');
const Telegraf = require('telegraf');
const bot = new Telegraf(process.env.BOT_API);
const uuidv4 = require('uuid/v4');
const { Signale } = require('signale');
const del = require('del');
const logOptions = {
types: {
debug: {
color: 'blue',
label: 'debug'
}
}
};
const log = new Signale(logOptions);
bot.on('document', ctx => {
if (
ctx.message.document &&
ctx.message.document.mime_type === 'application/pdf'
) {
console.log(ctx.message.document);
let document = ctx.message.document;
bot.telegram.getFileLink(document.file_id).then(data => {
ctx.reply('Received your file.');
log.debug('File URL', data);
let options = {
method: 'GET',
uri: data,
resolveWithFullResponse: true
};
let uuid = uuidv4();
//create temp folder for pdf and pptx
if (!fs.existsSync('./temp/' + uuid)) {
fs.mkdirSync('./temp/' + uuid);
}
let fileName = document.file_name.slice(0,document.file_name.length-4);
//make request to download pdf file
request(options)
.pipe(fs.createWriteStream('./temp/' + uuid + '/' + fileName + '.pdf'))
.on('finish', () => {
//console.log('finish downloading pdf..');
log.success('finish downloading pdf...');
convert('./temp/' + uuid, fileName, ctx);
})
.on('error', error => {
console.log('Error in creating map', error);
//remove folder and tell user error.
});
});
}
});
const convert = (path, fileName, ctx) => {
request({
method: 'POST',
uri: 'https://simplypdf.com/api/convert',
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data'
},
formData: {
DynamicFile: fs.createReadStream(path + '/' + fileName + '.pdf'),
OutputFormat: 'PowerPoint'
},
resolveWithFullResponse: true
})
.then(r => {
console.log('response', r.body);
ctx.reply('Converting your pdf.');
let id = JSON.parse(r.body).id;
let cookieValue = r.headers['set-cookie'][0]
.split('=', 2)[1]
.slice(0, 24);
let cookie = new tough.Cookie({
key: 'ASP.NET_SessionId',
value: cookieValue,
domain: 'simplypdf.com',
httpOnly: true
});
let cookiejar = request.jar();
cookiejar.setCookie(cookie, 'https://simplypdf.com');
let options = {
method: 'GET',
uri: 'https://simplypdf.com/api/status/' + id,
headers: {
Accept: 'application/json'
},
jar: cookiejar,
resolveWithFullResponse: true
};
checkStatusAndDownload(ctx, options, cookiejar, path, fileName);
})
.catch(err => {
log.err('error @ converting', err);
ctx.reply('There was an error uploading your file. Please try again.');
});
};
const checkStatusAndDownload = (ctx, options, cookiejar, path, fileName) => {
request(options).then(res => {
console.log('status', res.body);
let status = JSON.parse(res.body).status;
setTimeout(() => {
if (status === 'ready') {
log.success('ppt is ready for download...');
// console.log('ready for download...');
let id = JSON.parse(res.body).id;
let options = {
method: 'GET',
uri: 'https://simplypdf.com/api/download/' + id,
jar: cookiejar,
resolveWithFullResponse: true
};
request(options)
.pipe(fs.createWriteStream(path + '/' + fileName + '.pptx'))
.on('finish', () => {
log.success('finish downloading pptx...');
ctx.reply('finish downloading pptx.');
ctx.replyWithDocument({
source: path + '/' + fileName + '.pptx',
filename: fileName + '.pptx'
})
.then(() => {
del([path]).then(paths => {
log.debug('Deleted files and folders:\n', paths.join('\n'));
});
});
})
.on('error', function(error) {
console.log('Error in creating map', error);
});
} else {
log.error('ppt not ready');
checkStatusAndDownload(ctx, options, cookiejar, path, fileName);
}
}, 5000);
});
};
bot.command('start', ctx => {
ctx.reply(
'I convert PDF to PPTX. Simply drag and drop your PDF file 📎 and magically a PPTX will be sent to you.'
);
});
bot.command('help', ctx => {
ctx.reply(
'/help'
);
});
bot.startPolling();