refactor(chatbot): unify keywordFinish type and enhance session handling

- Changed the type of `keywordFinish` from an array to a string in multiple DTOs and controller interfaces to simplify data handling.
- Updated the `BaseChatbotService` to include logic for updating session status to 'opened' and managing user responses more effectively.
- Refactored the media message handling in the `BaseChatbotService` to streamline the process and improve readability.
- Enhanced error logging across various services to ensure better traceability during operations.

This commit focuses on improving the structure and consistency of chatbot integrations while ensuring that session management is robust and user-friendly.
This commit is contained in:
Davidson Gomes
2025-05-21 17:02:24 -03:00
parent d673c83a93
commit f9567fbeaa
17 changed files with 819 additions and 4139 deletions

View File

@@ -1,7 +1,7 @@
import { InstanceDto } from '@api/dto/instance.dto';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
import { ConfigService } from '@config/env.config';
import { Auth, ConfigService, HttpServer } from '@config/env.config';
import { IntegrationSession, N8n, N8nSetting } from '@prisma/client';
import { sendTelemetry } from '@utils/sendTelemetry';
import axios from 'axios';
@@ -104,26 +104,6 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
}
}
/**
* Send a message to the N8n bot webhook.
*/
public async sendMessage(n8nId: string, chatInput: string, sessionId: string): Promise<string> {
try {
const bot = await this.prismaRepository.n8n.findFirst({ where: { id: n8nId, enabled: true } });
if (!bot) throw new Error('N8n bot not found or not enabled');
const headers: Record<string, string> = {};
if (bot.basicAuthUser && bot.basicAuthPass) {
const auth = Buffer.from(`${bot.basicAuthUser}:${bot.basicAuthPass}`).toString('base64');
headers['Authorization'] = `Basic ${auth}`;
}
const response = await axios.post(bot.webhookUrl, { chatInput, sessionId }, { headers });
return response.data.output;
} catch (error) {
this.logger.error(error);
throw new Error('Error sending message to n8n bot');
}
}
public async createNewSession(instance: InstanceDto, data: any) {
return super.createNewSession(instance, data, 'n8n');
}
@@ -143,6 +123,12 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
const payload: any = {
chatInput: content,
sessionId: session.sessionId,
remoteJid: remoteJid,
pushName: pushName,
fromMe: msg?.key?.fromMe,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: this.configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
};
// Handle audio messages