fixes?
This commit is contained in:
69
src/bot.ts
69
src/bot.ts
@@ -62,6 +62,19 @@ export class QuixoticBot {
|
||||
}
|
||||
|
||||
private setupHandlers(): void {
|
||||
console.log('🔧 Setting up bot handlers...');
|
||||
|
||||
// Log all incoming messages for debugging
|
||||
this.bot.on('message', (msg: any) => {
|
||||
console.log('📨 Received message type:', msg.web_app ? 'web_app_data' : 'text', 'from user:', msg.from?.id);
|
||||
|
||||
// Handle web app data in regular message event
|
||||
if (msg.web_app?.data) {
|
||||
console.log('🔍 Web app data found in message:', msg.web_app.data);
|
||||
this.handleWebAppData(msg);
|
||||
}
|
||||
});
|
||||
|
||||
// Start command
|
||||
this.bot.onText(/\/start/, async (msg: Message) => {
|
||||
const chatId = msg.chat.id;
|
||||
@@ -155,33 +168,10 @@ export class QuixoticBot {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle web app data
|
||||
// Handle web app data (legacy event listener, may not work)
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
const data: WebAppData = JSON.parse(msg.web_app.data);
|
||||
console.log('📝 Parsed data:', data);
|
||||
|
||||
if (data.action === 'send_audio') {
|
||||
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 (parseError: any) {
|
||||
console.error('Web app data parse error:', parseError.message);
|
||||
console.error('Raw data:', msg.web_app?.data);
|
||||
await this.bot.sendMessage(chatId, '❌ Ошибка обработки данных.');
|
||||
}
|
||||
console.log('🔍 Web app data received via web_app_data event:', msg.web_app?.data);
|
||||
this.handleWebAppData(msg);
|
||||
});
|
||||
|
||||
// Handle inline queries for search
|
||||
@@ -294,6 +284,33 @@ export class QuixoticBot {
|
||||
}
|
||||
}
|
||||
|
||||
private async handleWebAppData(msg: Message): Promise<void> {
|
||||
const chatId = msg.chat.id;
|
||||
const userId = msg.from?.id;
|
||||
|
||||
if (!msg.web_app?.data) {
|
||||
console.log('❌ No web app data found');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const data: WebAppData = JSON.parse(msg.web_app.data);
|
||||
console.log('📝 Parsed data:', data);
|
||||
|
||||
if (data.action === 'send_audio') {
|
||||
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 (parseError: any) {
|
||||
console.error('Web app data parse error:', parseError.message);
|
||||
console.error('Raw data:', msg.web_app?.data);
|
||||
await this.bot.sendMessage(chatId, '❌ Ошибка обработки данных.');
|
||||
}
|
||||
}
|
||||
|
||||
private formatDuration(seconds: number): string {
|
||||
if (!seconds) return '';
|
||||
const mins = Math.floor(seconds / 60);
|
||||
|
||||
Reference in New Issue
Block a user