-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegramBot.js
307 lines (276 loc) · 10.8 KB
/
telegramBot.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
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
const TelegramBot = require('node-telegram-bot-api');
const axios = require('axios');
const token = '123456789:xxxxxxxxxxxxxxxxxxxxxx'; // 请替换为您的Telegram Bot Token
const bot = new TelegramBot(token, { polling: true });
const API_BASE_URL = 'https://xyz.xyz.xyz/api'; // 替换为您的API URL
//项目地址:https://github.com/wensley/javbus-api-bot
// 发送请求的函数
async function sendRequest(url, options = {}) {
try {
const response = await axios({
...options,
url,
});
return response.data;
} catch (error) {
console.error(`[ERROR] Error sending request to ${url}:`, error);
console.error('Status:', error.response ? error.response.status : 'Unknown');
console.error('Data:', error.response ? error.response.data : 'No response');
console.error('Message:', error.message);
throw error;
}
}
// 处理 /start 指令
bot.onText(/\/start/, (msg) => {
const chatId = msg.chat.id;
console.log(`[INFO] User ${msg.from.username} started the bot.`);
bot.sendMessage(chatId, 'Welcome to the @avgifbusbot ! Use /help to see available commands.');
});
// 处理 /help 指令
bot.onText(/\/help/, (msg) => {
const chatId = msg.chat.id;
console.log(`[INFO] User ${msg.from.username} requested help.`);
const helpMessage = `
Available commands:
/search [keyword] - Search movies by keyword
/id [id] - Get movie details and magnet links by ID
/star [id] - Get star details by ID
/starsearch [keyword] - Search stars by keyword
/starpage [id] [page] - Get star movies by page
/latest - Get the latest movies
`;
bot.sendMessage(chatId, helpMessage);
});
// 搜索电影
bot.onText(/\/search (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const query = match[1];
console.log(`[INFO] User ${msg.from.username} searched for "${query}".`);
try {
const data = await sendRequest(`${API_BASE_URL}/movies/search`, {
params: { keyword: query }
});
const movies = data.movies;
let message = 'Search results:\n';
movies.forEach(movie => {
message += `\nTitle: ${movie.title}\nID: ${movie.id}\nDate: ${movie.date}\n`;
});
bot.sendMessage(chatId, message);
} catch (error) {
console.error(`[ERROR] Error fetching search results: ${error.message}`);
bot.sendMessage(chatId, 'Error fetching data from API');
}
});
// 获取电影详情和磁力链接
bot.onText(/\/id (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const movieId = match[1];
console.log(`[INFO] User ${msg.from.username} requested details for movie ID: ${movieId}`);
try {
console.log(`[INFO] Fetching movie with ID: ${movieId}`);
const movie = await sendRequest(`${API_BASE_URL}/movies/${movieId}`);
const title = movie.title || 'N/A';
const date = movie.date || 'N/A';
const tags = movie.tags ? movie.tags.join(', ') : 'N/A';
const genres = movie.genres ? movie.genres.map(genre => genre.name).join(', ') : 'N/A';
const stars = movie.stars ? movie.stars.map(star => star.name).join(', ') : 'N/A';
const image = movie.img || null;
let message = `
【Title】 <code>${title}</code>
【Code】 <code>${movieId}</code>
【Date】 <code>${date}</code>
`;
if (movie.stars && movie.stars.length > 0) {
message += '【Actor】 ';
movie.stars.forEach((star, index) => {
message += `<code>${star.name}</code>${index < movie.stars.length - 1 ? ' | ' : ''}`;
});
message += '\n';
}
message += `【Tags】 <code>${tags}</code>\n`;
// 获取磁力链接
let magnets = [];
try {
magnets = await sendRequest(`${API_BASE_URL}/magnets/${movieId}`, {
params: { gid: movie.gid, uc: movie.uc }
});
if (magnets && magnets.length > 0) {
// 使用第一个磁力链接的大小
const fileSize = magnets[0].size;
// 格式化文件大小
const formatSize = (sizeString) => {
const size = parseFloat(sizeString);
const unit = sizeString.replace(/[0-9.]/g, '').trim().toUpperCase();
if (unit === 'GB') {
return `${size.toFixed(2)} GB`;
} else if (unit === 'MB') {
return `${(size / 1024).toFixed(2)} GB`;
} else {
return `${size} ${unit}`;
}
};
const formattedSize = formatSize(fileSize);
message += `【Magnet】 ${movie.videoLength || 'N/A'}分钟 ${formattedSize}\n`;
magnets.slice(0, 3).forEach((magnet, index) => {
message += `【magnet Links ${index + 1}】 <code>${magnet.link}</code>\n`;
});
} else {
message += '【Magnet】 No magnet links available.\n';
}
} catch (error) {
console.error(`[ERROR] Error fetching magnet links: ${error.message}`);
message += '【Magnet】 Error fetching magnet links.\n';
}
// 发送电影详情消息
const options = {
parse_mode: "HTML",
reply_markup: {
inline_keyboard: [
[
{ text: "预览截图", callback_data: `sample_${movieId}_0` },
...(magnets && magnets.length > 0 ? [
{
text: "在线播放",
url: `https://keepshare.org/gc6ia801/${encodeURIComponent(magnets[0].link)}`
}
] : [])
],
[
{ text: "项目地址", url: "https://github.com/wensley/javbus-api-bot" },
{ text: "关注频道", url: "https://t.me/zhcnxyz" }
]
]
}
};
try {
if (image) {
await bot.sendPhoto(chatId, image, { caption: message, ...options });
} else {
throw new Error('No image available');
}
} catch (error) {
console.error(`[ERROR] Error sending photo: ${error.message}`);
// 如果发送图片失败,就只发送文字消息
await bot.sendMessage(chatId, message, options);
}
} catch (error) {
console.error(`[ERROR] Error fetching movie data: ${error.message}`);
await bot.sendMessage(chatId, 'Error fetching data from API.');
}
});
// 搜索演员
bot.onText(/\/starsearch (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const query = match[1];
console.log(`[INFO] User ${msg.from.username} searched for star: "${query}".`);
try {
const data = await sendRequest(`${API_BASE_URL}/stars/search`, {
params: { keyword: query }
});
const stars = data.stars;
let message = 'Search results:\n';
stars.forEach(star => {
message += `\nName: ${star.name}\nID: ${star.id}\n`;
});
bot.sendMessage(chatId, message);
} catch (error) {
console.error(`[ERROR] Error fetching star search results: ${error.message}`);
bot.sendMessage(chatId, 'Error fetching data from API');
}
});
// 获取演员电影按页
bot.onText(/\/starpage (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const [starId, page] = match[1].split(' ');
console.log(`[INFO] User ${msg.from.username} requested movies for star ID: ${starId} on page ${page}`);
try {
const data = await sendRequest(`${API_BASE_URL}/stars/${starId}/movies`, {
params: { page }
});
const movies = data.movies;
let message = 'Star movies:\n';
movies.forEach(movie => {
message += `\nTitle: ${movie.title}\nID: ${movie.id}\n`;
});
bot.sendMessage(chatId, message);
} catch (error) {
console.error(`[ERROR] Error fetching star movies: ${error.message}`);
bot.sendMessage(chatId, 'Error fetching star movies from API');
}
});
// 获取最新电影
bot.onText(/\/latest/, async (msg) => {
const chatId = msg.chat.id;
console.log(`[INFO] User ${msg.from.username} requested latest movies.`);
try {
const data = await sendRequest(`${API_BASE_URL}/movies`);
const movies = data.movies;
let message = 'Latest movies:\n';
movies.forEach(movie => {
message += `\nTitle: ${movie.title}\nID: ${movie.id}\nDate: ${movie.date}\n`;
});
bot.sendMessage(chatId, message);
} catch (error) {
console.error(`[ERROR] Error fetching latest movies: ${error.message}`);
bot.sendMessage(chatId, 'Error fetching latest movies from API');
}
});
// 处理其他未识别的指令
bot.on('message', (msg) => {
const chatId = msg.chat.id;
console.log(`[INFO] User ${msg.from.username} sent an unrecognized message: ${msg.text}`);
if (!msg.text.startsWith('/')) {
bot.sendMessage(chatId, 'Unrecognized command. Use /help to see available commands.');
}
});
// 处理样品图像按钮点击事件
bot.on('callback_query', async (query) => {
const chatId = query.message.chat.id;
const data = query.data;
if (data.startsWith('sample_')) {
const [_, movieId, pageStr] = data.split('_');
const page = parseInt(pageStr);
console.log(`[INFO] User ${query.from.username} (ID: ${query.from.id}) requested sample images for movie ID: ${movieId}, page: ${page}`);
try {
const movie = await sendRequest(`${API_BASE_URL}/movies/${movieId}`);
console.log(`[INFO] Successfully fetched movie data for ID: ${movieId}`);
if (movie.samples && movie.samples.length > 0) {
const startIndex = page * 5;
const endIndex = Math.min(startIndex + 5, movie.samples.length);
const samples = movie.samples.slice(startIndex, endIndex);
console.log(`[INFO] Sending ${samples.length} sample images for movie ID: ${movieId}, page: ${page}`);
// 创建媒体组
const mediaGroup = samples.map(sample => ({
type: 'photo',
media: sample.src
}));
// 发送媒体组
await bot.sendMediaGroup(chatId, mediaGroup);
console.log(`[INFO] Successfully sent media group to user ${query.from.username} (ID: ${query.from.id})`);
// 如果还有更多图片,添加"下一页"按钮
if (endIndex < movie.samples.length) {
await bot.sendMessage(chatId, '查看更多截图', {
reply_markup: {
inline_keyboard: [[
{ text: '下一页', callback_data: `sample_${movieId}_${page + 1}` }
]]
}
});
console.log(`[INFO] Sent "Next Page" button for movie ID: ${movieId}, next page: ${page + 1}`);
} else {
console.log(`[INFO] No more pages available for movie ID: ${movieId}`);
}
} else {
await bot.sendMessage(chatId, '没有可用的截图。');
console.log(`[INFO] No samples available for movie ID: ${movieId}`);
}
} catch (error) {
console.error(`[ERROR] Error fetching sample images for movie ID ${movieId}: ${error.message}`);
await bot.sendMessage(chatId, '获取截图时出错。');
}
// 回应回调查询以消除加载状态
await bot.answerCallbackQuery(query.id);
console.log(`[INFO] Answered callback query for user ${query.from.username} (ID: ${query.from.id})`);
}
});
module.exports = bot;