Skip to content

Commit 17ecc88

Browse files
committed
test
1 parent 7bc4c7b commit 17ecc88

File tree

1 file changed

+142
-139
lines changed

1 file changed

+142
-139
lines changed

src/whatsapp/services/whatsapp.baileys.service.ts

Lines changed: 142 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -936,177 +936,180 @@ export class BaileysStartupService extends WAStartupService {
936936
) => {
937937
try {
938938
this.logger.verbose('Event received: messages.upsert');
939-
for (const received of messages) {
940-
if (
941-
(type !== 'notify' && type !== 'append') ||
942-
received.message?.protocolMessage ||
943-
received.message?.pollUpdateMessage
944-
) {
945-
this.logger.verbose('message rejected');
946-
return;
947-
}
948939

949-
if (Long.isLong(received.messageTimestamp)) {
950-
received.messageTimestamp = received.messageTimestamp?.toNumber();
951-
}
940+
const received = messages[0];
952941

953-
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
954-
this.logger.verbose('group ignored');
955-
return;
956-
}
942+
// for (const received of messages) {
943+
if (
944+
(type !== 'notify' && type !== 'append') ||
945+
received.message?.protocolMessage ||
946+
received.message?.pollUpdateMessage
947+
) {
948+
this.logger.verbose('message rejected');
949+
return;
950+
}
957951

958-
let messageRaw: MessageRaw;
952+
if (Long.isLong(received.messageTimestamp)) {
953+
received.messageTimestamp = received.messageTimestamp?.toNumber();
954+
}
959955

960-
const isMedia =
961-
received?.message?.imageMessage ||
962-
received?.message?.videoMessage ||
963-
received?.message?.stickerMessage ||
964-
received?.message?.documentMessage ||
965-
received?.message?.audioMessage;
956+
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
957+
this.logger.verbose('group ignored');
958+
return;
959+
}
966960

967-
const contentMsg = received.message[getContentType(received.message)] as any;
961+
let messageRaw: MessageRaw;
968962

969-
if (this.localWebhook.webhook_base64 === true && isMedia) {
970-
const buffer = await downloadMediaMessage(
971-
{ key: received.key, message: received?.message },
972-
'buffer',
973-
{},
974-
{
975-
logger: P({ level: 'error' }) as any,
976-
reuploadRequest: this.client.updateMediaMessage,
977-
},
978-
);
979-
messageRaw = {
980-
key: received.key,
981-
pushName: received.pushName,
982-
message: {
983-
...received.message,
984-
base64: buffer ? buffer.toString('base64') : undefined,
985-
},
986-
contextInfo: contentMsg?.contextInfo,
987-
messageType: getContentType(received.message),
988-
messageTimestamp: received.messageTimestamp as number,
989-
owner: this.instance.name,
990-
source: getDevice(received.key.id),
991-
};
992-
} else {
993-
messageRaw = {
994-
key: received.key,
995-
pushName: received.pushName,
996-
message: { ...received.message },
997-
contextInfo: contentMsg?.contextInfo,
998-
messageType: getContentType(received.message),
999-
messageTimestamp: received.messageTimestamp as number,
1000-
owner: this.instance.name,
1001-
source: getDevice(received.key.id),
1002-
};
1003-
}
963+
const isMedia =
964+
received?.message?.imageMessage ||
965+
received?.message?.videoMessage ||
966+
received?.message?.stickerMessage ||
967+
received?.message?.documentMessage ||
968+
received?.message?.audioMessage;
1004969

1005-
if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
1006-
await this.client.readMessages([received.key]);
1007-
}
970+
const contentMsg = received?.message[getContentType(received.message)] as any;
1008971

1009-
if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
1010-
await this.client.readMessages([received.key]);
1011-
}
972+
if (this.localWebhook.webhook_base64 === true && isMedia) {
973+
const buffer = await downloadMediaMessage(
974+
{ key: received.key, message: received?.message },
975+
'buffer',
976+
{},
977+
{
978+
logger: P({ level: 'error' }) as any,
979+
reuploadRequest: this.client.updateMediaMessage,
980+
},
981+
);
982+
messageRaw = {
983+
key: received.key,
984+
pushName: received.pushName,
985+
message: {
986+
...received.message,
987+
base64: buffer ? buffer.toString('base64') : undefined,
988+
},
989+
contextInfo: contentMsg?.contextInfo,
990+
messageType: getContentType(received.message),
991+
messageTimestamp: received.messageTimestamp as number,
992+
owner: this.instance.name,
993+
source: getDevice(received.key.id),
994+
};
995+
} else {
996+
messageRaw = {
997+
key: received.key,
998+
pushName: received.pushName,
999+
message: { ...received.message },
1000+
contextInfo: contentMsg?.contextInfo,
1001+
messageType: getContentType(received.message),
1002+
messageTimestamp: received.messageTimestamp as number,
1003+
owner: this.instance.name,
1004+
source: getDevice(received.key.id),
1005+
};
1006+
}
10121007

1013-
this.logger.log(messageRaw);
1008+
if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
1009+
await this.client.readMessages([received.key]);
1010+
}
10141011

1015-
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
1016-
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
1012+
if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
1013+
await this.client.readMessages([received.key]);
1014+
}
10171015

1018-
if (this.localChatwoot.enabled && !received.key.id.includes('@broadcast')) {
1019-
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
1020-
Events.MESSAGES_UPSERT,
1021-
{ instanceName: this.instance.name },
1022-
messageRaw,
1023-
);
1016+
this.logger.log(messageRaw);
10241017

1025-
if (chatwootSentMessage?.id) {
1026-
messageRaw.chatwoot = {
1027-
messageId: chatwootSentMessage.id,
1028-
inboxId: chatwootSentMessage.inbox_id,
1029-
conversationId: chatwootSentMessage.conversation_id,
1030-
};
1031-
}
1032-
}
1018+
this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
1019+
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
10331020

1034-
const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
1035-
(session) => session.remoteJid === received.key.remoteJid,
1021+
if (this.localChatwoot.enabled && !received.key.id.includes('@broadcast')) {
1022+
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
1023+
Events.MESSAGES_UPSERT,
1024+
{ instanceName: this.instance.name },
1025+
messageRaw,
10361026
);
10371027

1038-
if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
1039-
if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
1040-
if (messageRaw.messageType !== 'reactionMessage')
1041-
await this.typebotService.sendTypebot(
1042-
{ instanceName: this.instance.name },
1043-
messageRaw.key.remoteJid,
1044-
messageRaw,
1045-
);
1046-
}
1028+
if (chatwootSentMessage?.id) {
1029+
messageRaw.chatwoot = {
1030+
messageId: chatwootSentMessage.id,
1031+
inboxId: chatwootSentMessage.inbox_id,
1032+
conversationId: chatwootSentMessage.conversation_id,
1033+
};
10471034
}
1035+
}
10481036

1049-
if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') {
1050-
await this.chamaaiService.sendChamaai(
1051-
{ instanceName: this.instance.name },
1052-
messageRaw.key.remoteJid,
1053-
messageRaw,
1054-
);
1037+
const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
1038+
(session) => session.remoteJid === received.key.remoteJid,
1039+
);
1040+
1041+
if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
1042+
if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
1043+
if (messageRaw.messageType !== 'reactionMessage')
1044+
await this.typebotService.sendTypebot(
1045+
{ instanceName: this.instance.name },
1046+
messageRaw.key.remoteJid,
1047+
messageRaw,
1048+
);
10551049
}
1050+
}
10561051

1057-
this.logger.verbose('Inserting message in database');
1058-
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);
1052+
if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') {
1053+
await this.chamaaiService.sendChamaai(
1054+
{ instanceName: this.instance.name },
1055+
messageRaw.key.remoteJid,
1056+
messageRaw,
1057+
);
1058+
}
10591059

1060-
this.logger.verbose('Verifying contact from message');
1061-
const contact = await this.repository.contact.find({
1062-
where: { owner: this.instance.name, id: received.key.remoteJid },
1063-
});
1060+
this.logger.verbose('Inserting message in database');
1061+
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);
1062+
1063+
this.logger.verbose('Verifying contact from message');
1064+
const contact = await this.repository.contact.find({
1065+
where: { owner: this.instance.name, id: received.key.remoteJid },
1066+
});
1067+
1068+
const contactRaw: ContactRaw = {
1069+
id: received.key.remoteJid,
1070+
pushName: received.pushName,
1071+
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
1072+
owner: this.instance.name,
1073+
};
1074+
1075+
if (contactRaw.id === 'status@broadcast') {
1076+
this.logger.verbose('Contact is status@broadcast');
1077+
return;
1078+
}
10641079

1080+
if (contact?.length) {
1081+
this.logger.verbose('Contact found in database');
10651082
const contactRaw: ContactRaw = {
10661083
id: received.key.remoteJid,
1067-
pushName: received.pushName,
1084+
pushName: contact[0].pushName,
10681085
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
10691086
owner: this.instance.name,
10701087
};
10711088

1072-
if (contactRaw.id === 'status@broadcast') {
1073-
this.logger.verbose('Contact is status@broadcast');
1074-
return;
1075-
}
1076-
1077-
if (contact?.length) {
1078-
this.logger.verbose('Contact found in database');
1079-
const contactRaw: ContactRaw = {
1080-
id: received.key.remoteJid,
1081-
pushName: contact[0].pushName,
1082-
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
1083-
owner: this.instance.name,
1084-
};
1085-
1086-
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
1087-
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
1088-
1089-
if (this.localChatwoot.enabled) {
1090-
await this.chatwootService.eventWhatsapp(
1091-
Events.CONTACTS_UPDATE,
1092-
{ instanceName: this.instance.name },
1093-
contactRaw,
1094-
);
1095-
}
1089+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
1090+
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
10961091

1097-
this.logger.verbose('Updating contact in database');
1098-
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1099-
return;
1092+
if (this.localChatwoot.enabled) {
1093+
await this.chatwootService.eventWhatsapp(
1094+
Events.CONTACTS_UPDATE,
1095+
{ instanceName: this.instance.name },
1096+
contactRaw,
1097+
);
11001098
}
11011099

1102-
this.logger.verbose('Contact not found in database');
1100+
this.logger.verbose('Updating contact in database');
1101+
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1102+
return;
1103+
}
11031104

1104-
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
1105-
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
1105+
this.logger.verbose('Contact not found in database');
11061106

1107-
this.logger.verbose('Inserting contact in database');
1108-
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1109-
}
1107+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
1108+
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
1109+
1110+
this.logger.verbose('Inserting contact in database');
1111+
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1112+
// }
11101113
} catch (error) {
11111114
this.logger.error(error);
11121115
}

0 commit comments

Comments
 (0)