mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-21 19:47:21 -06:00
fix: added support to use source_id to check chatwoot's webhook needs to be ignored.
With this, messageCache is used to support Chatwoot version <= 3.3.1. After this version we can remove use of message cache and use only the source_id field to check chatwoot's webhook needs to be ignored. It's have much better performance.
This commit is contained in:
parent
82894a1c4f
commit
8b5f73badd
@ -28,16 +28,24 @@ export class ChatwootService {
|
||||
private readonly configService: ConfigService,
|
||||
private readonly repository: RepositoryBroker,
|
||||
) {
|
||||
// messageCache is used to support Chatwoot version <= 3.3.1.
|
||||
// after this version we can remove use of message cache and use source_id to check webhook needs to be ignored
|
||||
this.messageCache = {};
|
||||
}
|
||||
|
||||
private isMessageInCache(instance: InstanceDto, id: number) {
|
||||
private isMessageInCache(instance: InstanceDto, id: number, remove = true) {
|
||||
this.logger.verbose('check if message is in cache');
|
||||
if (!this.messageCache[instance.instanceName]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.messageCache[instance.instanceName].has(id);
|
||||
const hasId = this.messageCache[instance.instanceName].has(id);
|
||||
|
||||
if (remove) {
|
||||
this.messageCache[instance.instanceName].delete(id);
|
||||
}
|
||||
|
||||
return hasId;
|
||||
}
|
||||
|
||||
private addMessageToCache(instance: InstanceDto, id: number) {
|
||||
@ -636,6 +644,7 @@ export class ChatwootService {
|
||||
filename: string;
|
||||
}[],
|
||||
messageBody?: any,
|
||||
sourceId?: string,
|
||||
) {
|
||||
this.logger.verbose('create message to instance: ' + instance.instanceName);
|
||||
|
||||
@ -657,6 +666,7 @@ export class ChatwootService {
|
||||
message_type: messageType,
|
||||
attachments: attachments,
|
||||
private: privateMessage || false,
|
||||
source_id: sourceId,
|
||||
content_attributes: {
|
||||
...replyToIds,
|
||||
},
|
||||
@ -757,6 +767,7 @@ export class ChatwootService {
|
||||
content?: string,
|
||||
instance?: InstanceDto,
|
||||
messageBody?: any,
|
||||
sourceId?: string,
|
||||
) {
|
||||
this.logger.verbose('send data to chatwoot');
|
||||
|
||||
@ -783,6 +794,10 @@ export class ChatwootService {
|
||||
}
|
||||
}
|
||||
|
||||
if (sourceId) {
|
||||
data.append('source_id', sourceId);
|
||||
}
|
||||
|
||||
this.logger.verbose('get client to instance: ' + this.provider.instanceName);
|
||||
const config = {
|
||||
method: 'post',
|
||||
@ -1097,7 +1112,13 @@ export class ChatwootService {
|
||||
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
||||
this.logger.verbose('check if is group');
|
||||
|
||||
if (this.isMessageInCache(instance, body.id)) {
|
||||
// messageCache is used to support Chatwoot version <= 3.3.1.
|
||||
// after this version we can remove use of message cache and use only source_id value check
|
||||
// use of source_id is better for performance
|
||||
if (
|
||||
body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:' ||
|
||||
this.isMessageInCache(instance, body.id)
|
||||
) {
|
||||
this.logger.verbose('message is cached');
|
||||
return { message: 'bot' };
|
||||
}
|
||||
@ -1571,7 +1592,15 @@ export class ChatwootService {
|
||||
}
|
||||
|
||||
this.logger.verbose('send data to chatwoot');
|
||||
const send = await this.sendData(getConversation, fileName, messageType, content, instance, body);
|
||||
const send = await this.sendData(
|
||||
getConversation,
|
||||
fileName,
|
||||
messageType,
|
||||
content,
|
||||
instance,
|
||||
body,
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
|
||||
if (!send) {
|
||||
this.logger.warn('message not sent');
|
||||
@ -1585,7 +1614,15 @@ export class ChatwootService {
|
||||
this.logger.verbose('message is not group');
|
||||
|
||||
this.logger.verbose('send data to chatwoot');
|
||||
const send = await this.sendData(getConversation, fileName, messageType, bodyMessage, instance, body);
|
||||
const send = await this.sendData(
|
||||
getConversation,
|
||||
fileName,
|
||||
messageType,
|
||||
bodyMessage,
|
||||
instance,
|
||||
body,
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
|
||||
if (!send) {
|
||||
this.logger.warn('message not sent');
|
||||
@ -1612,6 +1649,7 @@ export class ChatwootService {
|
||||
{
|
||||
message: { extendedTextMessage: { contextInfo: { stanzaId: reactionMessage.key.id } } },
|
||||
},
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
if (!send) {
|
||||
this.logger.warn('message not sent');
|
||||
@ -1667,6 +1705,7 @@ export class ChatwootService {
|
||||
`${bodyMessage}\n\n\n**${title}**\n${description}\n${adsMessage.sourceUrl}`,
|
||||
instance,
|
||||
body,
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
|
||||
if (!send) {
|
||||
@ -1695,7 +1734,16 @@ export class ChatwootService {
|
||||
}
|
||||
|
||||
this.logger.verbose('send data to chatwoot');
|
||||
const send = await this.createMessage(instance, getConversation, content, messageType, false, [], body);
|
||||
const send = await this.createMessage(
|
||||
instance,
|
||||
getConversation,
|
||||
content,
|
||||
messageType,
|
||||
false,
|
||||
[],
|
||||
body,
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
|
||||
if (!send) {
|
||||
this.logger.warn('message not sent');
|
||||
@ -1709,7 +1757,16 @@ export class ChatwootService {
|
||||
this.logger.verbose('message is not group');
|
||||
|
||||
this.logger.verbose('send data to chatwoot');
|
||||
const send = await this.createMessage(instance, getConversation, bodyMessage, messageType, false, [], body);
|
||||
const send = await this.createMessage(
|
||||
instance,
|
||||
getConversation,
|
||||
bodyMessage,
|
||||
messageType,
|
||||
false,
|
||||
[],
|
||||
body,
|
||||
'WAID:' + body.key.id,
|
||||
);
|
||||
|
||||
if (!send) {
|
||||
this.logger.warn('message not sent');
|
||||
|
Loading…
Reference in New Issue
Block a user