From c63da9cd6ed11c7ec8a231e58a109841210fb82e Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 13 Jun 2023 17:23:52 -0300 Subject: [PATCH] feat: added option to generate qrcode as soon as the instance is created --- CHANGELOG.md | 1 + docker-compose.yaml | 27 ++++++++++++++----- src/validate/validate.schema.ts | 1 + .../controllers/instance.controller.ts | 24 +++++++++++++++-- src/whatsapp/dto/instance.dto.ts | 1 + 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f226aac..b6819213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Route to accept invite code * Added configuration of events by webhook of instances * Now the api key can be exposed in fetch instances if the EXPOSE_IN_FETCH_INSTANCES variable is set to true +* Added option to generate qrcode as soon as the instance is created ### Fixed diff --git a/docker-compose.yaml b/docker-compose.yaml index b85ddae1..1d6e5135 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -11,7 +11,7 @@ services: ports: - 8080:8080 volumes: - - ./docker-data/instances:/evolution/instances + - evolution_instances:/evolution/instances depends_on: - mongodb - redis @@ -77,7 +77,7 @@ services: # OBS: This key must be inserted in the request header to create an instance. - AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976 # Expose the api key on return from fetch instances - - AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=false + - AUTHENTICATION_EXPOSE_IN_FETCH_INSTANCES=true # Set the secret key to encrypt and decrypt your token and its expiration time. - AUTHENTICATION_JWT_EXPIRIN_IN=0 # seconds - 3600s === 1h | zero (0) - never expires # Set the instance name and webhook url to create an instance in init the application @@ -98,8 +98,8 @@ services: image: mongo restart: always volumes: - - ./docker-data/mongodb/data:/data/db - - ./docker-data/mongodb/configdb:/data/configdb + - evolution_mongodb_data:/data/db + - evolution_mongodb_configdb:/data/configdb ports: - 27017:27017 environment: @@ -112,10 +112,25 @@ services: redis: image: redis:latest + command: > + redis-server + --port 6379 + --appendonly yes + --save 900 1 + --save 300 10 + --save 60 10000 + --appendfsync everysec volumes: - - ./docker-data/redis:/data + - evolution_redis:/data container_name: redis ports: - 6379:6379 networks: - - evolution-net \ No newline at end of file + - evolution-net + + +volumes: + evolution_instances: + evolution_mongodb_data: + evolution_mongodb_configdb: + evolution_redis: \ No newline at end of file diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 408fa32e..3e6c7b04 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -55,6 +55,7 @@ export const instanceNameSchema: JSONSchema7 = { ], }, }, + qrcode: { type: 'boolean', enum: [true, false] }, }, ...isNotEmpty('instanceName'), }; diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index d6d12a04..b9d9bfe3 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -9,6 +9,7 @@ import { WAMonitoringService } from '../services/monitor.service'; import { WAStartupService } from '../services/whatsapp.service'; import { WebhookService } from '../services/webhook.service'; import { Logger } from '../../config/logger.config'; +import { wa } from '../types/wa.types'; export class InstanceController { constructor( @@ -22,7 +23,7 @@ export class InstanceController { private readonly logger = new Logger(InstanceController.name); - public async createInstance({ instanceName, webhook, events }: InstanceDto) { + public async createInstance({ instanceName, webhook, events, qrcode }: InstanceDto) { const mode = this.configService.get('AUTHENTICATION').INSTANCE.MODE; if (mode === 'container') { @@ -46,9 +47,13 @@ export class InstanceController { instanceName: instance.instanceName, }); + let getEvents: string[]; + if (webhook) { try { this.webhookService.create(instance, { enabled: true, url: webhook, events }); + + getEvents = (await this.webhookService.find(instance)).events; } catch (error) { this.logger.log(error); } @@ -61,6 +66,7 @@ export class InstanceController { }, hash, webhook, + events: getEvents, }; } else { const instance = new WAStartupService( @@ -76,14 +82,26 @@ export class InstanceController { instanceName: instance.instanceName, }); + let getEvents: string[]; + if (webhook) { try { - this.webhookService.create(instance, { enabled: true, url: webhook }); + this.webhookService.create(instance, { enabled: true, url: webhook, events }); + + getEvents = (await this.webhookService.find(instance)).events; } catch (error) { this.logger.log(error); } } + let getQrcode: wa.QrCode; + + if (qrcode) { + await instance.connectToWhatsapp(); + await delay(2000); + getQrcode = instance.qrCode; + } + return { instance: { instanceName: instance.instanceName, @@ -91,6 +109,8 @@ export class InstanceController { }, hash, webhook, + events: getEvents, + qrcode: getQrcode, }; } } diff --git a/src/whatsapp/dto/instance.dto.ts b/src/whatsapp/dto/instance.dto.ts index 90d1f9d3..99af100b 100644 --- a/src/whatsapp/dto/instance.dto.ts +++ b/src/whatsapp/dto/instance.dto.ts @@ -2,4 +2,5 @@ export class InstanceDto { instanceName: string; webhook?: string; events?: string[]; + qrcode?: boolean; }