mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-16 04:02:54 -06:00
feat: Added connection with pairing code in chatwoot
This commit is contained in:
parent
bcada5d553
commit
7103a95305
@ -7,6 +7,7 @@
|
|||||||
* Created settings controller
|
* Created settings controller
|
||||||
* Added reject call and send text message when receiving a call
|
* Added reject call and send text message when receiving a call
|
||||||
* Added setting to ignore group messages
|
* Added setting to ignore group messages
|
||||||
|
* Added connection with pairing code in chatwoot
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
@ -103,16 +103,10 @@ export class InstanceController {
|
|||||||
|
|
||||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
||||||
let getQrcode: wa.QrCode;
|
let getQrcode: wa.QrCode;
|
||||||
let getPairingCode: string;
|
|
||||||
|
|
||||||
if (qrcode) {
|
if (qrcode) {
|
||||||
this.logger.verbose('creating qrcode');
|
this.logger.verbose('creating qrcode');
|
||||||
await instance.connectToWhatsapp();
|
await instance.connectToWhatsapp(number);
|
||||||
if (number) {
|
|
||||||
this.logger.verbose('creating number');
|
|
||||||
await delay(5000);
|
|
||||||
getPairingCode = await instance.client.requestPairingCode(number);
|
|
||||||
}
|
|
||||||
await delay(2000);
|
await delay(2000);
|
||||||
getQrcode = instance.qrCode;
|
getQrcode = instance.qrCode;
|
||||||
}
|
}
|
||||||
@ -126,14 +120,9 @@ export class InstanceController {
|
|||||||
webhook,
|
webhook,
|
||||||
webhook_by_events,
|
webhook_by_events,
|
||||||
events: getEvents,
|
events: getEvents,
|
||||||
|
qrcode: getQrcode,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (getPairingCode) {
|
|
||||||
result['pairingCode'] = getPairingCode;
|
|
||||||
} else {
|
|
||||||
result['qrcode'] = getQrcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.verbose('instance created');
|
this.logger.verbose('instance created');
|
||||||
this.logger.verbose(result);
|
this.logger.verbose(result);
|
||||||
|
|
||||||
@ -166,6 +155,7 @@ export class InstanceController {
|
|||||||
url: chatwoot_url,
|
url: chatwoot_url,
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
name_inbox: instance.instanceName,
|
name_inbox: instance.instanceName,
|
||||||
|
number,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.chatwootService.initInstanceChatwoot(
|
this.chatwootService.initInstanceChatwoot(
|
||||||
@ -173,6 +163,7 @@ export class InstanceController {
|
|||||||
instance.instanceName,
|
instance.instanceName,
|
||||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
qrcode,
|
qrcode,
|
||||||
|
number,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.log(error);
|
this.logger.log(error);
|
||||||
@ -193,6 +184,7 @@ export class InstanceController {
|
|||||||
token: chatwoot_token,
|
token: chatwoot_token,
|
||||||
url: chatwoot_url,
|
url: chatwoot_url,
|
||||||
sign_msg: chatwoot_sign_msg || false,
|
sign_msg: chatwoot_sign_msg || false,
|
||||||
|
number,
|
||||||
name_inbox: instance.instanceName,
|
name_inbox: instance.instanceName,
|
||||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||||
},
|
},
|
||||||
@ -220,19 +212,7 @@ export class InstanceController {
|
|||||||
|
|
||||||
if (state == 'close') {
|
if (state == 'close') {
|
||||||
this.logger.verbose('connecting');
|
this.logger.verbose('connecting');
|
||||||
await instance.connectToWhatsapp();
|
await instance.connectToWhatsapp(number);
|
||||||
let pairingCode = null;
|
|
||||||
if (number) {
|
|
||||||
this.logger.verbose('creating pairing code');
|
|
||||||
await delay(5000);
|
|
||||||
pairingCode = await instance.client.requestPairingCode(number);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pairingCode) {
|
|
||||||
return {
|
|
||||||
pairingCode,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
await delay(2000);
|
await delay(2000);
|
||||||
return instance.qrCode;
|
return instance.qrCode;
|
||||||
|
@ -5,4 +5,5 @@ export class ChatwootDto {
|
|||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
|
number?: string;
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ export class ChatwootRaw {
|
|||||||
url?: string;
|
url?: string;
|
||||||
name_inbox?: string;
|
name_inbox?: string;
|
||||||
sign_msg?: boolean;
|
sign_msg?: boolean;
|
||||||
|
number?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chatwootSchema = new Schema<ChatwootRaw>({
|
const chatwootSchema = new Schema<ChatwootRaw>({
|
||||||
@ -19,6 +20,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
|
|||||||
url: { type: String, required: true },
|
url: { type: String, required: true },
|
||||||
name_inbox: { type: String, required: true },
|
name_inbox: { type: String, required: true },
|
||||||
sign_msg: { type: Boolean, required: true },
|
sign_msg: { type: Boolean, required: true },
|
||||||
|
number: { type: String, required: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ChatwootModel = dbserver?.model(
|
export const ChatwootModel = dbserver?.model(
|
||||||
|
@ -13,6 +13,7 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
|
|||||||
import { SendMediaDto } from '../dto/sendMessage.dto';
|
import { SendMediaDto } from '../dto/sendMessage.dto';
|
||||||
import { ROOT_DIR } from '../../config/path.config';
|
import { ROOT_DIR } from '../../config/path.config';
|
||||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||||
|
import { delay } from '@whiskeysockets/baileys';
|
||||||
|
|
||||||
export class ChatwootService {
|
export class ChatwootService {
|
||||||
private messageCacheFile: string;
|
private messageCacheFile: string;
|
||||||
@ -154,6 +155,7 @@ export class ChatwootService {
|
|||||||
inboxName: string,
|
inboxName: string,
|
||||||
webhookUrl: string,
|
webhookUrl: string,
|
||||||
qrcode: boolean,
|
qrcode: boolean,
|
||||||
|
number: string,
|
||||||
) {
|
) {
|
||||||
this.logger.verbose('init instance chatwoot: ' + instance.instanceName);
|
this.logger.verbose('init instance chatwoot: ' + instance.instanceName);
|
||||||
|
|
||||||
@ -243,11 +245,18 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('create message for init instance in chatwoot');
|
this.logger.verbose('create message for init instance in chatwoot');
|
||||||
|
|
||||||
|
let contentMsg = '/init';
|
||||||
|
|
||||||
|
if (number) {
|
||||||
|
contentMsg = `/init:${number}`;
|
||||||
|
}
|
||||||
|
|
||||||
const message = await client.messages.create({
|
const message = await client.messages.create({
|
||||||
accountId: this.provider.account_id,
|
accountId: this.provider.account_id,
|
||||||
conversationId: conversation.id,
|
conversationId: conversation.id,
|
||||||
data: {
|
data: {
|
||||||
content: '/init',
|
content: contentMsg,
|
||||||
message_type: 'outgoing',
|
message_type: 'outgoing',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -953,13 +962,14 @@ export class ChatwootService {
|
|||||||
|
|
||||||
const command = messageReceived.replace('/', '');
|
const command = messageReceived.replace('/', '');
|
||||||
|
|
||||||
if (command === 'init' || command === 'iniciar') {
|
if (command.includes('init') || command.includes('iniciar')) {
|
||||||
this.logger.verbose('command init found');
|
this.logger.verbose('command init found');
|
||||||
const state = waInstance?.connectionStatus?.state;
|
const state = waInstance?.connectionStatus?.state;
|
||||||
|
|
||||||
if (state !== 'open') {
|
if (state !== 'open') {
|
||||||
this.logger.verbose('connect to whatsapp');
|
this.logger.verbose('connect to whatsapp');
|
||||||
await waInstance.connectToWhatsapp();
|
const number = command.split(':')[1];
|
||||||
|
await waInstance.connectToWhatsapp(number);
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('whatsapp already connected');
|
this.logger.verbose('whatsapp already connected');
|
||||||
await this.createBotMessage(
|
await this.createBotMessage(
|
||||||
@ -1556,7 +1566,16 @@ export class ChatwootService {
|
|||||||
fileName,
|
fileName,
|
||||||
);
|
);
|
||||||
|
|
||||||
const msgQrCode = `⚡️ QRCode successfully generated!\n\nScan this QR code within the next 40 seconds:`;
|
let msgQrCode = `⚡️ QRCode successfully generated!\n\nScan this QR code within the next 40 seconds.`;
|
||||||
|
|
||||||
|
if (body?.qrcode?.pairingCode) {
|
||||||
|
msgQrCode =
|
||||||
|
msgQrCode +
|
||||||
|
`\n\n*Pairing Code:* ${body.qrcode.pairingCode.substring(
|
||||||
|
0,
|
||||||
|
4,
|
||||||
|
)}-${body.qrcode.pairingCode.substring(4, 8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('send message to chatwoot');
|
this.logger.verbose('send message to chatwoot');
|
||||||
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
||||||
|
@ -25,6 +25,7 @@ import {
|
|||||||
ContactModel,
|
ContactModel,
|
||||||
MessageModel,
|
MessageModel,
|
||||||
MessageUpModel,
|
MessageUpModel,
|
||||||
|
SettingsModel,
|
||||||
WebhookModel,
|
WebhookModel,
|
||||||
} from '../models';
|
} from '../models';
|
||||||
|
|
||||||
@ -241,6 +242,7 @@ export class WAMonitoringService {
|
|||||||
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`);
|
||||||
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
||||||
|
execSync(`rm -rf ${join(STORE_DIR, 'settings', instanceName + '*')}`);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -254,6 +256,7 @@ export class WAMonitoringService {
|
|||||||
await AuthModel.deleteMany({ _id: instanceName });
|
await AuthModel.deleteMany({ _id: instanceName });
|
||||||
await WebhookModel.deleteMany({ _id: instanceName });
|
await WebhookModel.deleteMany({ _id: instanceName });
|
||||||
await ChatwootModel.deleteMany({ _id: instanceName });
|
await ChatwootModel.deleteMany({ _id: instanceName });
|
||||||
|
await SettingsModel.deleteMany({ _id: instanceName });
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -152,6 +152,8 @@ export class WAStartupService {
|
|||||||
private endSession = false;
|
private endSession = false;
|
||||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||||
|
|
||||||
|
private phoneNumber: string;
|
||||||
|
|
||||||
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||||
|
|
||||||
public set instanceName(name: string) {
|
public set instanceName(name: string) {
|
||||||
@ -241,6 +243,12 @@ export class WAStartupService {
|
|||||||
|
|
||||||
public get qrCode(): wa.QrCode {
|
public get qrCode(): wa.QrCode {
|
||||||
this.logger.verbose('Getting qrcode');
|
this.logger.verbose('Getting qrcode');
|
||||||
|
if (this.instance.qrcode?.pairingCode) {
|
||||||
|
return {
|
||||||
|
pairingCode: this.instance.qrcode?.pairingCode,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code: this.instance.qrcode?.code,
|
code: this.instance.qrcode?.code,
|
||||||
base64: this.instance.qrcode?.base64,
|
base64: this.instance.qrcode?.base64,
|
||||||
@ -588,11 +596,6 @@ export class WAStartupService {
|
|||||||
return this.eventEmitter.emit('no.connection', this.instance.name);
|
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.logger.verbose('Incrementing QR code count');
|
||||||
this.instance.qrcode.count++;
|
this.instance.qrcode.count++;
|
||||||
|
|
||||||
@ -603,6 +606,13 @@ export class WAStartupService {
|
|||||||
color: { light: '#ffffff', dark: '#198754' },
|
color: { light: '#ffffff', dark: '#198754' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (this.phoneNumber) {
|
||||||
|
await delay(2000);
|
||||||
|
this.instance.qrcode.pairingCode = await this.client.requestPairingCode(
|
||||||
|
this.phoneNumber,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.logger.verbose('Generating QR code');
|
this.logger.verbose('Generating QR code');
|
||||||
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -614,7 +624,12 @@ export class WAStartupService {
|
|||||||
this.instance.qrcode.code = qr;
|
this.instance.qrcode.code = qr;
|
||||||
|
|
||||||
this.sendDataWebhook(Events.QRCODE_UPDATED, {
|
this.sendDataWebhook(Events.QRCODE_UPDATED, {
|
||||||
qrcode: { instance: this.instance.name, code: qr, base64 },
|
qrcode: {
|
||||||
|
instance: this.instance.name,
|
||||||
|
pairingCode: this.instance.qrcode.pairingCode,
|
||||||
|
code: qr,
|
||||||
|
base64,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.localChatwoot.enabled) {
|
if (this.localChatwoot.enabled) {
|
||||||
@ -622,7 +637,12 @@ export class WAStartupService {
|
|||||||
Events.QRCODE_UPDATED,
|
Events.QRCODE_UPDATED,
|
||||||
{ instanceName: this.instance.name },
|
{ instanceName: this.instance.name },
|
||||||
{
|
{
|
||||||
qrcode: { instance: this.instance.name, code: qr, base64 },
|
qrcode: {
|
||||||
|
instance: this.instance.name,
|
||||||
|
pairingCode: this.instance.qrcode.pairingCode,
|
||||||
|
code: qr,
|
||||||
|
base64,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -631,7 +651,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Generating QR code in terminal');
|
this.logger.verbose('Generating QR code in terminal');
|
||||||
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
||||||
this.logger.log(
|
this.logger.log(
|
||||||
`\n{ instance: ${this.instance.name}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
|
`\n{ instance: ${this.instance.name} pairingCode: ${this.instance.qrcode.pairingCode}, qrcodeCount: ${this.instance.qrcode.count} }\n` +
|
||||||
qrcode,
|
qrcode,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -798,7 +818,7 @@ export class WAStartupService {
|
|||||||
return await useMultiFileAuthState(join(INSTANCE_DIR, this.instance.name));
|
return await useMultiFileAuthState(join(INSTANCE_DIR, this.instance.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async connectToWhatsapp(): Promise<WASocket> {
|
public async connectToWhatsapp(number?: string): Promise<WASocket> {
|
||||||
this.logger.verbose('Connecting to whatsapp');
|
this.logger.verbose('Connecting to whatsapp');
|
||||||
try {
|
try {
|
||||||
this.loadWebhook();
|
this.loadWebhook();
|
||||||
@ -872,6 +892,15 @@ export class WAStartupService {
|
|||||||
|
|
||||||
this.logger.verbose('Socket event handler initialized');
|
this.logger.verbose('Socket event handler initialized');
|
||||||
|
|
||||||
|
this.phoneNumber = number;
|
||||||
|
|
||||||
|
// if (number) {
|
||||||
|
// this.logger.verbose('creating pairing code');
|
||||||
|
// await delay(5000);
|
||||||
|
// this.phoneNumber = number;
|
||||||
|
// this.instance.qrcode.pairingCode = await this.client.requestPairingCode(number);
|
||||||
|
// }
|
||||||
|
|
||||||
return this.client;
|
return this.client;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error(error);
|
this.logger.error(error);
|
||||||
|
@ -27,11 +27,13 @@ export enum Events {
|
|||||||
export declare namespace wa {
|
export declare namespace wa {
|
||||||
export type QrCode = {
|
export type QrCode = {
|
||||||
count?: number;
|
count?: number;
|
||||||
|
pairingCode?: string;
|
||||||
base64?: string;
|
base64?: string;
|
||||||
code?: string;
|
code?: string;
|
||||||
};
|
};
|
||||||
export type Instance = {
|
export type Instance = {
|
||||||
qrcode?: QrCode;
|
qrcode?: QrCode;
|
||||||
|
pairingCode?: string;
|
||||||
authState?: { state: AuthenticationState; saveCreds: () => void };
|
authState?: { state: AuthenticationState; saveCreds: () => void };
|
||||||
name?: string;
|
name?: string;
|
||||||
wuid?: string;
|
wuid?: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user