Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@
const messageRaw = this.prepareMessage(received);

if (messageRaw.messageType === 'pollUpdateMessage') {
const pollCreationKey = messageRaw.message.pollUpdateMessage.pollCreationMessageKey;

Check failure on line 1207 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'pollUpdateMessage' does not exist on type 'JsonValue'.
const pollMessage = (await this.getMessage(pollCreationKey, true)) as proto.IWebMessageInfo;
const pollMessageSecret = (await this.getMessage(pollCreationKey)) as any;

Expand All @@ -1213,7 +1213,7 @@
(pollMessage.message as any).pollCreationMessage?.options ||
(pollMessage.message as any).pollCreationMessageV3?.options ||
[];
const pollVote = messageRaw.message.pollUpdateMessage.vote;

Check failure on line 1216 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'pollUpdateMessage' does not exist on type 'JsonValue'.

const voterJid = received.key.fromMe
? this.instance.wuid
Expand Down Expand Up @@ -1293,14 +1293,14 @@
})
.map((option) => option.optionName);

messageRaw.message.pollUpdateMessage.vote.selectedOptions = selectedOptionNames;

Check failure on line 1296 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'pollUpdateMessage' does not exist on type 'JsonValue'.

const pollUpdates = pollOptions.map((option) => ({
name: option.optionName,
voters: selectedOptionNames.includes(option.optionName) ? [successfulVoterJid] : [],
}));

messageRaw.pollUpdates = pollUpdates;

Check failure on line 1303 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'pollUpdates' does not exist on type '{ status: string; message: JsonValue; instanceId: string; id: string; pushName: string; key: JsonValue; participant: string; messageType: string; contextInfo: JsonValue; ... 8 more ...; sessionId: string; }'.
}
}

Expand Down Expand Up @@ -1348,13 +1348,13 @@
});

if (openAiDefaultSettings && openAiDefaultSettings.openaiCredsId && openAiDefaultSettings.speechToText) {
messageRaw.message.speechToText = `[audio] ${await this.openaiService.speechToText(received, this)}`;

Check failure on line 1351 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'speechToText' does not exist on type 'JsonValue'.
}
}

if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pollUpdates, ...messageData } = messageRaw;

Check failure on line 1357 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'pollUpdates' does not exist on type '{ status: string; message: JsonValue; instanceId: string; id: string; pushName: string; key: JsonValue; participant: string; messageType: string; contextInfo: JsonValue; ... 8 more ...; sessionId: string; }'.
const msg = await this.prismaRepository.message.create({ data: messageData });

const { remoteJid } = received.key;
Expand Down Expand Up @@ -1430,7 +1430,7 @@

const mediaUrl = await s3Service.getObjectUrl(fullName);

messageRaw.message.mediaUrl = mediaUrl;

Check failure on line 1433 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'mediaUrl' does not exist on type 'JsonValue'.

await this.prismaRepository.message.update({ where: { id: msg.id }, data: messageRaw });
}
Expand All @@ -1452,7 +1452,7 @@
);

if (buffer) {
messageRaw.message.base64 = buffer.toString('base64');

Check failure on line 1455 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'base64' does not exist on type 'JsonValue'.
} else {
// retry to download media
const buffer = await downloadMediaMessage(
Expand All @@ -1463,7 +1463,7 @@
);

if (buffer) {
messageRaw.message.base64 = buffer.toString('base64');

Check failure on line 1466 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'base64' does not exist on type 'JsonValue'.
}
}
} catch (error) {
Expand All @@ -1475,7 +1475,7 @@
this.logger.verbose(messageRaw);

sendTelemetry(`received.message.${messageRaw.messageType ?? 'unknown'}`);
if (messageRaw.key.remoteJid?.includes('@lid') && messageRaw.key.remoteJidAlt) {

Check failure on line 1478 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Property 'remoteJid' does not exist on type 'JsonValue'.
messageRaw.key.remoteJid = messageRaw.key.remoteJidAlt;
}
console.log(messageRaw);
Expand Down Expand Up @@ -1561,7 +1561,12 @@
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}

for await (const { key, update } of args) {
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
// Normalize JIDs immediately to ensure consistent DB lookups
const keyAny = key as any;
if (keyAny.remoteJid) keyAny.remoteJid = keyAny.remoteJid.replace(/:.*$/, '');
if (keyAny.participant) keyAny.participant = keyAny.participant.replace(/:.*$/, '');

if (settings?.groupsIgnore && keyAny.remoteJid?.includes('@g.us')) {
continue;
}

Expand Down Expand Up @@ -1612,9 +1617,9 @@

const message: any = {
keyId: key.id,
remoteJid: key?.remoteJid,
remoteJid: keyAny?.remoteJid?.replace(/:.*$/, ''),
fromMe: key.fromMe,
participant: key?.participant,
participant: keyAny?.participant?.replace(/:.*$/, ''),
status: status[update.status] ?? 'SERVER_ACK',
pollUpdates,
instanceId: this.instanceId,
Expand Down Expand Up @@ -4662,26 +4667,20 @@
return obj;
}

private prepareMessage(message: proto.IWebMessageInfo): any {
const contentType = getContentType(message.message);
const contentMsg = message?.message[contentType] as any;

const messageRaw = {
key: message.key, // Save key exactly as it comes from Baileys
pushName:
message.pushName ||
(message.key.fromMe
? 'Você'
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
status: status[message.status],
message: this.deserializeMessageBuffers({ ...message.message }),
contextInfo: this.deserializeMessageBuffers(contentMsg?.contextInfo),
messageType: contentType || 'unknown',
messageTimestamp: Long.isLong(message.messageTimestamp)
? message.messageTimestamp.toNumber()
: (message.messageTimestamp as number),
private prepareMessage(message: WAMessage): Message {
const keyAny = message.key as any;
const messageRaw: any = {
key: {
...message.key,
remoteJid: keyAny.remoteJid?.replace(/:.*$/, ''),
participant: keyAny.participant?.replace(/:.*$/, ''),
},
pushName: message.pushName,
message: message.message,
messageType: getContentType(message.message),
messageTimestamp: message.messageTimestamp,
source: getDevice(keyAny.id),
instanceId: this.instanceId,
source: getDevice(message.key.id),
};

if (!messageRaw.status && message.key.fromMe === false) {
Expand Down
Loading