debug n fix
This commit is contained in:
1
go
Submodule
1
go
Submodule
Submodule go added at 89a7f4ab11
@@ -57,6 +57,13 @@ class QuixoticApp {
|
|||||||
this.tg.ready();
|
this.tg.ready();
|
||||||
this.tg.expand();
|
this.tg.expand();
|
||||||
this.tg.MainButton.hide();
|
this.tg.MainButton.hide();
|
||||||
|
|
||||||
|
// Debug Telegram user info
|
||||||
|
console.log('🔧 Telegram WebApp initialized');
|
||||||
|
console.log('👤 User ID:', this.tg.initDataUnsafe?.user?.id);
|
||||||
|
console.log('📋 Full initData:', this.tg.initDataUnsafe);
|
||||||
|
} else {
|
||||||
|
console.log('❌ Telegram WebApp not available');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.searchInput = document.getElementById('searchInput') as HTMLInputElement;
|
this.searchInput = document.getElementById('searchInput') as HTMLInputElement;
|
||||||
@@ -248,41 +255,60 @@ class QuixoticApp {
|
|||||||
console.log('🔧 About to send to Telegram, tg available:', !!this.tg);
|
console.log('🔧 About to send to Telegram, tg available:', !!this.tg);
|
||||||
|
|
||||||
if (this.tg) {
|
if (this.tg) {
|
||||||
// Send to Telegram chat
|
// Try WebApp method first (might not work)
|
||||||
const payload = {
|
const payload = {
|
||||||
action: 'send_audio',
|
action: 'send_audio',
|
||||||
audioUrl: data.audioUrl,
|
audioUrl: data.audioUrl,
|
||||||
title: title
|
title: title
|
||||||
};
|
};
|
||||||
console.log('📤 Sending data to Telegram via sendData:', payload);
|
console.log('📤 Attempting WebApp sendData first:', payload);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.tg.sendData(JSON.stringify(payload));
|
this.tg.sendData(JSON.stringify(payload));
|
||||||
console.log('✅ Data sent via Telegram.sendData');
|
console.log('✅ WebApp sendData called');
|
||||||
this.showMessage('✓ MP3 готов! Отправляем в чат...', 'success');
|
this.showMessage('✓ MP3 готов! Отправляем в чат...', 'success');
|
||||||
|
} catch (sendError) {
|
||||||
|
console.error('❌ WebApp sendData failed:', sendError);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always use direct API as primary method now
|
||||||
|
const userId = this.tg?.initDataUnsafe?.user?.id;
|
||||||
|
console.log('👤 Current user ID for sending:', userId);
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
console.error('❌ No user ID available from Telegram WebApp');
|
||||||
|
this.showMessage('❌ Ошибка: не удается определить пользователя', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Fallback: also try to send via HTTP request to our server
|
|
||||||
setTimeout(async () => {
|
|
||||||
try {
|
try {
|
||||||
console.log('🔄 Sending fallback notification to server...');
|
console.log('🔄 Sending via direct API...');
|
||||||
await fetch('/api/telegram-notify', {
|
const requestData = {
|
||||||
method: 'POST',
|
userId: userId,
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
userId: this.tg?.initDataUnsafe?.user?.id,
|
|
||||||
audioUrl: data.audioUrl,
|
audioUrl: data.audioUrl,
|
||||||
title: title
|
title: title
|
||||||
})
|
};
|
||||||
});
|
console.log('📦 Request data:', requestData);
|
||||||
} catch (fallbackError) {
|
|
||||||
console.log('Fallback notification failed (not critical):', fallbackError);
|
|
||||||
}
|
|
||||||
}, 2000); // Wait 2 seconds before fallback
|
|
||||||
|
|
||||||
} catch (sendError) {
|
const directResponse = await fetch('/api/telegram-send', {
|
||||||
console.error('❌ Failed to send via Telegram.sendData:', sendError);
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(requestData)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (directResponse.ok) {
|
||||||
|
const result = await directResponse.json();
|
||||||
|
console.log('✅ Direct API success:', result);
|
||||||
|
this.showMessage('✅ MP3 отправлен в чат!', 'success');
|
||||||
|
} else {
|
||||||
|
const error = await directResponse.json();
|
||||||
|
console.error('❌ Direct API failed:', error);
|
||||||
this.showMessage('❌ Ошибка отправки в Telegram', 'error');
|
this.showMessage('❌ Ошибка отправки в Telegram', 'error');
|
||||||
}
|
}
|
||||||
|
} catch (directError) {
|
||||||
|
console.error('❌ Direct API request failed:', directError);
|
||||||
|
this.showMessage('❌ Ошибка соединения с ботом', 'error');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// For testing in browser - download file
|
// For testing in browser - download file
|
||||||
const link = document.createElement('a');
|
const link = document.createElement('a');
|
||||||
|
|||||||
@@ -189,40 +189,36 @@ app.post('/api/convert', async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fallback API for Telegram notifications when WebApp data doesn't work
|
// Direct Telegram API for sending audio when WebApp fails
|
||||||
app.post('/api/telegram-notify', async (req: Request, res: Response) => {
|
app.post('/api/telegram-send', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { userId, audioUrl, title }: { userId?: string; audioUrl?: string; title?: string } = req.body;
|
const { userId, audioUrl, title }: { userId?: string; audioUrl?: string; title?: string } = req.body;
|
||||||
console.log('📡 Fallback Telegram notification received:', { userId, audioUrl, title });
|
console.log('🔄 Direct Telegram send request:', { userId, audioUrl, title });
|
||||||
|
|
||||||
if (!userId || !audioUrl || !title) {
|
if (!userId || !audioUrl || !title) {
|
||||||
return res.status(400).json({ error: 'Missing required fields' });
|
return res.status(400).json({ error: 'Missing required fields' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find bot instance and send the audio
|
// Get bot instance
|
||||||
// Note: We need to get the bot instance from somewhere
|
const botInstance = (global as any).quixoticBot;
|
||||||
// For now, we'll store a reference to it
|
if (!botInstance) {
|
||||||
if ((global as any).quixoticBot) {
|
console.log('❌ No bot instance available');
|
||||||
console.log('🤖 Using global bot instance for fallback notification');
|
return res.status(500).json({ error: 'Bot not available' });
|
||||||
const bot = (global as any).quixoticBot;
|
|
||||||
|
|
||||||
// Get user's chat ID from database
|
|
||||||
const user = await db.getUserByTelegramId(userId);
|
|
||||||
if (user) {
|
|
||||||
// We need to get chat ID - for now use user's telegram ID as chat ID
|
|
||||||
await bot.sendAudioFile(parseInt(userId), audioUrl, title);
|
|
||||||
console.log('✅ Fallback notification sent successfully');
|
|
||||||
} else {
|
|
||||||
console.log('❌ User not found for fallback notification');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('❌ No bot instance available for fallback');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ success: true });
|
console.log('🤖 Using bot instance for direct send');
|
||||||
} catch (error) {
|
|
||||||
console.error('Fallback notification error:', error);
|
// Use userId as chatId for private chats (this is how Telegram works)
|
||||||
res.status(500).json({ error: 'Fallback failed' });
|
const chatId = parseInt(userId);
|
||||||
|
console.log(`📤 Sending audio to chat ${chatId}`);
|
||||||
|
|
||||||
|
await botInstance.sendAudioFile(chatId, audioUrl, title);
|
||||||
|
console.log('✅ Audio sent successfully via direct API');
|
||||||
|
|
||||||
|
res.json({ success: true, message: 'Audio sent successfully' });
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('❌ Direct send error:', error.message);
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user