Fixes in mongodb and chatwoot

This commit is contained in:
Davidson Gomes 2023-12-06 15:21:50 -03:00
parent 4c69b059d4
commit a90f0f2c59
7 changed files with 197 additions and 179 deletions

View File

@ -3,6 +3,7 @@
### Feature
* Added AWS SQS Integration
* Added compatibility with typebot v2
* Added endpoint sendPresence
### Fixed
@ -16,6 +17,8 @@
* Removed await from webhook when sending a message
* Update typebot.service.ts - element.underline change ~ for *
* Adjusts in proxy
* Removed api restart on receiving an error
* Fixes in mongodb and chatwoot
# 1.5.4 (2023-10-09 20:43)

View File

@ -8,7 +8,7 @@ export function onUnexpectedError() {
stderr: process.stderr.fd,
error,
});
process.exit(1);
// process.exit(1);
});
process.on('unhandledRejection', (error, origin) => {
@ -18,6 +18,6 @@ export function onUnexpectedError() {
stderr: process.stderr.fd,
error,
});
process.exit(1);
// process.exit(1);
});
}

View File

@ -101,7 +101,7 @@ export async function useMultiFileAuthStateDb(
},
},
saveCreds: async () => {
return writeData(creds, 'creds');
return await writeData(creds, 'creds');
},
};
}

View File

@ -987,6 +987,10 @@ export class ChatwootService {
const state = waInstance?.connectionStatus?.state;
if (state !== 'open') {
if (state === 'close') {
this.logger.verbose('request cleaning up instance: ' + instance.instanceName);
await this.waMonitor.cleaningUp(instance.instanceName);
}
this.logger.verbose('connect to whatsapp');
const number = command.split(':')[1];
await waInstance.connectToWhatsapp(number);
@ -1272,13 +1276,6 @@ export class ChatwootService {
public async eventWhatsapp(event: string, instance: InstanceDto, body: any) {
this.logger.verbose('event whatsapp to instance: ' + instance.instanceName);
try {
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
return null;
}
const waInstance = this.waMonitor.waInstances[instance.instanceName];
if (!waInstance) {
@ -1286,6 +1283,13 @@ export class ChatwootService {
return null;
}
const client = await this.clientCw(instance);
if (!client) {
this.logger.warn('client not found');
return null;
}
if (event === 'messages.upsert' || event === 'send.message') {
this.logger.verbose('event messages.upsert');
@ -1535,16 +1539,18 @@ export class ChatwootService {
await this.createBotMessage(instance, msgStatus, 'incoming');
}
// if (event === 'connection.update') {
// this.logger.verbose('event connection.update');
if (event === 'connection.update') {
this.logger.verbose('event connection.update');
// if (body.status === 'open') {
// const msgConnection = `🚀 Connection successfully established!`;
// this.logger.verbose('send message to chatwoot');
// await this.createBotMessage(instance, msgConnection, 'incoming');
// }
// }
if (body.status === 'open') {
// if we have qrcode count then we understand that a new connection was established
if (this.waMonitor.waInstances[instance.instanceName].qrCode.count > 0) {
const msgConnection = `🚀 Connection successfully established!`;
this.logger.verbose('send message to chatwoot');
await this.createBotMessage(instance, msgConnection, 'incoming');
}
}
}
if (event === 'qrcode.updated') {
this.logger.verbose('event qrcode.updated');

View File

@ -13,11 +13,11 @@ import { RedisCache } from '../../libs/redis.client';
import {
AuthModel,
ChamaaiModel,
ChatModel,
// ChatModel,
ChatwootModel,
ContactModel,
MessageModel,
MessageUpModel,
// ContactModel,
// MessageModel,
// MessageUpModel,
ProxyModel,
RabbitmqModel,
SettingsModel,
@ -39,7 +39,7 @@ export class WAMonitoringService {
this.removeInstance();
this.noConnection();
this.delInstanceFiles();
// this.delInstanceFiles();
Object.assign(this.db, configService.get<Database>('DATABASE'));
Object.assign(this.redis, configService.get<Redis>('REDIS'));
@ -193,6 +193,13 @@ export class WAMonitoringService {
public async cleaningUp(instanceName: string) {
this.logger.verbose('cleaning up instance: ' + instanceName);
if (this.redis.ENABLED) {
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
this.cache.reference = instanceName;
await this.cache.delAll();
return;
}
if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
this.logger.verbose('cleaning up instance in database: ' + instanceName);
await this.repository.dbServer.connect();
@ -203,13 +210,6 @@ export class WAMonitoringService {
return;
}
if (this.redis.ENABLED) {
this.logger.verbose('cleaning up instance in redis: ' + instanceName);
this.cache.reference = instanceName;
await this.cache.delAll();
return;
}
this.logger.verbose('cleaning up instance in files: ' + instanceName);
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
}
@ -239,10 +239,10 @@ export class WAMonitoringService {
this.logger.verbose('cleaning store database instance: ' + instanceName);
await ChatModel.deleteMany({ owner: instanceName });
await ContactModel.deleteMany({ owner: instanceName });
await MessageUpModel.deleteMany({ owner: instanceName });
await MessageModel.deleteMany({ owner: instanceName });
// await ChatModel.deleteMany({ owner: instanceName });
// await ContactModel.deleteMany({ owner: instanceName });
// await MessageUpModel.deleteMany({ owner: instanceName });
// await MessageModel.deleteMany({ owner: instanceName });
await AuthModel.deleteMany({ _id: instanceName });
await WebhookModel.deleteMany({ _id: instanceName });
@ -357,8 +357,8 @@ export class WAMonitoringService {
this.eventEmitter.on('logout.instance', async (instanceName: string) => {
this.logger.verbose('logout instance: ' + instanceName);
try {
this.logger.verbose('request cleaning up instance: ' + instanceName);
this.cleaningUp(instanceName);
// this.logger.verbose('request cleaning up instance: ' + instanceName);
// this.cleaningUp(instanceName);
} finally {
this.logger.warn(`Instance "${instanceName}" - LOGOUT`);
}

View File

@ -246,7 +246,7 @@ export class TypebotService {
prefilledVariables: {
...data.prefilledVariables,
remoteJid: data.remoteJid,
pushName: data.pushName || '',
pushName: data.pushName || data.prefilledVariables?.pushName || '',
instanceName: instance.instanceName,
},
},

View File

@ -1336,7 +1336,7 @@ export class WAStartupService {
msgRetryCounterCache: this.msgRetryCounterCache,
getMessage: async (key) => (await this.getMessage(key)) as Promise<proto.IMessage>,
generateHighQualityLinkPreview: true,
syncFullHistory: true,
syncFullHistory: false,
userDevicesCache: this.userDevicesCache,
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 10 },
patchMessageBeforeSending: (message) => {
@ -1409,6 +1409,7 @@ export class WAStartupService {
browser,
version,
markOnlineOnConnect: this.localSettings.always_online,
retryRequestDelayMs: 10,
connectTimeoutMs: 60_000,
qrTimeout: 40_000,
defaultQueryTimeoutMs: undefined,
@ -1416,9 +1417,9 @@ export class WAStartupService {
msgRetryCounterCache: this.msgRetryCounterCache,
getMessage: async (key) => (await this.getMessage(key)) as Promise<proto.IMessage>,
generateHighQualityLinkPreview: true,
syncFullHistory: true,
syncFullHistory: false,
userDevicesCache: this.userDevicesCache,
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 },
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 10 },
patchMessageBeforeSending: (message) => {
const requiresPatch = !!(message.buttonsMessage || message.listMessage || message.templateMessage);
if (requiresPatch) {
@ -1637,10 +1638,14 @@ export class WAStartupService {
database: Database,
settings: SettingsRaw,
) => {
try {
this.logger.verbose('Event received: messages.upsert');
const received = messages[0];
if (type !== 'notify' || received.message?.protocolMessage || received.message?.pollUpdateMessage) {
for (const received of messages) {
if (
(type !== 'notify' && type !== 'append') ||
received.message?.protocolMessage ||
received.message?.pollUpdateMessage
) {
this.logger.verbose('message rejected');
return;
}
@ -1718,7 +1723,7 @@ export class WAStartupService {
(session) => session.remoteJid === received.key.remoteJid,
);
if (this.localTypebot.enabled || typebotSessionRemoteJid) {
if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name },
@ -1728,7 +1733,7 @@ export class WAStartupService {
}
}
if (this.localChamaai.enabled && messageRaw.key.fromMe === false) {
if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') {
await this.chamaaiService.sendChamaai(
{ instanceName: this.instance.name },
messageRaw.key.remoteJid,
@ -1788,6 +1793,10 @@ export class WAStartupService {
this.logger.verbose('Inserting contact in database');
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
}
} catch (error) {
this.logger.error(error);
}
},
'messages.update': async (args: WAMessageUpdate[], database: Database, settings: SettingsRaw) => {