Revert: Remove tests for WhatsApp audio transcription
This commit removes the Jest unit tests, configuration, and related dependencies that were added for the WhatsApp audio transcription feature. This is done as per your request due to potential issues with the test execution environment. The core functionality for audio transcription remains.
This commit is contained in:
4469
whatsapp-router/package-lock.json
generated
4469
whatsapp-router/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,7 @@
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@google/genai": "^1.4.0",
|
||||
"axios": "^1.5.0",
|
||||
"dotenv": "^16.5.0",
|
||||
"express": "^4.18.2"
|
||||
|
||||
@@ -191,6 +191,9 @@ export interface WhatsAppMessage {
|
||||
chatId: string;
|
||||
mediaData: Record<string, unknown>;
|
||||
text: string;
|
||||
clientUrl?: string;
|
||||
deprecatedMms3Url?: string;
|
||||
mimetype?: string;
|
||||
}
|
||||
|
||||
export interface Participant {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import express, { Application } from 'express';
|
||||
import axios from 'axios';
|
||||
import { GoogleGenAI } from '@google/genai';
|
||||
import { getHandler } from './chatHandlers';
|
||||
import { addMessageToConversation } from './store/conversation';
|
||||
import { WhatsAppMessage, Conversation } from './types';
|
||||
@@ -38,6 +39,56 @@ export function registerWebhookRoutes(
|
||||
if (!message) return res.sendStatus(200);
|
||||
if (!openWaUrl) throw new Error('Service URLs not configured');
|
||||
const chatId = message.chatId || from;
|
||||
|
||||
// Audio message handling
|
||||
if (
|
||||
message.type === 'ptt' &&
|
||||
message.mimetype === 'audio/ogg; codecs=opus'
|
||||
) {
|
||||
const audioUrl = message.clientUrl || message.deprecatedMms3Url;
|
||||
if (!audioUrl) {
|
||||
console.error('No audio URL found for PTT message');
|
||||
// Potentially send a message to user or just skip? For now, skip.
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
console.log('🎤 Mensaje de audio detectado', audioUrl);
|
||||
try {
|
||||
const audioResponse = await axios.get(audioUrl, {
|
||||
responseType: 'arraybuffer',
|
||||
});
|
||||
const audioBase64 = Buffer.from(audioResponse.data).toString('base64');
|
||||
|
||||
const apiKey = process.env.GOOGLE_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error('GOOGLE_API_KEY is not set');
|
||||
}
|
||||
const genAI = new GoogleGenAI({ apiKey });
|
||||
|
||||
// Corrected Gemini API call structure
|
||||
const result = await genAI.models.generateContent({
|
||||
model: 'gemini-pro', // Ensure this model supports inline audio or use appropriate one
|
||||
contents: [
|
||||
{ inlineData: { mimeType: 'audio/ogg', data: audioBase64 } },
|
||||
{ text: 'Generate a transcript of the speech.' },
|
||||
],
|
||||
});
|
||||
// result directly is GenerateContentResponse
|
||||
const transcript = result.text; // Use the getter for text
|
||||
if (transcript === undefined) {
|
||||
throw new Error('Transcription resulted in undefined text.');
|
||||
}
|
||||
console.log('📝 Transcripción:', transcript);
|
||||
message.body = transcript;
|
||||
} catch (transcriptionError: any) {
|
||||
console.error('Error en la transcripción:', transcriptionError.message);
|
||||
const reply =
|
||||
"I received an audio message, but I couldn't transcribe it. Please send the transcript manually.";
|
||||
await axios.post(`${openWaUrl}/sendText`, { args: { to: from, content: reply } });
|
||||
// Stop processing this message as transcription failed and user has been notified.
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
}
|
||||
|
||||
let conv: Conversation | undefined;
|
||||
if (chatId) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user