fix: integrate Typebot status change events for webhook in chatbot controller and service

This commit is contained in:
Josias Maceda 2025-09-09 11:49:13 -03:00
parent c2085b59ea
commit 0116bc4c9f
2 changed files with 76 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import { getConversationMessage } from '@utils/getConversationMessage';
import { BaseChatbotDto } from './base-chatbot.dto'; import { BaseChatbotDto } from './base-chatbot.dto';
import { ChatbotController, ChatbotControllerInterface, EmitData } from './chatbot.controller'; import { ChatbotController, ChatbotControllerInterface, EmitData } from './chatbot.controller';
import { Events } from '@api/types/wa.types';
// Common settings interface for all chatbot integrations // Common settings interface for all chatbot integrations
export interface ChatbotSettings { export interface ChatbotSettings {
@ -446,6 +447,16 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
const remoteJid = data.remoteJid; const remoteJid = data.remoteJid;
const status = data.status; const status = data.status;
const session = await this.getSession(remoteJid, instance);
if (this.integrationName === 'Typebot') {
const typebotData = {
remoteJid: remoteJid,
status: status,
session
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
}
if (status === 'delete') { if (status === 'delete') {
await this.sessionRepository.deleteMany({ await this.sessionRepository.deleteMany({
@ -867,6 +878,16 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
status: 'paused', status: 'paused',
}, },
}); });
if (this.integrationName === 'Typebot') {
const typebotData = {
remoteJid: remoteJid,
status: 'paused',
session,
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
}
return; return;
} }

View File

@ -8,6 +8,7 @@ import axios from 'axios';
import { BaseChatbotService } from '../../base-chatbot.service'; import { BaseChatbotService } from '../../base-chatbot.service';
import { OpenaiService } from '../../openai/services/openai.service'; import { OpenaiService } from '../../openai/services/openai.service';
import { Events } from '@api/types/wa.types';
export class TypebotService extends BaseChatbotService<TypebotModel, any> { export class TypebotService extends BaseChatbotService<TypebotModel, any> {
private openaiService: OpenaiService; private openaiService: OpenaiService;
@ -151,6 +152,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} }
const typebotData = {
remoteJid: data.remoteJid,
status: 'opened',
session,
};
this.waMonitor.waInstances[instance.name].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return { ...request.data, session }; return { ...request.data, session };
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
@ -399,12 +408,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} else { } else {
let statusChange = 'closed';
if (!settings?.keepOpen) { if (!settings?.keepOpen) {
await prismaRepository.integrationSession.deleteMany({ await prismaRepository.integrationSession.deleteMany({
where: { where: {
id: session.id, id: session.id,
}, },
}); });
statusChange = 'delete';
} else { } else {
await prismaRepository.integrationSession.update({ await prismaRepository.integrationSession.update({
where: { where: {
@ -415,6 +426,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} }
const typebotData = {
remoteJid: session.remoteJid,
status: statusChange,
session,
};
instance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
} }
} }
@ -639,6 +658,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
} }
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.integrationSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
@ -649,6 +669,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} else { } else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
botId: findTypebot.id, botId: findTypebot.id,
@ -656,6 +677,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} }
const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return; return;
} }
@ -788,6 +817,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
} }
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.integrationSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
@ -798,6 +828,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} else { } else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
botId: findTypebot.id, botId: findTypebot.id,
@ -806,6 +837,13 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}); });
} }
const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return; return;
} }
@ -881,6 +919,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
} }
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) { if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) { if (keepOpen) {
await this.prismaRepository.integrationSession.update({ await this.prismaRepository.integrationSession.update({
where: { where: {
@ -891,6 +930,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} else { } else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({ await this.prismaRepository.integrationSession.deleteMany({
where: { where: {
botId: findTypebot.id, botId: findTypebot.id,
@ -898,6 +938,15 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
}, },
}); });
} }
const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return; return;
} }