mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -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
|
||||
* Added reject call and send text message when receiving a call
|
||||
* Added setting to ignore group messages
|
||||
* Added connection with pairing code in chatwoot
|
||||
|
||||
### Fixed
|
||||
|
||||
|
@ -103,16 +103,10 @@ export class InstanceController {
|
||||
|
||||
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
|
||||
let getQrcode: wa.QrCode;
|
||||
let getPairingCode: string;
|
||||
|
||||
if (qrcode) {
|
||||
this.logger.verbose('creating qrcode');
|
||||
await instance.connectToWhatsapp();
|
||||
if (number) {
|
||||
this.logger.verbose('creating number');
|
||||
await delay(5000);
|
||||
getPairingCode = await instance.client.requestPairingCode(number);
|
||||
}
|
||||
await instance.connectToWhatsapp(number);
|
||||
await delay(2000);
|
||||
getQrcode = instance.qrCode;
|
||||
}
|
||||
@ -126,14 +120,9 @@ export class InstanceController {
|
||||
webhook,
|
||||
webhook_by_events,
|
||||
events: getEvents,
|
||||
qrcode: getQrcode,
|
||||
};
|
||||
|
||||
if (getPairingCode) {
|
||||
result['pairingCode'] = getPairingCode;
|
||||
} else {
|
||||
result['qrcode'] = getQrcode;
|
||||
}
|
||||
|
||||
this.logger.verbose('instance created');
|
||||
this.logger.verbose(result);
|
||||
|
||||
@ -166,6 +155,7 @@ export class InstanceController {
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg || false,
|
||||
name_inbox: instance.instanceName,
|
||||
number,
|
||||
});
|
||||
|
||||
this.chatwootService.initInstanceChatwoot(
|
||||
@ -173,6 +163,7 @@ export class InstanceController {
|
||||
instance.instanceName,
|
||||
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
qrcode,
|
||||
number,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.log(error);
|
||||
@ -193,6 +184,7 @@ export class InstanceController {
|
||||
token: chatwoot_token,
|
||||
url: chatwoot_url,
|
||||
sign_msg: chatwoot_sign_msg || false,
|
||||
number,
|
||||
name_inbox: instance.instanceName,
|
||||
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
|
||||
},
|
||||
@ -220,19 +212,7 @@ export class InstanceController {
|
||||
|
||||
if (state == '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);
|
||||
}
|
||||
|
||||
if (pairingCode) {
|
||||
return {
|
||||
pairingCode,
|
||||
};
|
||||
}
|
||||
await instance.connectToWhatsapp(number);
|
||||
|
||||
await delay(2000);
|
||||
return instance.qrCode;
|
||||
|
@ -5,4 +5,5 @@ export class ChatwootDto {
|
||||
url?: string;
|
||||
name_inbox?: string;
|
||||
sign_msg?: boolean;
|
||||
number?: string;
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ export class ChatwootRaw {
|
||||
url?: string;
|
||||
name_inbox?: string;
|
||||
sign_msg?: boolean;
|
||||
number?: string;
|
||||
}
|
||||
|
||||
const chatwootSchema = new Schema<ChatwootRaw>({
|
||||
@ -19,6 +20,7 @@ const chatwootSchema = new Schema<ChatwootRaw>({
|
||||
url: { type: String, required: true },
|
||||
name_inbox: { type: String, required: true },
|
||||
sign_msg: { type: Boolean, required: true },
|
||||
number: { type: String, required: true },
|
||||
});
|
||||
|
||||
export const ChatwootModel = dbserver?.model(
|
||||
|
@ -13,6 +13,7 @@ import { SendAudioDto } from '../dto/sendMessage.dto';
|
||||
import { SendMediaDto } from '../dto/sendMessage.dto';
|
||||
import { ROOT_DIR } from '../../config/path.config';
|
||||
import { ConfigService, HttpServer } from '../../config/env.config';
|
||||
import { delay } from '@whiskeysockets/baileys';
|
||||
|
||||
export class ChatwootService {
|
||||
private messageCacheFile: string;
|
||||
@ -154,6 +155,7 @@ export class ChatwootService {
|
||||
inboxName: string,
|
||||
webhookUrl: string,
|
||||
qrcode: boolean,
|
||||
number: string,
|
||||
) {
|
||||
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');
|
||||
|
||||
let contentMsg = '/init';
|
||||
|
||||
if (number) {
|
||||
contentMsg = `/init:${number}`;
|
||||
}
|
||||
|
||||
const message = await client.messages.create({
|
||||
accountId: this.provider.account_id,
|
||||
conversationId: conversation.id,
|
||||
data: {
|
||||
content: '/init',
|
||||
content: contentMsg,
|
||||
message_type: 'outgoing',
|
||||
},
|
||||
});
|
||||
@ -953,13 +962,14 @@ export class ChatwootService {
|
||||
|
||||
const command = messageReceived.replace('/', '');
|
||||
|
||||
if (command === 'init' || command === 'iniciar') {
|
||||
if (command.includes('init') || command.includes('iniciar')) {
|
||||
this.logger.verbose('command init found');
|
||||
const state = waInstance?.connectionStatus?.state;
|
||||
|
||||
if (state !== 'open') {
|
||||
this.logger.verbose('connect to whatsapp');
|
||||
await waInstance.connectToWhatsapp();
|
||||
const number = command.split(':')[1];
|
||||
await waInstance.connectToWhatsapp(number);
|
||||
} else {
|
||||
this.logger.verbose('whatsapp already connected');
|
||||
await this.createBotMessage(
|
||||
@ -1556,7 +1566,16 @@ export class ChatwootService {
|
||||
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');
|
||||
await this.createBotMessage(instance, msgQrCode, 'incoming');
|
||||
|
@ -25,6 +25,7 @@ import {
|
||||
ContactModel,
|
||||
MessageModel,
|
||||
MessageUpModel,
|
||||
SettingsModel,
|
||||
WebhookModel,
|
||||
} 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, 'webhook', instanceName + '.json')}`);
|
||||
execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`);
|
||||
execSync(`rm -rf ${join(STORE_DIR, 'settings', instanceName + '*')}`);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -254,6 +256,7 @@ export class WAMonitoringService {
|
||||
await AuthModel.deleteMany({ _id: instanceName });
|
||||
await WebhookModel.deleteMany({ _id: instanceName });
|
||||
await ChatwootModel.deleteMany({ _id: instanceName });
|
||||
await SettingsModel.deleteMany({ _id: instanceName });
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -152,6 +152,8 @@ export class WAStartupService {
|
||||
private endSession = false;
|
||||
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
|
||||
|
||||
private phoneNumber: string;
|
||||
|
||||
private chatwootService = new ChatwootService(waMonitor, this.configService);
|
||||
|
||||
public set instanceName(name: string) {
|
||||
@ -241,6 +243,12 @@ export class WAStartupService {
|
||||
|
||||
public get qrCode(): wa.QrCode {
|
||||
this.logger.verbose('Getting qrcode');
|
||||
if (this.instance.qrcode?.pairingCode) {
|
||||
return {
|
||||
pairingCode: this.instance.qrcode?.pairingCode,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: this.instance.qrcode?.code,
|
||||
base64: this.instance.qrcode?.base64,
|
||||
@ -588,11 +596,6 @@ 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++;
|
||||
|
||||
@ -603,6 +606,13 @@ export class WAStartupService {
|
||||
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');
|
||||
qrcode.toDataURL(qr, optsQrcode, (error, base64) => {
|
||||
if (error) {
|
||||
@ -614,7 +624,12 @@ export class WAStartupService {
|
||||
this.instance.qrcode.code = qr;
|
||||
|
||||
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) {
|
||||
@ -622,7 +637,12 @@ export class WAStartupService {
|
||||
Events.QRCODE_UPDATED,
|
||||
{ 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');
|
||||
qrcodeTerminal.generate(qr, { small: true }, (qrcode) =>
|
||||
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,
|
||||
),
|
||||
);
|
||||
@ -798,7 +818,7 @@ export class WAStartupService {
|
||||
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');
|
||||
try {
|
||||
this.loadWebhook();
|
||||
@ -872,6 +892,15 @@ export class WAStartupService {
|
||||
|
||||
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;
|
||||
} catch (error) {
|
||||
this.logger.error(error);
|
||||
|
@ -27,11 +27,13 @@ export enum Events {
|
||||
export declare namespace wa {
|
||||
export type QrCode = {
|
||||
count?: number;
|
||||
pairingCode?: string;
|
||||
base64?: string;
|
||||
code?: string;
|
||||
};
|
||||
export type Instance = {
|
||||
qrcode?: QrCode;
|
||||
pairingCode?: string;
|
||||
authState?: { state: AuthenticationState; saveCreds: () => void };
|
||||
name?: string;
|
||||
wuid?: string;
|
||||
|
Loading…
Reference in New Issue
Block a user