This commit is contained in:
Andrey Kondratev
2025-08-29 15:00:42 +05:00
parent 45a071ab4f
commit 63904343bc

View File

@@ -159,24 +159,27 @@ export class QuixoticBot {
this.bot.on('web_app_data', async (msg: Message) => { this.bot.on('web_app_data', async (msg: Message) => {
console.log('🔍 Web app data received:', msg.web_app?.data); console.log('🔍 Web app data received:', msg.web_app?.data);
const chatId = msg.chat.id; const chatId = msg.chat.id;
const userId = msg.from?.id;
if (!msg.web_app?.data) { if (!msg.web_app?.data) {
console.log('❌ No web app data found'); console.log('❌ No web app data found');
return; return;
} }
const data: WebAppData = JSON.parse(msg.web_app.data);
console.log('📝 Parsed data:', data);
try { try {
const data: WebAppData = JSON.parse(msg.web_app.data);
console.log('📝 Parsed data:', data);
if (data.action === 'send_audio') { if (data.action === 'send_audio') {
console.log('🎵 Sending audio file:', data.title); console.log(`🎵 Processing audio request for user ${userId}, chat ${chatId}: ${data.title}`);
await this.sendAudioFile(chatId, data.audioUrl, data.title); await this.sendAudioFile(chatId, data.audioUrl, data.title);
} else { } else {
console.log('⚠️ Unknown action:', data.action); console.log('⚠️ Unknown action:', data.action);
await this.bot.sendMessage(chatId, '❌ Неизвестное действие.');
} }
} catch (error) { } catch (parseError: any) {
console.error('Web app data error:', error); console.error('Web app data parse error:', parseError.message);
console.error('Raw data:', msg.web_app?.data);
await this.bot.sendMessage(chatId, '❌ Ошибка обработки данных.'); await this.bot.sendMessage(chatId, '❌ Ошибка обработки данных.');
} }
}); });
@@ -217,9 +220,22 @@ export class QuixoticBot {
} }
}); });
// Error handler // Error handler with detailed logging
this.bot.on('error', (error: Error) => { this.bot.on('error', (error: any) => {
console.error('Telegram bot error:', error); console.error('🚨 Telegram bot error:', error.message || error);
console.error('Error code:', error.code);
console.error('Full error:', error);
});
// Handle polling errors specifically
this.bot.on('polling_error', (error: any) => {
console.error('🚨 Telegram polling error:', error.message || error);
console.error('Error code:', error.code);
// Don't crash on polling errors, just log them
if (error.code === 'ETELEGRAM') {
console.warn('⚠️ Telegram API error - continuing operation');
}
}); });
console.log('✅ Bot handlers setup complete'); console.log('✅ Bot handlers setup complete');
@@ -231,21 +247,52 @@ export class QuixoticBot {
private async sendAudioFile(chatId: number, audioUrl: string, title: string): Promise<void> { private async sendAudioFile(chatId: number, audioUrl: string, title: string): Promise<void> {
try { try {
console.log(`🎵 Attempting to send audio to chat ${chatId}: ${audioUrl}`);
// Send initial message
await this.bot.sendMessage(chatId, '⏳ Подготавливаю MP3 файл...'); await this.bot.sendMessage(chatId, '⏳ Подготавливаю MP3 файл...');
// Send audio file // Try sending as audio first
await this.bot.sendAudio(chatId, audioUrl, { try {
title: title, await this.bot.sendAudio(chatId, audioUrl, {
performer: 'Quixotic', title: title,
caption: `🎵 ${title}` performer: 'Quixotic',
}); caption: `🎵 ${title}`,
parse_mode: undefined // Explicitly avoid parse mode issues
});
console.log(`✅ Audio sent successfully to chat ${chatId}`);
} catch (audioError: any) {
console.error('SendAudio failed, trying sendDocument:', audioError.message);
// Fallback: try sending as document
try {
await this.bot.sendDocument(chatId, audioUrl, {
caption: `🎵 ${title}`,
parse_mode: undefined
});
console.log(`✅ Document sent successfully to chat ${chatId}`);
} catch (documentError: any) {
console.error('SendDocument also failed:', documentError.message);
throw documentError;
}
}
} catch (error) { } catch (error: any) {
console.error('Send audio error:', error); console.error('Complete send audio failure:', error);
await this.bot.sendMessage(chatId,
'❌ Не удалось отправить аудиофайл. Попробуйте еще раз.\n\n' + try {
`Прямая ссылка: ${audioUrl}` await this.bot.sendMessage(chatId,
); '❌ Не удалось отправить аудиофайл. Вот прямая ссылка для скачивания:
' +
`🔗 ${audioUrl}`,
{ parse_mode: undefined }
);
} catch (msgError: any) {
console.error('Failed to send error message:', msgError.message);
}
} }
} }