fix: Correct pause and listen to my messages from Typebot integration

This commit fixes an issue where the bot was not pausing and listening to my messages from the Typebot integration. The changes include modifying the Typebot service, Typebot schema, and WhatsApp Baileys and Business services to properly handle the pause and message listening functionality. The affected files are typebot.service.ts, typebot.schema.ts, and both WhatsApp services.

The specific modifications include adding a new 'delete' status option in the Typebot schema, updating the Typebot service to handle the 'delete' status, and adjusting the WhatsApp services to properly send Typebot messages based on the new schema. This ensures that the bot can pause and listen to messages from Typebot integration, providing a better user experience.
This commit is contained in:
Davidson Gomes 2024-07-29 17:53:18 -03:00
parent 21375bcb4d
commit 7ba1f2fdf4
4 changed files with 138 additions and 95 deletions

View File

@ -579,7 +579,13 @@ export class TypebotService {
const remoteJid = data.remoteJid;
const status = data.status;
if (status === 'closed') {
const defaultSettingCheck = await this.prismaRepository.typebotSetting.findFirst({
where: {
instanceId,
},
});
if (status === 'delete') {
await this.prismaRepository.typebotSession.deleteMany({
where: {
remoteJid: remoteJid,
@ -588,27 +594,50 @@ export class TypebotService {
});
return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } };
} else {
const session = await this.prismaRepository.typebotSession.updateMany({
where: {
instanceId: instanceId,
remoteJid: remoteJid,
},
data: {
status: status,
},
});
const typebotData = {
remoteJid: remoteJid,
status: status,
session,
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return { typebot: { ...instance, typebot: typebotData } };
}
if (status === 'closed') {
if (defaultSettingCheck?.keepOpen) {
await this.prismaRepository.typebotSession.updateMany({
where: {
instanceId: instanceId,
remoteJid: remoteJid,
},
data: {
status: status,
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
remoteJid: remoteJid,
instanceId: instanceId,
},
});
}
return { typebot: { ...instance, typebot: { remoteJid: remoteJid, status: status } } };
}
const session = await this.prismaRepository.typebotSession.updateMany({
where: {
instanceId: instanceId,
remoteJid: remoteJid,
},
data: {
status: status,
},
});
const typebotData = {
remoteJid: remoteJid,
status: status,
session,
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
return { typebot: { ...instance, typebot: typebotData } };
} catch (error) {
this.logger.error(error);
throw new Error('Error changing status');
@ -719,7 +748,6 @@ export class TypebotService {
}
if (startSession) {
console.log('startSession', startSession);
let findTypebot: any = await this.prismaRepository.typebot.findFirst({
where: {
url: url,
@ -728,8 +756,6 @@ export class TypebotService {
},
});
console.log('findTypebot', findTypebot);
if (!findTypebot) {
findTypebot = await this.prismaRepository.typebot.create({
data: {
@ -749,8 +775,6 @@ export class TypebotService {
});
}
console.log('findTypebot2', findTypebot);
await this.prismaRepository.typebotSession.deleteMany({
where: {
remoteJid: remoteJid,
@ -1500,17 +1524,28 @@ export class TypebotService {
participant: string;
};
if (!listeningFromMe && key.fromMe) {
if (stopBotFromMe && key.fromMe && session) {
if (keepOpen) {
await this.prismaRepository.typebotSession.update({
where: {
id: session.id,
},
data: {
status: 'closed',
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
}
return;
}
if (stopBotFromMe && listeningFromMe && key.fromMe && session) {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
if (!listeningFromMe && key.fromMe) {
return;
}
@ -1554,24 +1589,6 @@ export class TypebotService {
);
}
// await this.processTypebot(
// instance,
// remoteJid,
// msg,
// session,
// findTypebot,
// url,
// expire,
// typebot,
// keywordFinish,
// delayMessage,
// unknownMessage,
// listeningFromMe,
// stopBotFromMe,
// keepOpen,
// content,
// );
if (session && !session.awaitUser) return;
} catch (error) {
this.logger.error(error);
@ -1606,12 +1623,23 @@ export class TypebotService {
const diffInMinutes = Math.floor(diff / 1000 / 60);
if (diffInMinutes > expire) {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
if (keepOpen) {
await this.prismaRepository.typebotSession.update({
where: {
id: session.id,
},
data: {
status: 'closed',
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
}
const data = await this.createNewSession(instance, {
enabled: findTypebot.enabled,
@ -1669,12 +1697,23 @@ export class TypebotService {
}
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
if (keepOpen) {
await this.prismaRepository.typebotSession.update({
where: {
id: session.id,
},
data: {
status: 'closed',
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
}
return;
}
@ -1783,12 +1822,23 @@ export class TypebotService {
}
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
if (keepOpen) {
await this.prismaRepository.typebotSession.update({
where: {
id: session.id,
},
data: {
status: 'closed',
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
}
return;
}
@ -1864,12 +1914,23 @@ export class TypebotService {
}
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
if (keepOpen) {
await this.prismaRepository.typebotSession.update({
where: {
id: session.id,
},
data: {
status: 'closed',
},
});
} else {
await this.prismaRepository.typebotSession.deleteMany({
where: {
typebotId: findTypebot.id,
remoteJid: remoteJid,
},
});
}
return;
}

View File

@ -47,7 +47,7 @@ export const typebotStatusSchema: JSONSchema7 = {
type: 'object',
properties: {
remoteJid: { type: 'string' },
status: { type: 'string', enum: ['opened', 'closed', 'paused'] },
status: { type: 'string', enum: ['opened', 'closed', 'paused', 'delete'] },
},
required: ['remoteJid', 'status'],
...isNotEmpty('remoteJid', 'status'),

View File

@ -1951,15 +1951,6 @@ export class BaileysStartupService extends ChannelStartupService {
);
}
if (this.configService.get<Typebot>('TYPEBOT').ENABLED && !isIntegration) {
if (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw.key.remoteJid,
messageRaw,
);
}
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE)
await this.prismaRepository.message.create({
data: messageRaw,

View File

@ -850,15 +850,6 @@ export class BusinessStartupService extends ChannelStartupService {
);
}
if (this.configService.get<Typebot>('TYPEBOT').ENABLED && !isIntegration) {
if (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name, instanceId: this.instanceId },
messageRaw.key.remoteJid,
messageRaw,
);
}
await this.prismaRepository.message.create({
data: messageRaw,
});