ya se puede transcribir mensajes y recibirlos en el agent
This commit is contained in:
@@ -4,6 +4,7 @@ import { GoogleGenAI } from '@google/genai';
|
||||
import { getHandler } from './chatHandlers';
|
||||
import { addMessageToConversation } from './store/conversation';
|
||||
import { WhatsAppMessage, Conversation } from './types';
|
||||
import { transcribeAudioMessage } from './transcribeAudioMessage';
|
||||
|
||||
export interface WebhookConfig {
|
||||
API_URL: string;
|
||||
@@ -32,6 +33,12 @@ export function registerWebhookRoutes(
|
||||
|
||||
if (message) {
|
||||
const origen = from || message.chatId || 'desconocido';
|
||||
|
||||
if(origen == '50493849962@c.us') //si el mensajes es de un agente, no lo proceses
|
||||
{
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
|
||||
console.log(`📩 Mensaje recibido (${message.text}) de ${origen}`);
|
||||
}
|
||||
|
||||
@@ -41,6 +48,8 @@ export function registerWebhookRoutes(
|
||||
const chatId = message.chatId || from;
|
||||
|
||||
// Audio message handling
|
||||
// console.log(message);
|
||||
|
||||
if (
|
||||
message.type === 'ptt' &&
|
||||
message.mimetype === 'audio/ogg; codecs=opus'
|
||||
@@ -53,33 +62,12 @@ export function registerWebhookRoutes(
|
||||
}
|
||||
console.log('🎤 Mensaje de audio detectado', audioUrl);
|
||||
try {
|
||||
// Download audio using the /downloadFileWithCredentials endpoint
|
||||
const audioResponse = await axios.post(`${openWaUrl}/downloadFileWithCredentials`, {
|
||||
args: { url: audioUrl },
|
||||
});
|
||||
const audioBase64 = audioResponse.data; // This is already a base64 string
|
||||
|
||||
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.');
|
||||
}
|
||||
const transcript = await transcribeAudioMessage(message);
|
||||
console.log('📝 Transcripción:', transcript);
|
||||
message.body = transcript;
|
||||
message.body = transcript || '';
|
||||
message.text = transcript || '';
|
||||
|
||||
|
||||
} catch (transcriptionError: any) {
|
||||
console.error('Error en la transcripción:', transcriptionError.message);
|
||||
const reply =
|
||||
@@ -90,6 +78,7 @@ export function registerWebhookRoutes(
|
||||
}
|
||||
}
|
||||
|
||||
console.log(message);
|
||||
let conv: Conversation | undefined;
|
||||
if (chatId) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user