mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 03:42:23 -06:00
fix: retry messages
This commit is contained in:
@@ -36,6 +36,7 @@ export class WAMonitoringService {
|
||||
private readonly repository: RepositoryBroker,
|
||||
private readonly cache: RedisCache,
|
||||
private readonly chatwootCache: CacheService,
|
||||
private readonly messagesLostCache: CacheService,
|
||||
) {
|
||||
this.logger.verbose('instance created');
|
||||
|
||||
@@ -346,6 +347,7 @@ export class WAMonitoringService {
|
||||
this.repository,
|
||||
this.cache,
|
||||
this.chatwootCache,
|
||||
this.messagesLostCache,
|
||||
);
|
||||
|
||||
instance.instanceName = name;
|
||||
@@ -356,6 +358,7 @@ export class WAMonitoringService {
|
||||
this.repository,
|
||||
this.cache,
|
||||
this.chatwootCache,
|
||||
this.messagesLostCache,
|
||||
);
|
||||
|
||||
instance.instanceName = name;
|
||||
|
||||
@@ -71,6 +71,8 @@ export class WAStartupService {
|
||||
public chamaaiService = new ChamaaiService(waMonitor, this.configService);
|
||||
|
||||
public set instanceName(name: string) {
|
||||
this.logger.setInstance(name);
|
||||
|
||||
this.logger.verbose(`Initializing instance '${name}'`);
|
||||
if (!name) {
|
||||
this.logger.verbose('Instance name not found, generating random name with uuid');
|
||||
|
||||
@@ -121,8 +121,6 @@ import { Events, MessageSubtype, TypeMediaMessage, wa } from '../../types/wa.typ
|
||||
import { CacheService } from './../cache.service';
|
||||
import { WAStartupService } from './../whatsapp.service';
|
||||
|
||||
// const retryCache = {};
|
||||
|
||||
export class BaileysStartupService extends WAStartupService {
|
||||
constructor(
|
||||
public readonly configService: ConfigService,
|
||||
@@ -130,12 +128,17 @@ export class BaileysStartupService extends WAStartupService {
|
||||
public readonly repository: RepositoryBroker,
|
||||
public readonly cache: RedisCache,
|
||||
public readonly chatwootCache: CacheService,
|
||||
public readonly messagesLostCache: CacheService,
|
||||
) {
|
||||
super(configService, eventEmitter, repository, chatwootCache);
|
||||
this.logger.verbose('BaileysStartupService initialized');
|
||||
this.cleanStore();
|
||||
this.instance.qrcode = { count: 0 };
|
||||
this.mobile = false;
|
||||
|
||||
setTimeout(() => {
|
||||
this.recoveringMessages();
|
||||
}, 30000);
|
||||
}
|
||||
|
||||
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
|
||||
@@ -148,6 +151,18 @@ export class BaileysStartupService extends WAStartupService {
|
||||
public phoneNumber: string;
|
||||
public mobile: boolean;
|
||||
|
||||
private async recoveringMessages() {
|
||||
this.logger.info('Recovering messages');
|
||||
this.messagesLostCache.keys().then((keys) => {
|
||||
keys.forEach(async (key) => {
|
||||
const message = await this.messagesLostCache.get(key.split(':')[2]);
|
||||
|
||||
if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node')
|
||||
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public get connectionStatus() {
|
||||
this.logger.verbose('Getting connection status');
|
||||
return this.stateConnection;
|
||||
@@ -378,10 +393,12 @@ export class BaileysStartupService extends WAStartupService {
|
||||
│ CONNECTED TO WHATSAPP │
|
||||
└──────────────────────────────┘`.replace(/^ +/gm, ' '),
|
||||
);
|
||||
this.logger.info(`
|
||||
this.logger.info(
|
||||
`
|
||||
wuid: ${formattedWuid}
|
||||
name: ${formattedName}
|
||||
`);
|
||||
`,
|
||||
);
|
||||
|
||||
if (this.localChatwoot.enabled) {
|
||||
this.chatwootService.eventWhatsapp(
|
||||
@@ -1044,12 +1061,22 @@ export class BaileysStartupService extends WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
await this.messagesLostCache.set(received.key.id, received);
|
||||
|
||||
if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') {
|
||||
this.logger.info('Recovering message lost');
|
||||
await this.client.sendMessageAck(JSON.parse(received.messageStubParameters[1], BufferJSON.reviver));
|
||||
|
||||
await this.messagesLostCache.set(received.key.id, received);
|
||||
continue;
|
||||
}
|
||||
|
||||
// const retryCache = (await this.messagesLostCache.get(received.key.id)) || null;
|
||||
|
||||
// if (retryCache) {
|
||||
// this.logger.info('Recovered message lost');
|
||||
// await this.messagesLostCache.delete(received.key.id);
|
||||
// }
|
||||
|
||||
if (
|
||||
(type !== 'notify' && type !== 'append') ||
|
||||
received.message?.protocolMessage ||
|
||||
@@ -1248,7 +1275,6 @@ export class BaileysStartupService extends WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
// if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
|
||||
if (key.remoteJid !== 'status@broadcast') {
|
||||
this.logger.verbose('Message update is valid');
|
||||
|
||||
@@ -1479,27 +1505,12 @@ export class BaileysStartupService extends WAStartupService {
|
||||
if (events['messages.upsert']) {
|
||||
this.logger.verbose('Listening event: messages.upsert');
|
||||
const payload = events['messages.upsert'];
|
||||
// if (payload.messages.find((a) => a?.messageStubType === 2)) {
|
||||
// const msg = payload.messages[0];
|
||||
// retryCache[msg.key.id] = msg;
|
||||
// return;
|
||||
// }
|
||||
this.messageHandle['messages.upsert'](payload, database, settings);
|
||||
}
|
||||
|
||||
if (events['messages.update']) {
|
||||
this.logger.verbose('Listening event: messages.update');
|
||||
const payload = events['messages.update'];
|
||||
// payload.forEach((message) => {
|
||||
// if (retryCache[message.key.id]) {
|
||||
// this.client.ev.emit('messages.upsert', {
|
||||
// messages: [message],
|
||||
// type: 'notify',
|
||||
// });
|
||||
// delete retryCache[message.key.id];
|
||||
// return;
|
||||
// }
|
||||
// });
|
||||
this.messageHandle['messages.update'](payload, database, settings);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ export class BusinessStartupService extends WAStartupService {
|
||||
public readonly repository: RepositoryBroker,
|
||||
public readonly cache: RedisCache,
|
||||
public readonly chatwootCache: CacheService,
|
||||
public readonly messagesLostCache: CacheService,
|
||||
) {
|
||||
super(configService, eventEmitter, repository, chatwootCache);
|
||||
this.logger.verbose('BusinessStartupService initialized');
|
||||
|
||||
Reference in New Issue
Block a user