Skip to content

Commit f9567fb

Browse files
committed
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.
1 parent d673c83 commit f9567fb

File tree

17 files changed

+802
-4122
lines changed

17 files changed

+802
-4122
lines changed

src/api/integrations/chatbot/base-chatbot.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface BaseBotData {
3131
enabled?: boolean;
3232
description: string;
3333
expire?: number;
34-
keywordFinish?: string[];
34+
keywordFinish?: string;
3535
delayMessage?: number;
3636
unknownMessage?: string;
3737
listeningFromMe?: boolean;
@@ -792,7 +792,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
792792
const content = getConversationMessage(msg);
793793

794794
// Get integration type
795-
const integrationType = this.getIntegrationType();
795+
// const integrationType = this.getIntegrationType();
796796

797797
// Find a bot for this message
798798
let findBot: any = await this.findBotTrigger(this.botRepository, content, instance, session);

src/api/integrations/chatbot/base-chatbot.dto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class BaseChatbotDto {
88
enabled?: boolean;
99
description: string;
1010
expire?: number;
11-
keywordFinish?: string[];
11+
keywordFinish?: string;
1212
delayMessage?: number;
1313
unknownMessage?: string;
1414
listeningFromMe?: boolean;
@@ -28,7 +28,7 @@ export class BaseChatbotDto {
2828
*/
2929
export class BaseChatbotSettingDto {
3030
expire?: number;
31-
keywordFinish?: string[];
31+
keywordFinish?: string;
3232
delayMessage?: number;
3333
unknownMessage?: string;
3434
listeningFromMe?: boolean;

src/api/integrations/chatbot/base-chatbot.service.ts

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
194194

195195
// Forward the message to the chatbot API
196196
await this.sendMessageToBot(instance, session, settings, bot, remoteJid, pushName || '', content, msg);
197+
198+
// Update session to indicate we're waiting for user response
199+
await this.prismaRepository.integrationSession.update({
200+
where: {
201+
id: session.id,
202+
},
203+
data: {
204+
status: 'opened',
205+
awaitUser: true,
206+
},
207+
});
197208
} catch (error) {
198209
this.logger.error(`Error in process: ${error}`);
199210
return;
@@ -218,12 +229,9 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
218229
let match: RegExpExecArray | null;
219230

220231
const splitMessages = (settings as any)?.splitMessages ?? false;
221-
const timePerChar = (settings as any)?.timePerChar ?? 0;
222-
const minDelay = 1000;
223-
const maxDelay = 20000;
224232

225233
while ((match = linkRegex.exec(message)) !== null) {
226-
const [fullMatch, exclamation, altText, url] = match;
234+
const [, , altText, url] = match;
227235
const mediaType = this.getMediaType(url);
228236
const beforeText = message.slice(lastIndex, match.index);
229237

@@ -240,38 +248,26 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
240248

241249
// Handle sending the media
242250
try {
243-
switch (mediaType) {
244-
case 'image':
245-
await instance.mediaMessage({
251+
if (mediaType === 'audio') {
252+
await instance.audioWhatsapp({
253+
number: remoteJid.split('@')[0],
254+
delay: (settings as any)?.delayMessage || 1000,
255+
audio: url,
256+
caption: altText,
257+
});
258+
} else {
259+
await instance.mediaMessage(
260+
{
246261
number: remoteJid.split('@')[0],
247262
delay: (settings as any)?.delayMessage || 1000,
248-
caption: altText,
263+
mediatype: mediaType,
249264
media: url,
250-
});
251-
break;
252-
case 'video':
253-
await instance.mediaMessage({
254-
number: remoteJid.split('@')[0],
255-
delay: (settings as any)?.delayMessage || 1000,
256265
caption: altText,
257-
media: url,
258-
});
259-
break;
260-
case 'document':
261-
await instance.documentMessage({
262-
number: remoteJid.split('@')[0],
263-
delay: (settings as any)?.delayMessage || 1000,
264-
fileName: altText || 'document',
265-
media: url,
266-
});
267-
break;
268-
case 'audio':
269-
await instance.audioMessage({
270-
number: remoteJid.split('@')[0],
271-
delay: (settings as any)?.delayMessage || 1000,
272-
media: url,
273-
});
274-
break;
266+
fileName: mediaType === 'document' ? altText || 'document' : undefined,
267+
},
268+
null,
269+
false,
270+
);
275271
}
276272
} catch (error) {
277273
this.logger.error(`Error sending media: ${error}`);
@@ -283,12 +279,15 @@ export abstract class BaseChatbotService<BotType = any, SettingsType = any> {
283279
textBuffer += `[${altText}](${url})`;
284280
}
285281

286-
lastIndex = match.index + fullMatch.length;
282+
lastIndex = linkRegex.lastIndex;
287283
}
288284

289285
// Add any remaining text after the last match
290286
if (lastIndex < message.length) {
291-
textBuffer += message.slice(lastIndex);
287+
const remainingText = message.slice(lastIndex);
288+
if (remainingText.trim()) {
289+
textBuffer += remainingText;
290+
}
292291
}
293292

294293
// Send any remaining text

src/api/integrations/chatbot/evoai/controllers/evoai.controller.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IgnoreJidDto } from '@api/dto/chatbot.dto';
21
import { InstanceDto } from '@api/dto/instance.dto';
32
import { EvoaiDto } from '@api/integrations/chatbot/evoai/dto/evoai.dto';
43
import { EvoaiService } from '@api/integrations/chatbot/evoai/services/evoai.service';
@@ -9,7 +8,7 @@ import { Logger } from '@config/logger.config';
98
import { BadRequestException } from '@exceptions';
109
import { Evoai as EvoaiModel, IntegrationSession } from '@prisma/client';
1110

12-
import { BaseChatbotController, ChatbotSettings } from '../../base-chatbot.controller';
11+
import { BaseChatbotController } from '../../base-chatbot.controller';
1312

1413
export class EvoaiController extends BaseChatbotController<EvoaiModel, EvoaiDto> {
1514
constructor(
@@ -34,7 +33,7 @@ export class EvoaiController extends BaseChatbotController<EvoaiModel, EvoaiDto>
3433
userMessageDebounce: { [key: string]: { message: string; timeoutId: NodeJS.Timeout } } = {};
3534

3635
protected getFallbackBotId(settings: any): string | undefined {
37-
return settings?.fallbackId;
36+
return settings?.evoaiIdFallback;
3837
}
3938

4039
protected getFallbackFieldName(): string {
@@ -168,6 +167,6 @@ export class EvoaiController extends BaseChatbotController<EvoaiModel, EvoaiDto>
168167
pushName?: string,
169168
msg?: any,
170169
) {
171-
this.evoaiService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
170+
await this.evoaiService.process(instance, remoteJid, bot, session, settings, content, pushName, msg);
172171
}
173172
}

0 commit comments

Comments
 (0)