mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 12:06:54 -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 configService: ConfigService,
|
||||||
private readonly repository: RepositoryBroker,
|
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 = {};
|
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');
|
this.logger.verbose('check if message is in cache');
|
||||||
if (!this.messageCache[instance.instanceName]) {
|
if (!this.messageCache[instance.instanceName]) {
|
||||||
return false;
|
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) {
|
private addMessageToCache(instance: InstanceDto, id: number) {
|
||||||
@ -636,6 +644,7 @@ export class ChatwootService {
|
|||||||
filename: string;
|
filename: string;
|
||||||
}[],
|
}[],
|
||||||
messageBody?: any,
|
messageBody?: any,
|
||||||
|
sourceId?: string,
|
||||||
) {
|
) {
|
||||||
this.logger.verbose('create message to instance: ' + instance.instanceName);
|
this.logger.verbose('create message to instance: ' + instance.instanceName);
|
||||||
|
|
||||||
@ -657,6 +666,7 @@ export class ChatwootService {
|
|||||||
message_type: messageType,
|
message_type: messageType,
|
||||||
attachments: attachments,
|
attachments: attachments,
|
||||||
private: privateMessage || false,
|
private: privateMessage || false,
|
||||||
|
source_id: sourceId,
|
||||||
content_attributes: {
|
content_attributes: {
|
||||||
...replyToIds,
|
...replyToIds,
|
||||||
},
|
},
|
||||||
@ -757,6 +767,7 @@ export class ChatwootService {
|
|||||||
content?: string,
|
content?: string,
|
||||||
instance?: InstanceDto,
|
instance?: InstanceDto,
|
||||||
messageBody?: any,
|
messageBody?: any,
|
||||||
|
sourceId?: string,
|
||||||
) {
|
) {
|
||||||
this.logger.verbose('send data to chatwoot');
|
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);
|
this.logger.verbose('get client to instance: ' + this.provider.instanceName);
|
||||||
const config = {
|
const config = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@ -1097,7 +1112,13 @@ export class ChatwootService {
|
|||||||
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
||||||
this.logger.verbose('check if is group');
|
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');
|
this.logger.verbose('message is cached');
|
||||||
return { message: 'bot' };
|
return { message: 'bot' };
|
||||||
}
|
}
|
||||||
@ -1571,7 +1592,15 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('send data to chatwoot');
|
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) {
|
if (!send) {
|
||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
@ -1585,7 +1614,15 @@ export class ChatwootService {
|
|||||||
this.logger.verbose('message is not group');
|
this.logger.verbose('message is not group');
|
||||||
|
|
||||||
this.logger.verbose('send data to chatwoot');
|
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) {
|
if (!send) {
|
||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
@ -1612,6 +1649,7 @@ export class ChatwootService {
|
|||||||
{
|
{
|
||||||
message: { extendedTextMessage: { contextInfo: { stanzaId: reactionMessage.key.id } } },
|
message: { extendedTextMessage: { contextInfo: { stanzaId: reactionMessage.key.id } } },
|
||||||
},
|
},
|
||||||
|
'WAID:' + body.key.id,
|
||||||
);
|
);
|
||||||
if (!send) {
|
if (!send) {
|
||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
@ -1667,6 +1705,7 @@ export class ChatwootService {
|
|||||||
`${bodyMessage}\n\n\n**${title}**\n${description}\n${adsMessage.sourceUrl}`,
|
`${bodyMessage}\n\n\n**${title}**\n${description}\n${adsMessage.sourceUrl}`,
|
||||||
instance,
|
instance,
|
||||||
body,
|
body,
|
||||||
|
'WAID:' + body.key.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!send) {
|
if (!send) {
|
||||||
@ -1695,7 +1734,16 @@ export class ChatwootService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('send data to chatwoot');
|
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) {
|
if (!send) {
|
||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
@ -1709,7 +1757,16 @@ export class ChatwootService {
|
|||||||
this.logger.verbose('message is not group');
|
this.logger.verbose('message is not group');
|
||||||
|
|
||||||
this.logger.verbose('send data to chatwoot');
|
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) {
|
if (!send) {
|
||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
|
Loading…
Reference in New Issue
Block a user