Skip to content

Commit a81f595

Browse files
committed
test
1 parent bd09328 commit a81f595

File tree

2 files changed

+143
-148
lines changed

2 files changed

+143
-148
lines changed

src/libs/amqp.server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ export const initQueues = (instanceName: string, events: string[]) => {
6767
const amqp = getAMQP();
6868

6969
const rabbitMode = rabbitConfig.MODE || 'isolated';
70-
const exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
70+
let exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
7171

7272
const receivedEvents = events.map(parseEvtName);
7373
if (rabbitMode === 'isolated') {
74-
// exchangeName = instanceName ?? 'evolution_exchange';
74+
exchangeName = instanceName ?? 'evolution_exchange';
7575

7676
receivedEvents.forEach((event) => {
7777
amqp.assertExchange(exchangeName, 'topic', {
@@ -196,8 +196,8 @@ interface SendEventData {
196196
export const sendEventData = ({ data, event, wuid, apiKey, instanceName }: SendEventData) => {
197197
const rabbitConfig = configService.get<Rabbitmq>('RABBITMQ');
198198
const rabbitMode = rabbitConfig.MODE || 'isolated';
199-
const exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
200-
// if (rabbitMode === 'isolated') exchangeName = instanceName ?? 'evolution_exchange';
199+
let exchangeName = rabbitConfig.EXCHANGE_NAME ?? 'evolution_exchange';
200+
if (rabbitMode === 'isolated') exchangeName = instanceName ?? 'evolution_exchange';
201201

202202
console.log('exchangeName: ', exchangeName);
203203
console.log('rabbitMode: ', rabbitMode);

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

Lines changed: 139 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -936,182 +936,177 @@ 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+
}
939948

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

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

954-
if (Long.isLong(received.messageTimestamp)) {
955-
received.messageTimestamp = received.messageTimestamp?.toNumber();
956-
}
958+
let messageRaw: MessageRaw;
957959

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

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

965-
const isMedia =
966-
received?.message?.imageMessage ||
967-
received?.message?.videoMessage ||
968-
received?.message?.stickerMessage ||
969-
received?.message?.documentMessage ||
970-
received?.message?.audioMessage;
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+
}
9711004

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

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

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

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

1018-
this.logger.log(messageRaw);
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+
);
10191024

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

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

1030-
if (chatwootSentMessage?.id) {
1031-
messageRaw.chatwoot = {
1032-
messageId: chatwootSentMessage.id,
1033-
inboxId: chatwootSentMessage.inbox_id,
1034-
conversationId: chatwootSentMessage.conversation_id,
1035-
};
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+
}
10361047
}
1037-
}
10381048

1039-
const typebotSessionRemoteJid = this.localTypebot.sessions?.find(
1040-
(session) => session.remoteJid === received.key.remoteJid,
1041-
);
1042-
1043-
if ((this.localTypebot.enabled && type === 'notify') || typebotSessionRemoteJid) {
1044-
if (!(this.localTypebot.listening_from_me === false && messageRaw.key.fromMe === true)) {
1045-
if (messageRaw.messageType !== 'reactionMessage')
1046-
await this.typebotService.sendTypebot(
1047-
{ instanceName: this.instance.name },
1048-
messageRaw.key.remoteJid,
1049-
messageRaw,
1050-
);
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+
);
10511055
}
1052-
}
10531056

1054-
if (this.localChamaai.enabled && messageRaw.key.fromMe === false && type === 'notify') {
1055-
await this.chamaaiService.sendChamaai(
1056-
{ instanceName: this.instance.name },
1057-
messageRaw.key.remoteJid,
1058-
messageRaw,
1059-
);
1060-
}
1061-
1062-
this.logger.verbose('Inserting message in database');
1063-
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);
1064-
1065-
this.logger.verbose('Verifying contact from message');
1066-
const contact = await this.repository.contact.find({
1067-
where: { owner: this.instance.name, id: received.key.remoteJid },
1068-
});
1069-
1070-
const contactRaw: ContactRaw = {
1071-
id: received.key.remoteJid,
1072-
pushName: received.pushName,
1073-
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
1074-
owner: this.instance.name,
1075-
};
1057+
this.logger.verbose('Inserting message in database');
1058+
await this.repository.message.insert([messageRaw], this.instance.name, database.SAVE_DATA.NEW_MESSAGE);
10761059

1077-
if (contactRaw.id === 'status@broadcast') {
1078-
this.logger.verbose('Contact is status@broadcast');
1079-
return;
1080-
}
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+
});
10811064

1082-
if (contact?.length) {
1083-
this.logger.verbose('Contact found in database');
10841065
const contactRaw: ContactRaw = {
10851066
id: received.key.remoteJid,
1086-
pushName: contact[0].pushName,
1067+
pushName: received.pushName,
10871068
profilePictureUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
10881069
owner: this.instance.name,
10891070
};
10901071

1091-
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
1092-
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
1093-
1094-
if (this.localChatwoot.enabled) {
1095-
await this.chatwootService.eventWhatsapp(
1096-
Events.CONTACTS_UPDATE,
1097-
{ instanceName: this.instance.name },
1098-
contactRaw,
1099-
);
1072+
if (contactRaw.id === 'status@broadcast') {
1073+
this.logger.verbose('Contact is status@broadcast');
1074+
return;
11001075
}
11011076

1102-
this.logger.verbose('Updating contact in database');
1103-
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1104-
return;
1105-
}
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+
};
11061085

1107-
this.logger.verbose('Contact not found in database');
1086+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPDATE');
1087+
this.sendDataWebhook(Events.CONTACTS_UPDATE, contactRaw);
11081088

1109-
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
1110-
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
1089+
if (this.localChatwoot.enabled) {
1090+
await this.chatwootService.eventWhatsapp(
1091+
Events.CONTACTS_UPDATE,
1092+
{ instanceName: this.instance.name },
1093+
contactRaw,
1094+
);
1095+
}
11111096

1112-
this.logger.verbose('Inserting contact in database');
1113-
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1114-
// }
1097+
this.logger.verbose('Updating contact in database');
1098+
await this.repository.contact.update([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1099+
return;
1100+
}
1101+
1102+
this.logger.verbose('Contact not found in database');
1103+
1104+
this.logger.verbose('Sending data to webhook in event CONTACTS_UPSERT');
1105+
this.sendDataWebhook(Events.CONTACTS_UPSERT, contactRaw);
1106+
1107+
this.logger.verbose('Inserting contact in database');
1108+
this.repository.contact.insert([contactRaw], this.instance.name, database.SAVE_DATA.CONTACTS);
1109+
}
11151110
} catch (error) {
11161111
this.logger.error(error);
11171112
}

0 commit comments

Comments
 (0)