logs?
This commit is contained in:
89
src/bot.ts
89
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<void> {
|
||||
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 (error) {
|
||||
console.error('Send audio error:', error);
|
||||
await this.bot.sendMessage(chatId,
|
||||
'❌ Не удалось отправить аудиофайл. Попробуйте еще раз.\n\n' +
|
||||
`Прямая ссылка: ${audioUrl}`
|
||||
);
|
||||
} 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: 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user