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_ORIGIN='*' # Or separate by commas - ex.: 'yourdomain1.com, yourdomain2.com'
CORS_METHODS='POST,GET,PUT,DELETE' CORS_METHODS='POST,GET,PUT,DELETE'
CORS_CREDENTIALS=true CORS_CREDENTIALS=true

View File

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

View File

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

View File

@ -117,6 +117,87 @@ export class ChatwootService {
return contact; 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( public async createContact(
instance: InstanceDto, instance: InstanceDto,
phoneNumber: string, phoneNumber: string,

View File

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