feat: automation chatwoot

This commit is contained in:
Davidson Gomes 2023-07-13 15:39:40 -03:00
parent 3e13ae9740
commit 8622e1e4ff
5 changed files with 141 additions and 43 deletions

View File

@ -1,3 +1,5 @@
SERVER_URL='<url>' # ex.: http://localhost:3333
CORS_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
CORS_METHODS='POST,GET,PUT,DELETE'
CORS_CREDENTIALS=true

View File

@ -42,9 +42,9 @@
"dependencies": {
"@adiwajshing/keyed-db": "^0.2.4",
"@ffmpeg-installer/ffmpeg": "^1.1.0",
"@figuro/chatwoot-sdk": "^1.1.14",
"@hapi/boom": "^10.0.1",
"@whiskeysockets/baileys": "github:vphelipe/WhiskeySockets-Baileys#master",
"@figuro/chatwoot-sdk": "^1.1.14",
"axios": "^1.3.5",
"class-validator": "^0.13.2",
"compression": "^1.7.4",

View File

@ -79,12 +79,6 @@ export class InstanceController {
this.logger.verbose('hash: ' + hash + ' generated');
if (
!chatwoot_account_id ||
!chatwoot_token ||
!chatwoot_url ||
!chatwoot_sign_msg
) {
let getEvents: string[];
if (webhook) {
@ -103,6 +97,12 @@ export class InstanceController {
}
}
if (
!chatwoot_account_id ||
!chatwoot_token ||
!chatwoot_url ||
!chatwoot_sign_msg
) {
this.logger.verbose('instance created');
this.logger.verbose({
instance: {
@ -141,6 +141,8 @@ export class InstanceController {
throw new BadRequestException('sign_msg is required');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
@ -150,12 +152,17 @@ export class InstanceController {
sign_msg: chatwoot_sign_msg,
name_inbox: instance.instanceName,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
);
} catch (error) {
this.logger.log(error);
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
return {
instance: {
instanceName: instance.instanceName,
@ -202,12 +209,6 @@ export class InstanceController {
this.logger.verbose('hash: ' + hash + ' generated');
if (
!chatwoot_account_id ||
!chatwoot_token ||
!chatwoot_url ||
!chatwoot_sign_msg
) {
let getEvents: string[];
if (webhook) {
@ -226,6 +227,12 @@ export class InstanceController {
}
}
if (
!chatwoot_account_id ||
!chatwoot_token ||
!chatwoot_url ||
!chatwoot_sign_msg
) {
let getQrcode: wa.QrCode;
if (qrcode) {
@ -277,6 +284,8 @@ export class InstanceController {
throw new BadRequestException('sign_msg is required');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
@ -286,18 +295,26 @@ export class InstanceController {
sign_msg: chatwoot_sign_msg,
name_inbox: instance.instanceName,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
);
} catch (error) {
this.logger.log(error);
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
return {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events,
events: getEvents,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,

View File

@ -117,6 +117,87 @@ export class ChatwootService {
return contact;
}
public async initInstanceChatwoot(
instance: InstanceDto,
inboxName: string,
webhookUrl: string,
qrcode: boolean,
) {
const client = await this.clientCw(instance);
if (!client) {
throw new Error('client not found');
}
const findInbox: any = await client.inboxes.list({
accountId: this.provider.account_id,
});
const checkDuplicate = findInbox.payload
.map((inbox) => inbox.name)
.includes(inboxName);
let inboxId: number;
if (!checkDuplicate) {
const data = {
type: 'api',
webhook_url: webhookUrl,
};
const inbox = await client.inboxes.create({
accountId: this.provider.account_id,
data: {
name: inboxName,
channel: data as any,
},
});
if (!inbox) {
return null;
}
inboxId = inbox.id;
} else {
const inbox = findInbox.payload.find((inbox) => inbox.name === inboxName);
inboxId = inbox.id;
}
const contact =
(await this.findContact(instance, '123456')) ||
((await this.createContact(
instance,
'123456',
inboxId,
false,
'EvolutionAPI',
)) as any);
const contactId = contact.id || contact.payload.contact.id;
if (qrcode) {
const conversation = await client.conversations.create({
accountId: this.provider.account_id,
data: {
contact_id: contactId.toString(),
inbox_id: inboxId.toString(),
},
});
await client.messages.create({
accountId: this.provider.account_id,
conversationId: conversation.id,
data: {
content: '/iniciar',
message_type: 'outgoing',
},
});
}
return true;
}
public async createContact(
instance: InstanceDto,
phoneNumber: string,

View File

@ -129,7 +129,6 @@ export class WAMonitoringService {
profileName: (await value.getProfileName()) || 'not loaded',
profilePictureUrl: value.profilePictureUrl,
status: (await value.getProfileStatus()) || '',
chatwoot,
},
});
}
@ -161,7 +160,6 @@ export class WAMonitoringService {
instance: {
instanceName: key,
status: value.connectionStatus.state,
chatwoot,
},
});
}