Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (88 loc) · 4.42 KB
/
Copy pathindex.js
File metadata and controls
107 lines (88 loc) · 4.42 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
const TelegramBot = require('node-telegram-bot-api');
const token = '🥺🥺🥺🥺🥺🥺🥺';
const bot = new TelegramBot(token, { polling: true });
bot.onText(/\/start/, async (msg) => {
const messageText = "Hi! Send me any file, and I will convert it to a direct download link.\n 😉"
await bot.sendMessage(msg.chat.id, messageText );
// const telegram = `[Param](https://t.me/parampanchal)`;
await bot.sendMessage(msg.chat.id, `Made with ❤ by [Param](https://t.me/parampanchal)`,{ parse_mode: 'Markdown', disable_web_page_preview: true })
});
bot.on('document', async (msg) => {
try {
const fileId = msg.document.file_id;
const fileName = msg.document.file_name;
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: ${fileName}\nDownload URL: ${fileUrl}`;
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling file: ${error.message}`);
}
});
bot.on('photo', async (msg) => {
try {
// Photo messages contain an array of photo sizes, we typically use the last one (highest resolution)
const photo = msg.photo[msg.photo.length - 1];
const fileId = photo.file_id;
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: photo.jpg\nDownload URL: ${fileUrl}`; // You can customize the file name if needed
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling photo: ${error.message}`);
}
});
bot.on('audio', async (msg) => {
try {
const fileId = msg.audio.file_id;
const fileName = `${msg.audio.performer} - ${msg.audio.title}.mp3`;
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: ${fileName}\nDownload URL: ${fileUrl}`;
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling audio: ${error.message}`);
}
});
bot.on('video', async (msg) => {
try {
const fileId = msg.video.file_id;
const fileName = `${msg.video.file_name}.mp4`;
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: ${fileName}\nDownload URL: ${fileUrl}`;
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling video: ${error.message}`);
}
});
bot.on('voice', async (msg) => {
try {
const fileId = msg.voice.file_id;
const fileName = `voice_message_${Date.now()}.ogg`; // You can customize the file name if needed
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: ${fileName}\nDownload URL: ${fileUrl}`;
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling voice message: ${error.message}`);
}
});
bot.on('animation', async (msg) => {
try {
const fileId = msg.animation.file_id;
const fileName = `${msg.animation.file_name}.gif`;
const fileUrl = await getFileUrl(fileId);
const messageText = `File Name: ${fileName}\nDownload URL: ${fileUrl}`;
await bot.sendMessage(msg.chat.id, messageText);
} catch (error) {
await bot.sendMessage(msg.chat.id, `Error handling animation: ${error.message}`);
}
});
// Helper function to retrieve file URL using file_id
async function getFileUrl(fileId) {
const file = await bot.getFile(fileId);
return `https://api.telegram.org/file/bot${token}/${file.file_path}`;
}
bot.on('message', (msg) => {
// Handle messages that are not file uploads
if (!msg.document && !msg.photo && !msg.audio && !msg.video && !msg.voice && !msg.animation) {
bot.sendMessage(msg.chat.id, "Please upload a file to get a download link.");
}
});
console.log("BOT started");