From d87d7c07759254b818ae29267ced0a16e60c031c Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:07:57 -0300 Subject: [PATCH 1/6] fixed image mime type --- src/api/integrations/channel/meta/whatsapp.business.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 65c879614..1b1fada6c 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -339,7 +339,7 @@ export class BusinessStartupService extends ChannelStartupService { ? 'audio' : 'video'; - const mimetype = result.headers['content-type']; + const mimetype = result.data?.mime_type || result.headers['content-type']; const contentDisposition = result.headers['content-disposition']; let fileName = `${message.messages[0].id}.${mimetype.split('/')[1]}`; From 59383d5944b923d93e7d09a2bb9717db4c962254 Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:08:23 -0300 Subject: [PATCH 2/6] fixed remoteJid --- src/api/integrations/channel/meta/whatsapp.business.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 1b1fada6c..c847101a2 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -352,7 +352,7 @@ export class BusinessStartupService extends ChannelStartupService { const size = result.headers['content-length'] || buffer.data.byteLength; - const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName); + const fullName = join(`${this.instance.id}`, key.remoteJid, mediaType, fileName); await s3Service.uploadFile(fullName, buffer.data, size, { 'Content-Type': mimetype, From 5eaabfb1eb554b831aec30eab33bb37e8fc5d1d4 Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:08:52 -0300 Subject: [PATCH 3/6] fixed messageId --- .../integrations/channel/meta/whatsapp.business.service.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index c847101a2..db8b628a1 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -358,9 +358,13 @@ export class BusinessStartupService extends ChannelStartupService { 'Content-Type': mimetype, }); + const createdMessage = await this.prismaRepository.message.create({ + data: messageRaw, + }); + await this.prismaRepository.media.create({ data: { - messageId: received.messages[0].id, + messageId: createdMessage.id, instanceId: this.instanceId, type: mediaType, fileName: fullName, From e7ed1446eec34bb857410bd844f05d1c4bf8756c Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:09:27 -0300 Subject: [PATCH 4/6] saved base64 file in memory --- src/api/integrations/channel/meta/whatsapp.business.service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index db8b628a1..e348786a9 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -375,6 +375,7 @@ export class BusinessStartupService extends ChannelStartupService { const mediaUrl = await s3Service.getObjectUrl(fullName); messageRaw.message.mediaUrl = mediaUrl; + messageRaw.message.base64 = buffer.data.toString('base64'); } catch (error) { this.logger.error(['Error on upload file to minio', error?.message, error?.stack]); } From d797d8177ca5f36c10a8a5e60d868ea85a6d81cd Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:10:11 -0300 Subject: [PATCH 5/6] added isMediaMessage method --- .../channel/meta/whatsapp.business.service.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index e348786a9..452c1415b 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -70,6 +70,13 @@ export class BusinessStartupService extends ChannelStartupService { await this.closeClient(); } + private isMediaMessage(message: any) { + return message.document || + message.image || + message.audio || + message.video + } + private async post(message: any, params: string) { try { let urlServer = this.configService.get('WA_BUSINESS').URL; @@ -301,12 +308,7 @@ export class BusinessStartupService extends ChannelStartupService { remoteJid: this.phoneNumber, fromMe: received.messages[0].from === received.metadata.phone_number_id, }; - if ( - received?.messages[0].document || - received?.messages[0].image || - received?.messages[0].audio || - received?.messages[0].video - ) { + if (this.isMediaMessage(received?.messages[0])) { messageRaw = { key, pushName, From ef7574273c84c98aab1b1beec0c503a05db56aae Mon Sep 17 00:00:00 2001 From: Alan Cezar Date: Thu, 14 Nov 2024 09:11:21 -0300 Subject: [PATCH 6/6] message recorded for no media messages --- .../channel/meta/whatsapp.business.service.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 452c1415b..c44cbb18c 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -504,9 +504,11 @@ export class BusinessStartupService extends ChannelStartupService { } } - await this.prismaRepository.message.create({ - data: messageRaw, - }); + if (!this.isMediaMessage(received?.messages[0])) { + await this.prismaRepository.message.create({ + data: messageRaw, + }); + } const contact = await this.prismaRepository.contact.findFirst({ where: { instanceId: this.instanceId, remoteJid: key.remoteJid },