mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-15 19:52:54 -06:00
fix: Adjusted group message sending with cache improvement
This commit addresses an adjustment in the process of sending messages to groups, with a focus on improving cache utilization. The main modifications include: - Updated 'package.json' to use the latest 'baileys' version from 'WhiskeySockets' repository. - Modified 'src/api/services/channels/whatsapp.baileys.service.ts' for better handling of group metadata cache. The force update group metadata cache function has been removed to streamline the codebase and improve cache handling. This change will ensure that group metadata is updated more efficiently and reduce the overall processing time.
This commit is contained in:
parent
5ea276398f
commit
457628b996
@ -54,7 +54,7 @@
|
|||||||
"@sentry/node": "^7.59.2",
|
"@sentry/node": "^7.59.2",
|
||||||
"amqplib": "^0.10.3",
|
"amqplib": "^0.10.3",
|
||||||
"axios": "^1.6.5",
|
"axios": "^1.6.5",
|
||||||
"baileys": "github:bobslavtriev/Baileys",
|
"baileys": "github:WhiskeySockets/Baileys",
|
||||||
"class-validator": "^0.14.1",
|
"class-validator": "^0.14.1",
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
@ -22,7 +22,6 @@ import makeWASocket, {
|
|||||||
GroupParticipant,
|
GroupParticipant,
|
||||||
isJidBroadcast,
|
isJidBroadcast,
|
||||||
isJidGroup,
|
isJidGroup,
|
||||||
isJidNewsletter,
|
|
||||||
isJidUser,
|
isJidUser,
|
||||||
makeCacheableSignalKeyStore,
|
makeCacheableSignalKeyStore,
|
||||||
MessageUpsertType,
|
MessageUpsertType,
|
||||||
@ -41,6 +40,7 @@ import makeWASocket, {
|
|||||||
import { Label } from 'baileys/lib/Types/Label';
|
import { Label } from 'baileys/lib/Types/Label';
|
||||||
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
import { LabelAssociation } from 'baileys/lib/Types/LabelAssociation';
|
||||||
import { isBase64, isURL } from 'class-validator';
|
import { isBase64, isURL } from 'class-validator';
|
||||||
|
import { randomBytes } from 'crypto';
|
||||||
import EventEmitter2 from 'eventemitter2';
|
import EventEmitter2 from 'eventemitter2';
|
||||||
// import { exec } from 'child_process';
|
// import { exec } from 'child_process';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
@ -146,7 +146,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
super(configService, eventEmitter, prismaRepository, chatwootCache);
|
super(configService, eventEmitter, prismaRepository, chatwootCache);
|
||||||
this.instance.qrcode = { count: 0 };
|
this.instance.qrcode = { count: 0 };
|
||||||
this.recoveringMessages();
|
this.recoveringMessages();
|
||||||
this.cronForceUpdateGroupMetadataCache();
|
|
||||||
|
|
||||||
this.authStateProvider = new AuthStateProvider(this.providerFiles);
|
this.authStateProvider = new AuthStateProvider(this.providerFiles);
|
||||||
}
|
}
|
||||||
@ -204,19 +203,8 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async cronForceUpdateGroupMetadataCache() {
|
|
||||||
if (
|
|
||||||
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
|
|
||||||
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
|
|
||||||
setInterval(async () => {
|
|
||||||
await this.forceUpdateGroupMetadataCache();
|
|
||||||
}, 3600000);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async forceUpdateGroupMetadataCache() {
|
private async forceUpdateGroupMetadataCache() {
|
||||||
|
this.logger.verbose('Force update group metadata cache');
|
||||||
const groups = await this.fetchAllGroups({ getParticipants: 'false' });
|
const groups = await this.fetchAllGroups({ getParticipants: 'false' });
|
||||||
|
|
||||||
for (const group of groups) {
|
for (const group of groups) {
|
||||||
@ -553,7 +541,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
let log;
|
let log;
|
||||||
|
|
||||||
if (session.VERSION) {
|
if (session.VERSION) {
|
||||||
version = session.VERSION.split(',');
|
version = session.VERSION.split('.');
|
||||||
log = `Baileys version env: ${version}`;
|
log = `Baileys version env: ${version}`;
|
||||||
} else {
|
} else {
|
||||||
const baileysVersion = await fetchLatestBaileysVersion();
|
const baileysVersion = await fetchLatestBaileysVersion();
|
||||||
@ -624,7 +612,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
shouldIgnoreJid: (jid) => {
|
shouldIgnoreJid: (jid) => {
|
||||||
const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid);
|
const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid);
|
||||||
const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid);
|
const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid);
|
||||||
const isNewsletter = isJidNewsletter(jid);
|
const isNewsletter = jid.includes('newsletter');
|
||||||
|
|
||||||
return isGroupJid || isBroadcast || isNewsletter;
|
return isGroupJid || isBroadcast || isNewsletter;
|
||||||
},
|
},
|
||||||
@ -637,7 +625,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
},
|
},
|
||||||
userDevicesCache: this.userDevicesCache,
|
userDevicesCache: this.userDevicesCache,
|
||||||
transactionOpts: { maxCommitRetries: 5, delayBetweenTriesMs: 2500 },
|
transactionOpts: { maxCommitRetries: 5, delayBetweenTriesMs: 2500 },
|
||||||
cachedGroupMetadata: this.getGroupMetadataCache,
|
// forceGroupsPrekey: false,
|
||||||
patchMessageBeforeSending(message) {
|
patchMessageBeforeSending(message) {
|
||||||
if (
|
if (
|
||||||
message.deviceSentMessage?.message?.listMessage?.listType ===
|
message.deviceSentMessage?.message?.listMessage?.listType ===
|
||||||
@ -694,7 +682,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
let log;
|
let log;
|
||||||
|
|
||||||
if (session.VERSION) {
|
if (session.VERSION) {
|
||||||
version = session.VERSION.split(',');
|
version = session.VERSION.split('.');
|
||||||
log = `Baileys version env: ${version}`;
|
log = `Baileys version env: ${version}`;
|
||||||
} else {
|
} else {
|
||||||
const baileysVersion = await fetchLatestBaileysVersion();
|
const baileysVersion = await fetchLatestBaileysVersion();
|
||||||
@ -1751,22 +1739,23 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
linkPreview: any,
|
linkPreview: any,
|
||||||
quoted: any,
|
quoted: any,
|
||||||
messageId?: string,
|
messageId?: string,
|
||||||
|
ephemeralExpiration?: number,
|
||||||
participants?: GroupParticipant[],
|
participants?: GroupParticipant[],
|
||||||
) {
|
) {
|
||||||
const option: any = {
|
const option: any = {
|
||||||
quoted,
|
quoted,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (messageId) option.messageId = messageId;
|
|
||||||
|
|
||||||
if (participants)
|
if (participants)
|
||||||
option.cachedGroupMetadata = async () => {
|
option.cachedGroupMetadata = async () => {
|
||||||
participants;
|
return { participants: participants as GroupParticipant[] };
|
||||||
};
|
};
|
||||||
else
|
else option.cachedGroupMetadata = this.getGroupMetadataCache;
|
||||||
option.useCachedGroupMetadata =
|
|
||||||
!!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
|
if (ephemeralExpiration) option.ephemeralExpiration = ephemeralExpiration;
|
||||||
!!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED;
|
|
||||||
|
if (messageId) option.messageId = messageId;
|
||||||
|
else option.messageId = '3EB0' + randomBytes(18).toString('hex').toUpperCase();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!message['audio'] &&
|
!message['audio'] &&
|
||||||
@ -1788,6 +1777,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message['conversation']) {
|
if (message['conversation']) {
|
||||||
return await this.client.sendMessage(
|
return await this.client.sendMessage(
|
||||||
sender,
|
sender,
|
||||||
@ -1897,8 +1887,11 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
|
|
||||||
const sender = isWA.jid;
|
const sender = isWA.jid;
|
||||||
|
|
||||||
|
this.logger.verbose(`Sending message to ${sender}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (options?.delay) {
|
if (options?.delay) {
|
||||||
|
this.logger.verbose(`Typing for ${options.delay}ms to ${sender}`);
|
||||||
if (options.delay > 20000) {
|
if (options.delay > 20000) {
|
||||||
let remainingDelay = options.delay;
|
let remainingDelay = options.delay;
|
||||||
while (remainingDelay > 20000) {
|
while (remainingDelay > 20000) {
|
||||||
@ -1963,14 +1956,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
throw new NotFoundException('Group not found');
|
throw new NotFoundException('Group not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const participansList = group.participants;
|
|
||||||
|
|
||||||
const batchSize = 1;
|
|
||||||
|
|
||||||
const batches = Array.from({ length: Math.ceil(participansList.length / batchSize) }, (_, i) =>
|
|
||||||
participansList.slice(i * batchSize, i * batchSize + batchSize),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (options.mentionsEveryOne) {
|
if (options.mentionsEveryOne) {
|
||||||
mentions = group.participants.map((participant) => participant.id);
|
mentions = group.participants.map((participant) => participant.id);
|
||||||
} else if (options.mentioned?.length) {
|
} else if (options.mentioned?.length) {
|
||||||
@ -1983,25 +1968,46 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let msgId: string | null = null;
|
// console.log('group.participants', group.participants.length);
|
||||||
|
|
||||||
const firstBatch = batches.shift();
|
// const batchSize = 200;
|
||||||
|
|
||||||
if (firstBatch) {
|
// const batches = Array.from({ length: Math.ceil(group.participants.length / batchSize) }, (_, i) =>
|
||||||
messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted, null, firstBatch);
|
// group.participants.slice(i * batchSize, i * batchSize + batchSize),
|
||||||
|
// );
|
||||||
|
|
||||||
msgId = messageSent.key.id;
|
// console.log('batches', batches.length);
|
||||||
|
|
||||||
if (batches.length === 0) return messageSent;
|
// const firstBatch = batches.shift();
|
||||||
|
|
||||||
await Promise.allSettled(
|
// let firstMessage: WAMessage;
|
||||||
batches.map(async (batch: GroupParticipant[]) => {
|
// let msgId: string | null = null;
|
||||||
const messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted, msgId, batch);
|
|
||||||
|
|
||||||
return messageSent;
|
// if (firstBatch) {
|
||||||
}),
|
// firstMessage = await this.sendMessage(sender, message, mentions, linkPreview, quoted, null, firstBatch);
|
||||||
);
|
|
||||||
}
|
// msgId = firstMessage.key.id;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (batches.length === 0) messageSent = firstMessage;
|
||||||
|
|
||||||
|
// await Promise.allSettled(
|
||||||
|
// batches.map(async (batch: GroupParticipant[]) => {
|
||||||
|
// const messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted, msgId, batch);
|
||||||
|
|
||||||
|
// return messageSent;
|
||||||
|
// }),
|
||||||
|
// );
|
||||||
|
|
||||||
|
messageSent = await this.sendMessage(
|
||||||
|
sender,
|
||||||
|
message,
|
||||||
|
mentions,
|
||||||
|
linkPreview,
|
||||||
|
quoted,
|
||||||
|
null,
|
||||||
|
group?.ephemeralDuration,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted);
|
messageSent = await this.sendMessage(sender, message, mentions, linkPreview, quoted);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user