-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
132 lines (110 loc) · 3.98 KB
/
bot.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
const { TelegramClient } = require('telegram');
const { StringSession } = require('telegram/sessions');
const path = require('path');
const fs = require('fs');
const PdfExtractor = require('pdf-extractor').PdfExtractor;
const CanvasRenderer = require('pdf-extractor').CanvasRenderer;
const FileWriter = require('pdf-extractor').FileWriter;
require('dotenv').config();
class JPGWriter extends FileWriter {
getFilePathForPage(page) {
return super.getPagePath(page.pageNumber, 'png');
}
writeCanvasPage(page, viewport, canvas) {
return this.writeStreamToFile(canvas.jpgStream(), this.getFilePathForPage(page));
}
}
class JPGCanvasRenderer extends CanvasRenderer {
getWriters(writerOptions) {
let writers = super.getWriters(writerOptions);
writers.push(new JPGWriter(this.outputDir, writerOptions));
return writers;
}
}
const pdfExtractor = (url) =>
new PdfExtractor(url, {
pdfJs: { disableFontFace: true },
viewportScale: (width, height) => {
//dynamic zoom based on rendering a page to a fixed page size
if (width > height) {
//landscape: 1100px wide
return 1100 / width;
}
//portrait: 800px wide
return 800 / width;
},
// all the pages
pageRange: {
start: 1,
end: 100,
},
JPGCanvasRenderer: JPGCanvasRenderer,
});
const stringSession = process.env.STRING_SESSION;
const apiId = proccess.env.API_ID;
const apiHash = proccess.env.API_HASH;
(async () => {
const client = new TelegramClient(new StringSession(stringSession), apiId, apiHash, {
connectionRetries: 5,
});
await client.start();
client.addEventHandler(async (update) => {
console.log('Received new Update');
const chatID = update.message.chatId;
if (fs.existsSync(`./images/${chatID}`)) {
fs.rmSync(`./images/${chatID}`, { recursive: true });
}
if (update.message.message.startsWith('/start')) {
client.sendMessage(chatID, {
message:
'به ربات تبدیل pdf به عکس خوش آمدید.\n برای کار با بات، میتوانید یک فایل pdf را به بات ارسال کنید و منتظر عملیات تبدیل و ارسال عکس باشید.',
});
}
// check if the message is a document and if it is a pdf file
if (update.message?.media && update.message?.media?.document.mimeType === 'application/pdf') {
console.log('Got the PDF file');
const pdfData = await client.downloadMedia(update.message.media, {
workers: 5,
});
const chatFolderID = `./images/${chatID}`;
if (!fs.existsSync(chatFolderID)) {
fs.mkdir(path.join('./images/', `${chatID}`), (err) => {
if (err) {
return console.error('error create folder: ', err);
}
console.log('Directory created successfully!');
});
}
await fs.writeFileSync(`./images/${chatID}/pdfData.pdf`, pdfData);
try {
client.sendMessage(update.message.chatId, {
message: 'درحال تبدیل pdf به عکس...',
});
await pdfExtractor(`./images/${chatID}`)
.parse(`./images/${chatID}/pdfData.pdf`)
.then((res) => {
console.log('# End of Document - done');
})
.catch(function (err) {
console.error('Error: ' + err);
});
client.editMessage(update.message.chatId, update.message.id.messageLocalId, {
postMessage: 'تبدیل با موفقیت انجام شد',
});
} catch (error) {
console.log(error);
}
const files = fs.readdirSync(chatFolderID).filter((file) => file.endsWith('.png'));
// sort files by name
const sortedFiles = files.sort((a, b) => {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
for (const file of sortedFiles) {
console.log('file: ', file);
await client.sendFile(chatID, {
file: `./images/${chatID}/${file}`,
});
}
}
});
})();