-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtelebot.js
143 lines (117 loc) · 4.19 KB
/
telebot.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
import { TiktokDL } from './lib/ttapi.js';
import { ReelsUpload } from './lib/browserHandler.js';
import dotenv from 'dotenv';
import TeleBot from 'telebot';
import axios from 'axios';
import ProgressBar from 'progress';
import chalk from 'chalk';
import path from 'path';
import fs from 'fs';
dotenv.config();
const bot = new TeleBot({
token: process.env.BOT_TOKEN
});
const userStats = new Map();
const MAX_DOWNLOADS_PER_DAY = 5;
function updateUserStats(userId) {
const today = new Date().toDateString();
if (!userStats.has(userId)) {
userStats.set(userId, { date: today, count: 1 });
} else {
const stats = userStats.get(userId);
if (stats.date !== today) {
stats.date = today;
stats.count = 1;
} else {
stats.count++;
}
}
}
function canUserDownload(userId) {
const stats = userStats.get(userId);
return !stats || stats.count < MAX_DOWNLOADS_PER_DAY;
}
async function downloadTikTokVideo(url, outputPath) {
try {
const result = await TiktokDL(url);
const video = result.result.video[0];
const response = await axios({
url: video,
method: 'GET',
responseType: 'stream'
});
const totalLength = response.headers['content-length'];
const progressBar = new ProgressBar(`[ ${chalk.hex('#ffff1c')("Downloading")} ] [${chalk.hex('#6be585')(':bar')}] :percent in :elapseds`, {
width: 40,
complete: '<',
incomplete: '•',
renderThrottle: 1,
total: parseInt(totalLength)
});
const writer = fs.createWriteStream(outputPath);
response.data.on('data', (chunk) => progressBar.tick(chunk.length));
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
} catch (error) {
console.error('Error downloading video:', error);
throw error;
}
}
async function handleTikTokUrl(msg, url) {
try {
const userId = msg.from.id;
if (!canUserDownload(userId)) {
return bot.sendMessage(msg.chat.id, `You've reached the daily limit of ${MAX_DOWNLOADS_PER_DAY} downloads.`);
}
await bot.sendMessage(msg.chat.id, "Processing...", { replyToMessage: msg.message_id });
const result = await TiktokDL(url);
const namafile = result.result.id;
const caption = result.result.description;
const outputPath = path.resolve('download', `${namafile}.mp4`);
if (!fs.existsSync('download')) fs.mkdirSync('download');
await downloadTikTokVideo(url, outputPath);
const upload = await ReelsUpload(namafile, caption);
if (upload.status === "success") {
await bot.sendMessage(msg.chat.id, upload.message);
updateUserStats(userId);
} else {
await bot.sendMessage(msg.chat.id, `Upload failed: ${upload.message}`);
}
fs.unlinkSync(outputPath);
} catch (error) {
console.error('Error processing TikTok URL:', error);
await bot.sendMessage(msg.chat.id, `An error occurred: ${error.message}`);
}
}
bot.on(['/start', '/help'], (msg) => {
const helpMessage = `
Welcome to TikTok Downloader Bot!
Commands:
/start or /help - Show this help message
/stats - Show your download statistics
To download a TikTok video, simply send the TikTok URL to the bot.
Note: You can download up to ${MAX_DOWNLOADS_PER_DAY} videos per day.
`;
return bot.sendMessage(msg.chat.id, helpMessage);
});
bot.on('/stats', (msg) => {
const userId = msg.from.id;
const stats = userStats.get(userId);
if (stats) {
return bot.sendMessage(msg.chat.id, `Today (${stats.date}) you've downloaded ${stats.count} videos.`);
} else {
return bot.sendMessage(msg.chat.id, "You haven't downloaded any videos today.");
}
});
bot.on('text', async (msg) => {
const body = msg.text;
const tiktokRegex = /https?:\/\/(?:m|www|vm|vt)?\.?tiktok\.com\/\S+/;
const match = body.match(tiktokRegex);
if (match) {
await handleTikTokUrl(msg, match[0]);
}
});
bot.start();