Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/api/integrations/chatbot/base-chatbot.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IgnoreJidDto } from '@api/dto/chatbot.dto';

Check failure on line 1 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Run autofix to sort these imports!
import { InstanceDto } from '@api/dto/instance.dto';
import { PrismaRepository } from '@api/repository/repository.service';
import { WAMonitoringService } from '@api/services/monitor.service';
Expand All @@ -9,6 +9,7 @@

import { BaseChatbotDto } from './base-chatbot.dto';
import { ChatbotController, ChatbotControllerInterface, EmitData } from './chatbot.controller';
import { Events } from '@api/types/wa.types';

// Common settings interface for all chatbot integrations
export interface ChatbotSettings {
Expand Down Expand Up @@ -58,7 +59,7 @@
settingsRepository: any;
sessionRepository: any;
userMessageDebounce: { [key: string]: { message: string; timeoutId: NodeJS.Timeout } } = {};

Check failure on line 62 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `··`
// Name of the integration, to be set by the derived class
protected abstract readonly integrationName: string;

Expand Down Expand Up @@ -445,7 +446,17 @@
});

const remoteJid = data.remoteJid;
const status = data.status;
const status = data.status;

Check failure on line 449 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `······`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Prefer object destructuring when accessing and using properties. (use-object-destructuring)

Suggested change
const status = data.status;
const {status} = data;


ExplanationObject destructuring can often remove an unnecessary temporary reference, as well as making your code more succinct.

From the Airbnb Javascript Style Guide

const session = await this.getSession(remoteJid, instance);

if (this.integrationName === 'Typebot') {
const typebotData = {
remoteJid: remoteJid,
status: status,
session

Check failure on line 456 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `,`
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
}

if (status === 'delete') {
await this.sessionRepository.deleteMany({
Expand Down Expand Up @@ -496,7 +507,7 @@
status: status,
session,
};

Check failure on line 510 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `········`
return { bot: { ...instance, bot: botData } };
}
} catch (error) {
Expand Down Expand Up @@ -867,6 +878,16 @@
status: 'paused',
},
});

if (this.integrationName === 'Typebot') {
const typebotData = {
remoteJid: remoteJid,
status: 'paused',
session,
};
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
}

Check failure on line 890 in src/api/integrations/chatbot/base-chatbot.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `········`
return;
}

Expand Down
55 changes: 52 additions & 3 deletions src/api/integrations/chatbot/typebot/services/typebot.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaRepository } from '@api/repository/repository.service';

Check failure on line 1 in src/api/integrations/chatbot/typebot/services/typebot.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Run autofix to sort these imports!
import { WAMonitoringService } from '@api/services/monitor.service';
import { Auth, ConfigService, HttpServer, Typebot } from '@config/env.config';
import { Instance, IntegrationSession, Message, Typebot as TypebotModel } from '@prisma/client';
Expand All @@ -8,18 +8,19 @@

import { BaseChatbotService } from '../../base-chatbot.service';
import { OpenaiService } from '../../openai/services/openai.service';
import { Events } from '@api/types/wa.types';

export class TypebotService extends BaseChatbotService<TypebotModel, any> {
private openaiService: OpenaiService;

private openaiService: OpenaiService;

Check failure on line 14 in src/api/integrations/chatbot/typebot/services/typebot.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `··`

Check failure on line 15 in src/api/integrations/chatbot/typebot/services/typebot.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `··`
constructor(
waMonitor: WAMonitoringService,
configService: ConfigService,
prismaRepository: PrismaRepository,
openaiService: OpenaiService,
) {
super(waMonitor, prismaRepository, 'TypebotService', configService);
this.openaiService = openaiService;
this.openaiService = openaiService;

Check failure on line 23 in src/api/integrations/chatbot/typebot/services/typebot.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `······`
}

/**
Expand Down Expand Up @@ -151,6 +152,14 @@
},
});
}

const typebotData = {
remoteJid: data.remoteJid,
status: 'opened',
session,
};
this.waMonitor.waInstances[instance.name].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);

return { ...request.data, session };
} catch (error) {
this.logger.error(error);
Expand Down Expand Up @@ -399,12 +408,14 @@
},
});
} else {
let statusChange = 'closed';
if (!settings?.keepOpen) {
await prismaRepository.integrationSession.deleteMany({
where: {
id: session.id,
},
});
statusChange = 'delete';
} else {
await prismaRepository.integrationSession.update({
where: {
Expand All @@ -415,6 +426,14 @@
},
});
}

const typebotData = {
remoteJid: session.remoteJid,
status: statusChange,
session,
};
instance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);

}
}

Expand Down Expand Up @@ -639,6 +658,7 @@
}

if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) {
await this.prismaRepository.integrationSession.update({
where: {
Expand All @@ -649,13 +669,22 @@
},
});
} else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({
where: {
botId: findTypebot.id,
remoteJid: remoteJid,
},
});
}

const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);

return;
}

Expand Down Expand Up @@ -788,6 +817,7 @@
}

if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) {
await this.prismaRepository.integrationSession.update({
where: {
Expand All @@ -798,13 +828,21 @@
},
});
} else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({
where: {
botId: findTypebot.id,
remoteJid: remoteJid,
},
});
}

const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);

return;
}
Expand Down Expand Up @@ -881,6 +919,7 @@
}

if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
let statusChange = 'closed';
if (keepOpen) {
await this.prismaRepository.integrationSession.update({
where: {
Expand All @@ -891,13 +930,23 @@
},
});
} else {
statusChange = 'delete';
await this.prismaRepository.integrationSession.deleteMany({
where: {
botId: findTypebot.id,
remoteJid: remoteJid,
},
});
}

const typebotData = {
remoteJid: remoteJid,
status: statusChange,
session,
};

waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);

return;
}

Expand Down