artist
This commit is contained in:
55
src/bot.ts
55
src/bot.ts
@@ -254,28 +254,59 @@ export class QuixoticBot {
|
||||
return this.sendAudioFileInternal(chatId, audioUrl, title, performer, thumbnail);
|
||||
}
|
||||
|
||||
private async sendAudioFileInternal(chatId: number, audioUrl: string, title: string, performer?: string, thumbnail?: string): Promise<void> {
|
||||
private async sendAudioFileInternal(chatId: number, audioUrlOrPath: string, title: string, performer?: string, thumbnail?: string): Promise<void> {
|
||||
try {
|
||||
console.log(`📤 Sending: ${title} to chat ${chatId}`);
|
||||
|
||||
// Try sending as audio
|
||||
// Check if it's a URL or local file path
|
||||
const isUrl = audioUrlOrPath.startsWith('http');
|
||||
let filePath = audioUrlOrPath;
|
||||
|
||||
if (isUrl) {
|
||||
// Extract filename from URL and construct local path
|
||||
const urlParts = audioUrlOrPath.split('/');
|
||||
const filename = urlParts[urlParts.length - 1];
|
||||
filePath = require('path').join(process.cwd(), 'downloads', filename);
|
||||
}
|
||||
|
||||
// Generate custom filename for display
|
||||
const safeTitle = (title || '').replace(/[^\w\s-]/g, '').replace(/\s+/g, '_').substring(0, 30);
|
||||
const safePerformer = (performer || '').replace(/[^\w\s-]/g, '').replace(/\s+/g, '_').substring(0, 20);
|
||||
const customFilename = safePerformer ? `${safePerformer} - ${safeTitle}` : `${safeTitle}`;
|
||||
|
||||
// Try sending as audio with custom filename
|
||||
try {
|
||||
await this.bot.sendAudio(chatId, audioUrl, {
|
||||
const fs = require('fs');
|
||||
|
||||
// Check if file exists
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error('File not found: ' + filePath);
|
||||
}
|
||||
|
||||
await this.bot.sendAudio(chatId, filePath, {
|
||||
title: title,
|
||||
performer: performer || 'SoundCloud',
|
||||
performer: performer,
|
||||
caption: undefined,
|
||||
thumbnail: thumbnail,
|
||||
parse_mode: undefined
|
||||
}, {
|
||||
filename: customFilename,
|
||||
contentType: 'audio/mpeg'
|
||||
});
|
||||
console.log(`✅ Audio sent: ${title}`);
|
||||
return;
|
||||
|
||||
} catch {
|
||||
// Fallback: try as document
|
||||
} catch (error: any) {
|
||||
console.log('Audio send failed, trying as document...', error.message);
|
||||
|
||||
// Fallback: try as document with custom filename
|
||||
try {
|
||||
await this.bot.sendDocument(chatId, audioUrl, {
|
||||
await this.bot.sendDocument(chatId, filePath, {
|
||||
caption: undefined,
|
||||
parse_mode: undefined
|
||||
}, {
|
||||
filename: customFilename,
|
||||
contentType: 'audio/mpeg'
|
||||
});
|
||||
console.log(`✅ Document sent: ${title}`);
|
||||
return;
|
||||
@@ -288,11 +319,13 @@ export class QuixoticBot {
|
||||
} catch (error: any) {
|
||||
console.error('❌ Send failed:', error.message);
|
||||
|
||||
// Send fallback with link
|
||||
// Send fallback with link if it was a URL
|
||||
try {
|
||||
await this.bot.sendMessage(chatId,
|
||||
`❌ Не удалось отправить файл.\n🎵 ${title}\n🔗 ${audioUrl}`
|
||||
);
|
||||
const message = audioUrlOrPath.startsWith('http')
|
||||
? `❌ Не удалось отправить файл.\n🎵 ${title}\n🔗 ${audioUrlOrPath}`
|
||||
: `❌ Не удалось отправить файл: ${title}`;
|
||||
|
||||
await this.bot.sendMessage(chatId, message);
|
||||
} catch {
|
||||
// Silent fail
|
||||
}
|
||||
|
||||
@@ -99,8 +99,7 @@ app.post('/api/convert', async (req: Request, res: Response) => {
|
||||
|
||||
// Generate safe filename
|
||||
const safeTitle = (title || '').replace(/[^\w\s-]/g, '').replace(/\s+/g, '_').substring(0, 50);
|
||||
const safePerformer = (performer || '').replace(/[^\w\s-]/g, '').replace(/\s+/g, '_').substring(0, 20);
|
||||
const filename = safePerformer ? `${safePerformer} - ${safeTitle}.mp3` : `${safeTitle}.mp3`;
|
||||
const filename = `${videoId}_${safeTitle}.mp3`;
|
||||
const outputPath = path.join(downloadsDir, filename);
|
||||
|
||||
// Check if file already exists
|
||||
|
||||
Reference in New Issue
Block a user