-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
335 lines (282 loc) · 10.1 KB
/
Copy pathmain.js
File metadata and controls
335 lines (282 loc) · 10.1 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import axios from 'axios';
import fs from 'fs';
import path from 'path';
import { Telegraf, Markup } from 'telegraf'
import { message } from 'telegraf/filters'
// Custom imports
import config from './config.js';
import log from './log.js';
// Check if config variables are defined
if (!config.token) {
throw new Error('TELEGRAM_BOT_TOKEN is not defined.');
}
if (!config.id) {
throw new Error('TELEGRAM_CHAT_ID is not defined.');
}
// Create a bot using the token
const bot = new Telegraf(config.token);
// Map to store pending download requests waiting for destination selection
// Key format: "chatId:messageId"
// Value format: { url, fileName, destinations }
const pendingDownloads = new Map();
/**
* Downloads a file from the specified URL and saves it to the given file path.
*
* @param {Object} ctx - The context object, used for replying and logging.
* @param {string} url - The URL of the file to download.
* @param {string} filePath - The local file path where the downloaded file will be saved.
* @returns {Promise<void>} A promise that resolves when the file is successfully downloaded, or rejects on error.
*/
async function downloadFile(ctx, url, filePath) {
const writer = fs.createWriteStream(filePath);
const fileName = path.basename(filePath);
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
const message = `${fileName} downloaded to ${filePath}`;
ctx.reply(`🟢 ${fileName} downloaded successfully`)
log.success(message);
resolve();
});
writer.on('error', (err) => {
const message = `Error downloading ${fileName}`;
ctx.reply(`🔴 ${message}`);
log.error(message, err);
reject();
});
});
}
/**
* Extracts the file ID from a message object.
*
* @param {Object} message - The message object containing file information.
* @returns {string|undefined} The file ID of the document or the last photo in the array, or undefined if neither is present.
*/
function getFileId(message) {
if (message.audio) {
const audio = message.audio;
return audio.file_id;
} else if (message.document) {
const document = message.document;
return document.file_id;
} else if (message.photo) {
const photo = message.photo.pop();
return photo.file_id;
} else if (message.video) {
const video = message.video;
return video.file_id;
}
}
/**
* Extracts the file name from a given message object based on its type.
*
* @param {Object} message - The message object containing file information.
* @returns {string|undefined} The extracted file name, or `undefined` if no valid file type is found.
*/
function getFileName(message) {
if (message.audio) {
const audio = message.audio;
const fileName = audio.file_name;
return fileName;
} else if (message.document) {
const document = message.document;
const fileName = document.file_name;
return fileName;
} else if (message.photo) {
const photo = message.photo.pop();
const fileName = `${photo.file_unique_id}.jpeg`
return fileName;
} else if (message.video) {
const video = message.video;
const fileName = video.file_name;
return fileName;
}
}
/**
* Retrieves the destination(s) for a given file name and type based on its extension.
* Returns an array of { label, path } objects. Extensions are matched against the
* configured extensions lists.
*
* @param {string} fileName - The name of the file, including its extension.
* @param {string} type - The type of the file, such as 'audio', 'document', 'text', 'photo' or 'video'.
* @returns {Array<{label: string, path: string}>} Array of available destinations for this file.
*/
function getDestinations(fileName, type) {
const ext = path.extname(fileName).toLowerCase();
const { extensions, destinations, defaultPath } = config;
// Type-specific routing
if (type === 'audio') {
return destinations.audio;
}
if (type === 'photo') {
return destinations.photo;
}
if (type === 'video') {
return destinations.video;
}
// Document/Text: check extension to determine actual type
if (type === 'document' || type === 'text') {
if (extensions.audio.includes(ext)) return destinations.audio;
if (extensions.document.includes(ext)) return destinations.document;
if (extensions.photo.includes(ext)) return destinations.photo;
if (extensions.torrent.includes(ext)) return destinations.torrent;
if (extensions.video.includes(ext)) return destinations.video;
}
// Fallback to default destination
return [{ label: 'Default', path: defaultPath }];
}
/**
* Check if a message has downloadable content (audio, document, photo, text, or video).
* Filters out Telegram service messages (e.g., "auto-delete enabled" notifications).
*
* @param {Object} message - The message object.
* @returns {boolean} True if message has downloadable content.
*/
function isMessageDownloadable(message) {
if (!message) return false;
return !!(message.audio || message.document || message.photo || message.text || message.video);
}
/**
* Determines the type of a message from the given context object.
*
* @param {Object} ctx - The context object containing the message.
* @returns {string|null} The type of the message ('audio', 'document', 'photo', 'text', 'video'), or `null` if the type is unknown.
*/
function getMessageType(ctx) {
const message = ctx.message;
if (message.audio) {
return 'audio';
} else if (message.document) {
return 'document';
} else if (message.photo) {
return 'photo';
} else if (message.text) {
return 'text';
} else if (message.video) {
return 'video';
} else {
const errorMsg = `Message type is unknown`;
ctx.reply(`🔴 ${errorMsg}`);
log.error(errorMsg);
return null;
}
}
/**
* Handles file download logic with optional destination selection.
* If multiple destinations are configured for the file type, sends an inline keyboard
* for the user to choose. Otherwise, downloads immediately.
*
* @param {Object} ctx - The Telegraf context object
* @param {string} url - The download URL
* @param {string} fileName - The file name
* @param {string} type - The detected file type
*/
async function handleFileMessage(ctx, url, fileName, type) {
// Guard: ensure ctx and ctx.message exist
if (!ctx || !ctx.message) {
log.error('Invalid context: ctx or ctx.message is undefined');
return;
}
const destinations = getDestinations(fileName, type);
log.info(`${fileName} detected as ${type}`);
// Single destination: download immediately
if (destinations.length === 1) {
const filePath = path.resolve(destinations[0].path, fileName);
return downloadFile(ctx, url, filePath);
}
// Multiple destinations: ask user which one to use
try {
const key = `${ctx.chat.id}:${ctx.message.message_id}`;
pendingDownloads.set(key, { url, fileName, destinations });
const buttons = destinations.map((dest, i) =>
Markup.button.callback(dest.label, `dest:${key}:${i}`)
);
await ctx.reply(`Where should I save ${fileName}?`, Markup.inlineKeyboard(buttons));
} catch (err) {
log.error(`Error in handleFileMessage:`, err);
try {
await ctx.reply(`🔴 Error selecting destination`);
} catch (replyErr) {
log.error(`Failed to send error message:`, replyErr);
}
}
}
// Listen for text messages
bot.on(message('text'), async (ctx) => {
// Check if the message is from the authorized user
if (ctx.message.from.id !== config.id) {
log.error(`Unauthorized user ${ctx.message.from.id} tried to send a message`);
return;
}
const type = getMessageType(ctx);
if (!type) return;
const text = ctx.message.text;
const match = text.match(/(https?:\/\/[^\s]+)/);
if (match) {
const url = match[1];
const fileName = url.split('/').pop();
await handleFileMessage(ctx, url, fileName, type);
}
});
// Listen for audio | document | photo | video messages
bot.on('message', async (ctx) => {
// Ignore Telegram service messages (e.g., "auto-delete enabled" notifications)
if (!isMessageDownloadable(ctx.message)) {
return;
}
// Check if the message is from the authorized user
if (ctx.message.from.id !== config.id) {
log.error(`Unauthorized user ${ctx.message.from.id} tried to send a message`);
return;
}
const type = getMessageType(ctx);
if (!type) return;
const fileId = getFileId(ctx.message);
const fileUrl = await ctx.telegram.getFileLink(fileId);
const fileName = getFileName(ctx.message);
await handleFileMessage(ctx, fileUrl.href, fileName, type);
});
// Handle inline keyboard button presses (destination selection)
bot.action(/^dest:/, async (ctx) => {
try {
await ctx.answerCbQuery();
const data = ctx.callbackQuery.data;
log.info(`Destination callback: ${data}`);
// Parse callback data: "dest:chatId:messageId:destinationIndex"
const parts = data.split(':');
if (parts.length !== 4) {
log.error(`Invalid callback format: ${data}`);
return;
}
const key = `${parts[1]}:${parts[2]}`;
const index = parseInt(parts[3], 10);
const pending = pendingDownloads.get(key);
if (!pending) {
log.warning(`Pending download not found for key: ${key}`);
await ctx.reply('🔴 This download request has expired.');
return;
}
const dest = pending.destinations[index];
if (!dest) {
log.error(`Invalid destination index: ${index}`);
return;
}
pendingDownloads.delete(key);
log.info(`Saving ${pending.fileName} to ${dest.label} (${dest.path})`);
const filePath = path.resolve(dest.path, pending.fileName);
await downloadFile(ctx, pending.url, filePath);
} catch (err) {
log.error(`Error handling destination callback:`, err);
}
});
// Start the bot (allowedUpdates ensures Telegram sends callback_query events)
bot.launch({ allowedUpdates: ['message', 'callback_query'] });
log.success('file-bot started and waiting for messages');
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))