From 63904343bce5d67f7d05ec4c166ec0fcbdb96a40 Mon Sep 17 00:00:00 2001 From: Andrey Kondratev <81143241+cockroach-eater@users.noreply.github.com> Date: Fri, 29 Aug 2025 15:00:42 +0500 Subject: [PATCH] logs? --- src/bot.ts | 89 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index cc1ab14..374d093 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -159,24 +159,27 @@ export class QuixoticBot { this.bot.on('web_app_data', async (msg: Message) => { console.log('🔍 Web app data received:', msg.web_app?.data); const chatId = msg.chat.id; + const userId = msg.from?.id; if (!msg.web_app?.data) { console.log('❌ No web app data found'); return; } - const data: WebAppData = JSON.parse(msg.web_app.data); - console.log('📝 Parsed data:', data); - try { + const data: WebAppData = JSON.parse(msg.web_app.data); + console.log('📝 Parsed data:', data); + 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); } else { console.log('⚠️ Unknown action:', data.action); + await this.bot.sendMessage(chatId, '❌ Неизвестное действие.'); } - } catch (error) { - console.error('Web app data error:', error); + } catch (parseError: any) { + console.error('Web app data parse error:', parseError.message); + console.error('Raw data:', msg.web_app?.data); await this.bot.sendMessage(chatId, '❌ Ошибка обработки данных.'); } }); @@ -217,9 +220,22 @@ export class QuixoticBot { } }); - // Error handler - this.bot.on('error', (error: Error) => { - console.error('Telegram bot error:', error); + // Error handler with detailed logging + this.bot.on('error', (error: any) => { + 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'); @@ -231,21 +247,52 @@ export class QuixoticBot { private async sendAudioFile(chatId: number, audioUrl: string, title: string): Promise { try { + console.log(`🎵 Attempting to send audio to chat ${chatId}: ${audioUrl}`); + + // Send initial message await this.bot.sendMessage(chatId, '⏳ Подготавливаю MP3 файл...'); - // Send audio file - await this.bot.sendAudio(chatId, audioUrl, { - title: title, - performer: 'Quixotic', - caption: `🎵 ${title}` - }); + // Try sending as audio first + try { + await this.bot.sendAudio(chatId, audioUrl, { + title: 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) { - console.error('Send audio error:', error); - await this.bot.sendMessage(chatId, - '❌ Не удалось отправить аудиофайл. Попробуйте еще раз.\n\n' + - `Прямая ссылка: ${audioUrl}` - ); + } catch (error: any) { + console.error('Complete send audio failure:', error); + + try { + await this.bot.sendMessage(chatId, + '❌ Не удалось отправить аудиофайл. Вот прямая ссылка для скачивания: + +' + + `🔗 ${audioUrl}`, + { parse_mode: undefined } + ); + } catch (msgError: any) { + console.error('Failed to send error message:', msgError.message); + } } }