diff --git a/Docker/.env.example b/Docker/.env.example index e2b99a1e..f4e665aa 100644 --- a/Docker/.env.example +++ b/Docker/.env.example @@ -1,3 +1,5 @@ +SERVER_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 diff --git a/package.json b/package.json index 45d727f0..c0e8c825 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index b04933db..b08abc78 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -79,30 +79,30 @@ export class InstanceController { 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 ( !chatwoot_account_id || !chatwoot_token || !chatwoot_url || !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: { @@ -141,6 +141,8 @@ export class InstanceController { throw new BadRequestException('sign_msg is required'); } + const urlServer = this.configService.get('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('SERVER').URL; - return { instance: { instanceName: instance.instanceName, @@ -202,30 +209,30 @@ export class InstanceController { 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 ( !chatwoot_account_id || !chatwoot_token || !chatwoot_url || !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; if (qrcode) { @@ -277,6 +284,8 @@ export class InstanceController { throw new BadRequestException('sign_msg is required'); } + const urlServer = this.configService.get('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('SERVER').URL; - return { instance: { instanceName: instance.instanceName, status: 'created', }, hash, + webhook, + webhook_by_events, + events: getEvents, chatwoot: { enabled: true, account_id: chatwoot_account_id, diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 24d078a2..0f76e506 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -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, diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 60058254..c07e9f32 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -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, }, }); }