-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
74 lines (55 loc) · 2.55 KB
/
index.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
const TelegramBot = require('node-telegram-bot-api');
const simpleGit = require('simple-git');
const archiver = require('archiver');
const axios = require('axios');
const fs = require('fs');
// Replace 'YOUR_TELEGRAM_TOKEN' with your actual Telegram bot token
const token = 'YOUR_TELEGRAM_TOKEN';
const bot = new TelegramBot(token, { polling: true });
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
const imageUrl = 'https://kinsta.com/wp-content/uploads/2018/04/what-is-github-1-1.png';
bot.sendPhoto(chatId, imageUrl, { caption: 'Welcome to the GitHub Clone Bot! 🤖' });
const keyboard = {
inline_keyboard: [[{ text: 'Developer', url: 'https://harshitethic.in' }]],
};
bot.sendMessage(chatId, 'You can clone a GitHub repository using this bot. Just send me the repository URL.\nType /help - For Help Menu', {
reply_markup: JSON.stringify(keyboard),
});
});
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id;
const helpMessage =
'To clone a GitHub repository, use the /clone command followed by the repository URL.\n\nFor example:\n/clone https://github.com/username/repository';
bot.sendMessage(chatId, helpMessage);
});
bot.onText(/\/clone (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const gitUrl = match[1];
try {
bot.sendMessage(chatId, 'Cloning the repository... ⌛️');
// Clone the repository
const repoName = gitUrl.split('/').pop().replace('.git', '');
const cloneDir = `./${repoName}`;
await simpleGit().clone(gitUrl, cloneDir);
bot.sendMessage(chatId, 'Repository cloned successfully! Zipping the files... ⌛️');
// Create a ZIP archive
const output = fs.createWriteStream(`${repoName}.zip`);
const archive = archiver('zip', { zlib: { level: 9 } });
archive.pipe(output);
archive.directory(cloneDir, repoName);
await archive.finalize();
bot.sendMessage(chatId, 'Files zipped successfully! Uploading the ZIP file... ⌛️');
// Read the ZIP file as a buffer
const zipFile = fs.readFileSync(`${repoName}.zip`);
// Send the ZIP file to Telegram
await bot.sendDocument(chatId, zipFile, {}, { filename: `${repoName}.zip` });
bot.sendMessage(chatId, 'ZIP file uploaded successfully! Cleaning up... ⌛️');
// Delete the cloned directory and ZIP file
fs.rmdirSync(cloneDir, { recursive: true });
fs.unlinkSync(`${repoName}.zip`);
bot.sendMessage(chatId, 'Clean up complete! 🧹');
} catch (error) {
bot.sendMessage(chatId, 'An error occurred while cloning the repository. Please try again later.');
}
});