This commit is contained in:
Davidson Gomes 2024-03-08 12:02:13 -03:00
parent 7bc4c7b36e
commit 17ecc88f80

View File

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