Skip to content

Commit cebe5b6

Browse files
committed
fix: added context info on find messages
1 parent a531796 commit cebe5b6

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,25 @@ export class BaileysStartupService extends ChannelStartupService {
288288
statusReason: (lastDisconnect?.error as Boom)?.output?.statusCode ?? 200,
289289
};
290290

291+
try {
292+
const profilePic = await this.profilePicture(this.client.user.id.replace(/:\d+/, ''));
293+
this.instance.profilePictureUrl = profilePic.profilePictureUrl;
294+
} catch (error) {
295+
this.instance.profilePictureUrl = null;
296+
}
297+
291298
this.sendDataWebhook(Events.CONNECTION_UPDATE, {
292299
instance: this.instance.name,
300+
wuid: this.client.user?.id.replace(/:\d+/, ''),
301+
pictureUrl: this.instance.profilePictureUrl,
302+
profileName: await this.getProfileName(),
293303
...this.stateConnection,
294304
});
295305
}
296306

297307
if (connection === 'close') {
298308
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
299-
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
309+
const codesToNotReconnect = [DisconnectReason.forbidden, 402, 406];
300310
const shouldReconnect = !codesToNotReconnect.includes(statusCode);
301311
if (shouldReconnect) {
302312
await this.connectToWhatsapp(this.phoneNumber);
@@ -538,6 +548,7 @@ export class BaileysStartupService extends ChannelStartupService {
538548
public async connectToWhatsapp(number?: string): Promise<WASocket> {
539549
try {
540550
this.loadSettings();
551+
this.loadWebhook();
541552
this.loadProxy();
542553

543554
return await this.createClient(number);
@@ -908,23 +919,37 @@ export class BaileysStartupService extends ChannelStartupService {
908919
});
909920
}
910921

911-
if (isMedia) {
912-
const buffer = await downloadMediaMessage(
913-
{ key: received.key, message: received?.message },
914-
'buffer',
915-
{},
916-
{
917-
logger: P({ level: 'error' }) as any,
918-
reuploadRequest: this.client.updateMediaMessage,
919-
},
920-
);
922+
if (this.localWebhook.enabled) {
923+
if (isMedia && this.localWebhook.webhookBase64) {
924+
const buffer = await downloadMediaMessage(
925+
{ key: received.key, message: received?.message },
926+
'buffer',
927+
{},
928+
{
929+
logger: P({ level: 'error' }) as any,
930+
reuploadRequest: this.client.updateMediaMessage,
931+
},
932+
);
921933

922-
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
934+
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
935+
}
923936
}
924937

925938
this.logger.log(messageRaw);
926939

927-
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
940+
let messageWebhook = messageRaw;
941+
let picUrl;
942+
943+
if (received.key.remoteJid.includes('@s.whatsapp')) {
944+
picUrl = (await this.profilePicture(received.key.remoteJid)).profilePictureUrl;
945+
946+
messageWebhook = {
947+
...messageRaw,
948+
profilePictureUrl: picUrl,
949+
};
950+
}
951+
952+
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageWebhook);
928953

929954
const contact = await this.prismaRepository.contact.findFirst({
930955
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
@@ -933,7 +958,6 @@ export class BaileysStartupService extends ChannelStartupService {
933958
const contactRaw: { remoteJid: string; pushName: string; profilePicUrl?: string; instanceId: string } = {
934959
remoteJid: received.key.remoteJid,
935960
pushName: received.key.fromMe ? '' : received.key.fromMe == null ? '' : received.pushName,
936-
profilePicUrl: (await this.profilePicture(received.key.remoteJid)).profilePictureUrl,
937961
instanceId: this.instanceId,
938962
};
939963

@@ -1665,18 +1689,20 @@ export class BaileysStartupService extends ChannelStartupService {
16651689
});
16661690
}
16671691

1668-
if (isMedia) {
1669-
const buffer = await downloadMediaMessage(
1670-
{ key: messageRaw.key, message: messageRaw?.message },
1671-
'buffer',
1672-
{},
1673-
{
1674-
logger: P({ level: 'error' }) as any,
1675-
reuploadRequest: this.client.updateMediaMessage,
1676-
},
1677-
);
1692+
if (this.localWebhook.enabled) {
1693+
if (isMedia && this.localWebhook.webhookBase64) {
1694+
const buffer = await downloadMediaMessage(
1695+
{ key: messageRaw.key, message: messageRaw?.message },
1696+
'buffer',
1697+
{},
1698+
{
1699+
logger: P({ level: 'error' }) as any,
1700+
reuploadRequest: this.client.updateMediaMessage,
1701+
},
1702+
);
16781703

1679-
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
1704+
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
1705+
}
16801706
}
16811707

16821708
this.logger.log(messageRaw);

src/api/services/channel.service.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class ChannelStartupService {
2626
public readonly localChatwoot: wa.LocalChatwoot = {};
2727
public readonly localProxy: wa.LocalProxy = {};
2828
public readonly localSettings: wa.LocalSettings = {};
29+
public readonly localWebhook: wa.LocalWebHook = {};
2930

3031
public setInstance(instance: InstanceDto) {
3132
this.logger.setInstance(instance.instanceName);
@@ -92,6 +93,17 @@ export class ChannelStartupService {
9293
return this.instance.wuid;
9394
}
9495

96+
public async loadWebhook() {
97+
const data = await this.prismaRepository.webhook.findUnique({
98+
where: {
99+
instanceId: this.instanceId,
100+
},
101+
});
102+
103+
this.localWebhook.enabled = data?.enabled;
104+
this.localWebhook.webhookBase64 = data?.webhookBase64;
105+
}
106+
95107
public async loadSettings() {
96108
const data = await this.prismaRepository.setting.findUnique({
97109
where: {
@@ -405,6 +417,7 @@ export class ChannelStartupService {
405417
messageTimestamp: true,
406418
instanceId: true,
407419
source: true,
420+
contextInfo: true,
408421
MessageUpdate: {
409422
select: {
410423
status: true,

0 commit comments

Comments
 (0)