feat: added option to generate qrcode as soon as the instance is created

This commit is contained in:
Davidson Gomes 2023-06-13 17:23:52 -03:00
parent 849d570bcb
commit c63da9cd6e
5 changed files with 46 additions and 8 deletions

View File

@ -12,6 +12,7 @@
* Route to accept invite code * Route to accept invite code
* Added configuration of events by webhook of instances * 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 * 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 ### Fixed

View File

@ -11,7 +11,7 @@ services:
ports: ports:
- 8080:8080 - 8080:8080
volumes: volumes:
- ./docker-data/instances:/evolution/instances - evolution_instances:/evolution/instances
depends_on: depends_on:
- mongodb - mongodb
- redis - redis
@ -77,7 +77,7 @@ services:
# OBS: This key must be inserted in the request header to create an instance. # OBS: This key must be inserted in the request header to create an instance.
- AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976 - AUTHENTICATION_API_KEY=B6D711FCDE4D4FD5936544120E713976
# Expose the api key on return from fetch instances # 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. # 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 - 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 # Set the instance name and webhook url to create an instance in init the application
@ -98,8 +98,8 @@ services:
image: mongo image: mongo
restart: always restart: always
volumes: volumes:
- ./docker-data/mongodb/data:/data/db - evolution_mongodb_data:/data/db
- ./docker-data/mongodb/configdb:/data/configdb - evolution_mongodb_configdb:/data/configdb
ports: ports:
- 27017:27017 - 27017:27017
environment: environment:
@ -112,10 +112,25 @@ services:
redis: redis:
image: redis:latest image: redis:latest
command: >
redis-server
--port 6379
--appendonly yes
--save 900 1
--save 300 10
--save 60 10000
--appendfsync everysec
volumes: volumes:
- ./docker-data/redis:/data - evolution_redis:/data
container_name: redis container_name: redis
ports: ports:
- 6379:6379 - 6379:6379
networks: networks:
- evolution-net - evolution-net
volumes:
evolution_instances:
evolution_mongodb_data:
evolution_mongodb_configdb:
evolution_redis:

View File

@ -55,6 +55,7 @@ export const instanceNameSchema: JSONSchema7 = {
], ],
}, },
}, },
qrcode: { type: 'boolean', enum: [true, false] },
}, },
...isNotEmpty('instanceName'), ...isNotEmpty('instanceName'),
}; };

View File

@ -9,6 +9,7 @@ import { WAMonitoringService } from '../services/monitor.service';
import { WAStartupService } from '../services/whatsapp.service'; import { WAStartupService } from '../services/whatsapp.service';
import { WebhookService } from '../services/webhook.service'; import { WebhookService } from '../services/webhook.service';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { wa } from '../types/wa.types';
export class InstanceController { export class InstanceController {
constructor( constructor(
@ -22,7 +23,7 @@ export class InstanceController {
private readonly logger = new Logger(InstanceController.name); 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<Auth>('AUTHENTICATION').INSTANCE.MODE; const mode = this.configService.get<Auth>('AUTHENTICATION').INSTANCE.MODE;
if (mode === 'container') { if (mode === 'container') {
@ -46,9 +47,13 @@ export class InstanceController {
instanceName: instance.instanceName, instanceName: instance.instanceName,
}); });
let getEvents: string[];
if (webhook) { if (webhook) {
try { try {
this.webhookService.create(instance, { enabled: true, url: webhook, events }); this.webhookService.create(instance, { enabled: true, url: webhook, events });
getEvents = (await this.webhookService.find(instance)).events;
} catch (error) { } catch (error) {
this.logger.log(error); this.logger.log(error);
} }
@ -61,6 +66,7 @@ export class InstanceController {
}, },
hash, hash,
webhook, webhook,
events: getEvents,
}; };
} else { } else {
const instance = new WAStartupService( const instance = new WAStartupService(
@ -76,14 +82,26 @@ export class InstanceController {
instanceName: instance.instanceName, instanceName: instance.instanceName,
}); });
let getEvents: string[];
if (webhook) { if (webhook) {
try { 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) { } catch (error) {
this.logger.log(error); this.logger.log(error);
} }
} }
let getQrcode: wa.QrCode;
if (qrcode) {
await instance.connectToWhatsapp();
await delay(2000);
getQrcode = instance.qrCode;
}
return { return {
instance: { instance: {
instanceName: instance.instanceName, instanceName: instance.instanceName,
@ -91,6 +109,8 @@ export class InstanceController {
}, },
hash, hash,
webhook, webhook,
events: getEvents,
qrcode: getQrcode,
}; };
} }
} }

View File

@ -2,4 +2,5 @@ export class InstanceDto {
instanceName: string; instanceName: string;
webhook?: string; webhook?: string;
events?: string[]; events?: string[];
qrcode?: boolean;
} }