This commit is contained in:
Davidson Gomes 2024-03-08 12:58:23 -03:00
parent bd09328ec2
commit a81f5953fc
2 changed files with 149 additions and 154 deletions

View File

@ -67,11 +67,11 @@ export const initQueues = (instanceName: string, events: string[]) => {
const amqp = getAMQP(); const amqp = getAMQP();
const rabbitMode = rabbitConfig.MODE || 'isolated'; const rabbitMode = rabbitConfig.MODE || 'isolated';
const exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange'; let exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
const receivedEvents = events.map(parseEvtName); const receivedEvents = events.map(parseEvtName);
if (rabbitMode === 'isolated') { if (rabbitMode === 'isolated') {
// exchangeName = instanceName ?? 'evolution_exchange'; exchangeName = instanceName ?? 'evolution_exchange';
receivedEvents.forEach((event) => { receivedEvents.forEach((event) => {
amqp.assertExchange(exchangeName, 'topic', { amqp.assertExchange(exchangeName, 'topic', {
@ -196,8 +196,8 @@ interface SendEventData {
export const sendEventData = ({ data, event, wuid, apiKey, instanceName }: SendEventData) => { export const sendEventData = ({ data, event, wuid, apiKey, instanceName }: SendEventData) => {
const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ'); const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
const rabbitMode = rabbitConfig.MODE || 'isolated'; const rabbitMode = rabbitConfig.MODE || 'isolated';
const exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange'; let exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
// if (rabbitMode === 'isolated') exchangeName = instanceName ?? 'evolution_exchange'; if (rabbitMode === 'isolated') exchangeName = instanceName ?? 'evolution_exchange';
console.log('exchangeName: ', exchangeName); console.log('exchangeName: ', exchangeName);
console.log('rabbitMode: ', rabbitMode); console.log('rabbitMode: ', rabbitMode);

View File

@ -936,182 +936,177 @@ export class BaileysStartupService extends WAStartupService {
) => { ) => {
try { try {
this.logger.verbose('Event received: messages.upsert'); this.logger.verbose('Event received: messages.upsert');
for (const received of messages) {
if (
(type !== 'notify' && type !== 'append') ||
received.message?.protocolMessage ||
received.message?.pollUpdateMessage
) {
this.logger.verbose('message rejected');
return;
}
const received = messages[0]; if (Long.isLong(received.messageTimestamp)) {
received.messageTimestamp = received.messageTimestamp?.toNumber();
}
// for (const received of messages) { if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
if ( this.logger.verbose('group ignored');
// (type !== 'notify' && type !== 'append') || return;
type !== 'notify' || }
!received?.message ||
received.message?.protocolMessage ||
received.message?.pollUpdateMessage
) {
this.logger.verbose('message rejected');
return;
}
if (Long.isLong(received.messageTimestamp)) { let messageRaw: MessageRaw;
received.messageTimestamp = received.messageTimestamp?.toNumber();
}
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) { const isMedia =
this.logger.verbose('group ignored'); received?.message?.imageMessage ||
return; received?.message?.videoMessage ||
} received?.message?.stickerMessage ||
received?.message?.documentMessage ||
received?.message?.audioMessage;
let messageRaw: MessageRaw; const contentMsg = received?.message[getContentType(received.message)] as any;
const isMedia = if (this.localWebhook.webhook_base64 === true && isMedia) {
received?.message?.imageMessage || const buffer = await downloadMediaMessage(
received?.message?.videoMessage || { key: received.key, message: received?.message },
received?.message?.stickerMessage || 'buffer',
received?.message?.documentMessage || {},
received?.message?.audioMessage; {
logger: P({ level: 'error' }) as any,
const contentMsg = received?.message[getContentType(received.message)] as any; reuploadRequest: this.client.updateMediaMessage,
},
if (this.localWebhook.webhook_base64 === true && isMedia) { );
const buffer = await downloadMediaMessage( messageRaw = {
{ key: received.key, message: received?.message }, key: received.key,
'buffer', pushName: received.pushName,
{}, message: {
{ ...received.message,
logger: P({ level: 'error' }) as any, base64: buffer ? buffer.toString('base64') : undefined,
reuploadRequest: this.client.updateMediaMessage, },
}, contextInfo: contentMsg?.contextInfo,
); messageType: getContentType(received.message),
messageRaw = { messageTimestamp: received.messageTimestamp as number,
key: received.key, owner: this.instance.name,
pushName: received.pushName, source: getDevice(received.key.id),
message: { };
...received.message, } else {
base64: buffer ? buffer.toString('base64') : undefined, messageRaw = {
}, key: received.key,
contextInfo: contentMsg?.contextInfo, pushName: received.pushName,
messageType: getContentType(received.message), message: { ...received.message },
messageTimestamp: received.messageTimestamp as number, contextInfo: contentMsg?.contextInfo,
owner: this.instance.name, messageType: getContentType(received.message),
source: getDevice(received.key.id), messageTimestamp: received.messageTimestamp as number,
}; owner: this.instance.name,
} else { source: getDevice(received.key.id),
messageRaw = {
key: received.key,
pushName: received.pushName,
message: { ...received.message },
contextInfo: contentMsg?.contextInfo,
messageType: getContentType(received.message),
messageTimestamp: received.messageTimestamp as number,
owner: this.instance.name,
source: getDevice(received.key.id),
};
}
if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
await this.client.readMessages([received.key]);
}
if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
await this.client.readMessages([received.key]);
}
this.logger.log(messageRaw);
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
if (this.localChatwoot.enabled && !received.key.id.includes('@broadcast')) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name },
messageRaw,
);
if (chatwootSentMessage?.id) {
messageRaw.chatwoot = {
messageId: chatwootSentMessage.id,
inboxId: chatwootSentMessage.inbox_id,
conversationId: chatwootSentMessage.conversation_id,
}; };
} }
}
const typebotSessionRemoteJid = this.localTypebot.sessions?.find( if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
(session) => session.remoteJid === received.key.remoteJid, await this.client.readMessages([received.key]);
);
if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
if (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name },
messageRaw.key.remoteJid,
messageRaw,
);
} }
}
if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') { if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
await this.chamaaiService.sendChamaai( await this.client.readMessages([received.key]);
{ instanceName: this.instance.name }, }
messageRaw.key.remoteJid,
messageRaw, this.logger.log(messageRaw);
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
if (this.localChatwoot.enabled && !received.key.id.includes('@broadcast')) {
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
Events.MESSAGES_UPSERT,
{ instanceName: this.instance.name },
messageRaw,
);
if (chatwootSentMessage?.id) {
messageRaw.chatwoot = {
messageId: chatwootSentMessage.id,
inboxId: chatwootSentMessage.inbox_id,
conversationId: chatwootSentMessage.conversation_id,
};
}
}
const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
(session) => session.remoteJid === received.key.remoteJid,
); );
}
this.logger.verbose('Inserting message in database'); if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE); if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
if (messageRaw.messageType !== 'reactionMessage')
await this.typebotService.sendTypebot(
{ instanceName: this.instance.name },
messageRaw.key.remoteJid,
messageRaw,
);
}
}
this.logger.verbose('Verifying contact from message'); if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') {
const contact = await this.repository.contact.find({ await this.chamaaiService.sendChamaai(
where: { owner: this.instance.name, id: received.key.remoteJid }, { instanceName: this.instance.name },
}); messageRaw.key.remoteJid,
messageRaw,
);
}
const contactRaw: ContactRaw = { this.logger.verbose('Inserting message in database');
id: received.key.remoteJid, await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);
pushName: received.pushName,
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
owner: this.instance.name,
};
if (contactRaw.id === 'status@broadcast') { this.logger.verbose('Verifying contact from message');
this.logger.verbose('Contact is status@broadcast'); const contact = await this.repository.contact.find({
return; where: { owner: this.instance.name, id: received.key.remoteJid },
} });
if (contact?.length) {
this.logger.verbose('Contact found in database');
const contactRaw: ContactRaw = { const contactRaw: ContactRaw = {
id: received.key.remoteJid, id: received.key.remoteJid,
pushName: contact[0].pushName, pushName: received.pushName,
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl, profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
owner: this.instance.name, owner: this.instance.name,
}; };
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE'); if (contactRaw.id === 'status@broadcast') {
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw); this.logger.verbose('Contact is status@broadcast');
return;
if (this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name },
contactRaw,
);
} }
this.logger.verbose('Updating contact in database'); if (contact?.length) {
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS); this.logger.verbose('Contact found in database');
return; const contactRaw: ContactRaw = {
id: received.key.remoteJid,
pushName: contact[0].pushName,
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
owner: this.instance.name,
};
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
if (this.localChatwoot.enabled) {
await this.chatwootService.eventWhatsapp(
Events.CONTACTS_UPDATE,
{ instanceName: this.instance.name },
contactRaw,
);
}
this.logger.verbose('Updating contact in database');
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
return;
}
this.logger.verbose('Contact not found in database');
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
this.logger.verbose('Inserting contact in database');
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
} }
this.logger.verbose('Contact not found in database');
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
this.logger.verbose('Inserting contact in database');
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
// }
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
} }