This commit is contained in:
Andrey Kondratev
2025-08-30 17:06:00 +05:00
parent 07ed434375
commit 8f1ef3c621
4 changed files with 72 additions and 13 deletions

View File

@@ -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
}