Added connection functionality via pairing code

This commit is contained in:
Davidson Gomes 2023-07-21 20:37:58 -03:00
parent 93a5d07f9a
commit 8f4d44a212
8 changed files with 41 additions and 5 deletions

View File

@ -1,3 +1,9 @@
# 1.4.0 (homolog)
### Features
* Added connection functionality via pairing code
# 1.3.2 (2023-07-21 17:19)
### Fixed

View File

@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.3.2",
"version": "1.4.0",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {

View File

@ -58,6 +58,7 @@ export const instanceNameSchema: JSONSchema7 = {
},
},
qrcode: { type: 'boolean', enum: [true, false] },
number: { type: 'string', pattern: '^\\d+[\\.@\\w-]+' },
token: { type: 'string' },
},
...isNotEmpty('instanceName'),

View File

@ -34,6 +34,7 @@ export class InstanceController {
webhook_by_events,
events,
qrcode,
number,
token,
chatwoot_account_id,
chatwoot_token,
@ -102,10 +103,16 @@ export class InstanceController {
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
let getQrcode: wa.QrCode;
let getParingCode: string;
if (qrcode) {
this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp();
if (number) {
this.logger.verbose('creating number');
await delay(5000);
getParingCode = await instance.client.requestPairingCode(number);
}
await delay(2000);
getQrcode = instance.qrCode;
}
@ -120,6 +127,7 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
pairingCode: getParingCode,
qrcode: getQrcode,
});
@ -132,6 +140,7 @@ export class InstanceController {
webhook,
webhook_by_events,
events: getEvents,
pairingCode: getParingCode,
qrcode: getQrcode,
};
}
@ -195,7 +204,7 @@ export class InstanceController {
};
}
public async connectToWhatsapp({ instanceName }: InstanceDto) {
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {
try {
this.logger.verbose(
'requested connectToWhatsapp from ' + instanceName + ' instance',
@ -210,8 +219,17 @@ export class InstanceController {
case 'close':
this.logger.verbose('connecting');
await instance.connectToWhatsapp();
let pairingCode = null;
if (number) {
this.logger.verbose('creating pairing code');
await delay(5000);
pairingCode = await instance.client.requestPairingCode(number);
}
await delay(2000);
return instance.qrCode;
return {
pairingCode,
...instance.qrCode,
};
case 'connecting':
return instance.qrCode;
default:

View File

@ -4,6 +4,7 @@ export class InstanceDto {
webhook_by_events?: boolean;
events?: string[];
qrcode?: boolean;
number?: string;
token?: string;
chatwoot_account_id?: string;
chatwoot_token?: string;

View File

@ -60,7 +60,8 @@ export class InstanceRouter extends RouterBroker {
request: req,
schema: instanceNameSchema,
ClassRef: InstanceDto,
execute: (instance) => instanceController.connectToWhatsapp(instance),
execute: (instance, data) =>
instanceController.connectToWhatsapp(instance, data),
});
return res.status(HttpStatus.OK).json(response);

View File

@ -545,6 +545,11 @@ export class WAStartupService {
return this.eventEmitter.emit('no.connection', this.instance.name);
}
// pairing code
// await delay(5000);
// const code = await this.client.requestPairingCode('557499879409');
// console.log(`Pairing code: ${code}`);
this.logger.verbose('Incrementing QR code count');
this.instance.qrcode.count++;

View File

@ -25,7 +25,11 @@ export enum Events {
}
export declare namespace wa {
export type QrCode = { count?: number; base64?: string; code?: string };
export type QrCode = {
count?: number;
base64?: string;
code?: string;
};
export type Instance = {
qrcode?: QrCode;
authState?: { state: AuthenticationState; saveCreds: () => void };