Skip to content

Commit 8b3cbbb

Browse files
committed
fix(proto): normalize proto.Message before serialization to prevent "this.isZero is not a function"
1 parent e888647 commit 8b3cbbb

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

src/whatsapp/services/whatsapp.service.ts

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import makeWASocket, {
4141
AnyMessageContent,
4242
BaileysEventMap,
4343
BufferedEventData,
44+
BufferJSON,
4445
CacheStore,
4546
Chat,
4647
ConnectionState,
@@ -885,13 +886,22 @@ export class WAStartupService {
885886

886887
this.client.sendPresenceUpdate('unavailable');
887888

888-
let timestamp = received?.messageTimestamp;
889+
let timestamp = received?.messageTimestamp;
889890

890-
if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
891+
if (
892+
timestamp &&
893+
typeof timestamp === 'object' &&
894+
typeof timestamp.toNumber === 'function'
895+
) {
891896
timestamp = timestamp.toNumber();
892-
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
897+
} else if (
898+
timestamp &&
899+
typeof timestamp === 'object' &&
900+
'low' in timestamp &&
901+
'high' in timestamp
902+
) {
893903
timestamp = Number(timestamp.low) || 0;
894-
}else if (typeof timestamp !== 'number') {
904+
} else if (typeof timestamp !== 'number') {
895905
timestamp = 0;
896906
}
897907

@@ -924,7 +934,9 @@ export class WAStartupService {
924934
keyParticipant:
925935
received?.participant || this.normalizeParticipant(received.key),
926936
messageType,
927-
content: JSON.parse(JSON.stringify(received.message[messageType])) as PrismType.Prisma.JsonValue,
937+
content: JSON.parse(
938+
JSON.stringify(received.message[messageType]),
939+
) as PrismType.Prisma.JsonValue,
928940
messageTimestamp: timestamp,
929941
instanceId: this.instance.id,
930942
device: (() => {
@@ -1074,11 +1086,11 @@ export class WAStartupService {
10741086
},
10751087

10761088
'group-participants.update': (participantsUpdate: {
1077-
id: string
1078-
author: string
1079-
authorPn?: string
1080-
participants: GroupParticipant[]
1081-
action: ParticipantAction
1089+
id: string;
1090+
author: string;
1091+
authorPn?: string;
1092+
participants: GroupParticipant[];
1093+
action: ParticipantAction;
10821094
}) => {
10831095
this.ws.send(this.instance.name, 'group-participants.update', participantsUpdate);
10841096
this.sendDataWebhook('groupsParticipantsUpdated', participantsUpdate);
@@ -1349,23 +1361,33 @@ export class WAStartupService {
13491361
const messageSent: Partial<PrismType.Message> = await (async () => {
13501362
let q: WAMessage;
13511363
if (quoted) {
1364+
if (quoted.messageType === 'conversation') {
1365+
quoted.messageType = 'extendedTextMessage';
1366+
}
1367+
13521368
q = {
13531369
key: {
1354-
remoteJid: quoted.keyRemoteJid,
1355-
fromMe: quoted.keyFromMe,
13561370
id: quoted.keyId,
1371+
fromMe: quoted.keyFromMe,
1372+
remoteJid: quoted.keyRemoteJid,
13571373
},
13581374
message: {
1359-
[quoted.messageType]: quoted.content,
1375+
[quoted.messageType]: {
1376+
contextInfo: {},
1377+
...(quoted.content as any),
1378+
},
13601379
},
1380+
messageTimestamp: quoted.messageTimestamp,
13611381
};
1382+
1383+
q.message = proto.Message.decode(proto.Message.encode(q.message).finish());
13621384
}
13631385

13641386
let m: proto.IWebMessageInfo;
13651387

13661388
const messageId = options?.messageId || ulid(Date.now());
13671389

1368-
if (message?.['react'] || message?.['edit']) {
1390+
if (message?.['react'] || message?.['edit'] || message?.['text']) {
13691391
m = await this.client.sendMessage(recipient, message as AnyMessageContent, {
13701392
quoted: q,
13711393
messageId,
@@ -1393,14 +1415,23 @@ export class WAStartupService {
13931415
}
13941416
}
13951417
}
1396-
1397-
let timestamp = m?.messageTimestamp;
13981418

1399-
if (timestamp && typeof timestamp === 'object' && typeof timestamp.toNumber === 'function') {
1419+
let timestamp = m?.messageTimestamp;
1420+
1421+
if (
1422+
timestamp &&
1423+
typeof timestamp === 'object' &&
1424+
typeof timestamp.toNumber === 'function'
1425+
) {
14001426
timestamp = timestamp.toNumber();
1401-
}else if (timestamp && typeof timestamp === 'object' && 'low' in timestamp && 'high' in timestamp) {
1427+
} else if (
1428+
timestamp &&
1429+
typeof timestamp === 'object' &&
1430+
'low' in timestamp &&
1431+
'high' in timestamp
1432+
) {
14021433
timestamp = Number(timestamp.low) || 0;
1403-
}else if (typeof timestamp !== 'number') {
1434+
} else if (typeof timestamp !== 'number') {
14041435
timestamp = 0;
14051436
}
14061437

@@ -1411,7 +1442,9 @@ export class WAStartupService {
14111442
keyParticipant: m?.participant,
14121443
pushName: m?.pushName,
14131444
messageType: getContentType(m.message),
1414-
content: JSON.parse(JSON.stringify(m.message[getContentType(m.message)])) as PrismType.Prisma.JsonValue,
1445+
content: JSON.parse(
1446+
JSON.stringify(m.message[getContentType(m.message)]),
1447+
) as PrismType.Prisma.JsonValue,
14151448
messageTimestamp: timestamp,
14161449
instanceId: this.instance.id,
14171450
device: 'web',
@@ -1455,13 +1488,9 @@ export class WAStartupService {
14551488

14561489
// Send Message Controller
14571490
public async textMessage(data: SendTextDto) {
1458-
return await this.sendMessageWithTyping(
1491+
return await this.sendMessageWithTyping<AnyMessageContent>(
14591492
data.number,
1460-
{
1461-
extendedTextMessage: {
1462-
text: data.textMessage.text,
1463-
},
1464-
},
1493+
{ text: data.textMessage.text },
14651494
data?.options,
14661495
);
14671496
}
@@ -2559,5 +2588,4 @@ export class WAStartupService {
25592588
throw new BadRequestException('Unable to leave the group', error.toString());
25602589
}
25612590
}
2562-
25632591
}

0 commit comments

Comments
 (0)