Skip to content

Commit

Permalink
fix: Updated media handling in WhatsApp Baileys and Typebot services
Browse files Browse the repository at this point in the history
This commit fixes issues with media handling in the WhatsApp Baileys and Typebot services. Specifically, it updates the way media messages are processed and stored. The changes include:

- Importing the `S3` class from the configuration in the Typebot service (`src/api/integrations/typebot/services/typebot.service.ts`).
- Modifying the `getTypeMessage` method in the Typebot service to handle media messages more efficiently.
- Refactoring the `BaileysStartupService` in the WhatsApp Baileys service (`src/api/services/channels/whatsapp.baileys.service.ts`) to handle media messages more consistently.

These changes improve the handling and storage of media messages, ensuring that they are processed correctly and efficiently.
  • Loading branch information
dgcode-tec committed Jul 14, 2024
1 parent e6916ac commit 484736f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 74 deletions.
17 changes: 11 additions & 6 deletions src/api/integrations/typebot/services/typebot.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Message, Typebot as TypebotModel, TypebotSession } from '@prisma/client';
import axios from 'axios';

import { ConfigService, Typebot } from '../../../../config/env.config';
import { ConfigService, S3, Typebot } from '../../../../config/env.config';
import { Logger } from '../../../../config/logger.config';
import { InstanceDto } from '../../../dto/instance.dto';
import { PrismaRepository } from '../../../repository/repository.service';
Expand Down Expand Up @@ -839,6 +839,11 @@ export class TypebotService {
}

private getTypeMessage(msg: any) {
let mediaId: string;

if (this.configService.get<S3>('S3').ENABLE) mediaId = msg.message.mediaUrl;
else mediaId = msg.key.id;

const types = {
conversation: msg?.message?.conversation,
extendedTextMessage: msg?.message?.extendedTextMessage?.text,
Expand All @@ -851,12 +856,12 @@ export class TypebotService {
listResponseMessage: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId,
responseRowId: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId,
// Medias
audioMessage: msg?.message?.audioMessage ? `audioMessage:${msg?.key?.id}` : undefined,
imageMessage: msg?.message?.imageMessage ? `imageMessage:${msg?.key?.id}` : undefined,
videoMessage: msg?.message?.videoMessage ? `videoMessage:${msg?.key?.id}` : undefined,
documentMessage: msg?.message?.documentMessage ? `documentMessage:${msg?.key?.id}` : undefined,
audioMessage: msg?.message?.audioMessage ? `audioMessage|${mediaId}` : undefined,
imageMessage: msg?.message?.imageMessage ? `imageMessage|${mediaId}` : undefined,
videoMessage: msg?.message?.videoMessage ? `videoMessage|${mediaId}` : undefined,
documentMessage: msg?.message?.documentMessage ? `documentMessage|${mediaId}` : undefined,
documentWithCaptionMessage: msg?.message?.auddocumentWithCaptionMessageioMessage
? `documentWithCaptionMessage:${msg?.key?.id}`
? `documentWithCaptionMessage|${mediaId}`
: undefined,
};

Expand Down
124 changes: 56 additions & 68 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,18 @@ export class BaileysStartupService extends ChannelStartupService {
return;
}

let messageRaw: any;
const contentMsg = received?.message[getContentType(received.message)] as any;

const messageRaw: any = {
key: received.key,
pushName: received.pushName,
message: { ...received.message },
contextInfo: contentMsg?.contextInfo,
messageType: getContentType(received.message),
messageTimestamp: received.messageTimestamp as number,
instanceId: this.instanceId,
source: getDevice(received.key.id),
};

const isMedia =
received?.message?.imageMessage ||
Expand All @@ -1046,45 +1057,6 @@ export class BaileysStartupService extends ChannelStartupService {
received?.message?.documentWithCaptionMessage ||
received?.message?.audioMessage;

const contentMsg = received?.message[getContentType(received.message)] as any;

if (this.localWebhook.webhookBase64 === true && !this.configService.get<S3>('S3').ENABLE && isMedia) {
const buffer = await downloadMediaMessage(
{ key: received.key, message: received?.message },
'buffer',
{},
{
logger: P({ level: 'error' }) as any,
reuploadRequest: this.client.updateMediaMessage,
},
);

messageRaw = {
key: received.key,
pushName: received.pushName,
message: {
...received.message,
base64: buffer ? buffer.toString('base64') : undefined,
},
contextInfo: contentMsg?.contextInfo,
messageType: getContentType(received.message),
messageTimestamp: received.messageTimestamp as number,
instanceId: this.instanceId,
source: getDevice(received.key.id),
};
} else {
messageRaw = {
key: received.key,
pushName: received.pushName,
message: { ...received.message },
contextInfo: contentMsg?.contextInfo,
messageType: getContentType(received.message),
messageTimestamp: received.messageTimestamp as number,
instanceId: this.instanceId,
source: getDevice(received.key.id),
};
}

if (this.localSettings.readMessages && received.key.id !== 'status@broadcast') {
await this.client.readMessages([received.key]);
}
Expand Down Expand Up @@ -1115,41 +1087,57 @@ export class BaileysStartupService extends ChannelStartupService {
data: messageRaw,
});

if (this.configService.get<S3>('S3').ENABLE && isMedia) {
try {
const message: any = received;
const media = await this.getBase64FromMediaMessage(
{
message,
},
true,
);
if (isMedia) {
if (this.configService.get<S3>('S3').ENABLE) {
try {
const message: any = received;
const media = await this.getBase64FromMediaMessage(
{
message,
},
true,
);

const { buffer, mediaType, fileName, size } = media;
const { buffer, mediaType, fileName, size } = media;

const mimetype = mime.lookup(fileName).toString();
const mimetype = mime.lookup(fileName).toString();

const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);
const fullName = join(`${this.instance.id}`, received.key.remoteJid, mediaType, fileName);

await s3Service.uploadFile(fullName, buffer, size.fileLength, {
'Content-Type': mimetype,
});
await s3Service.uploadFile(fullName, buffer, size.fileLength, {
'Content-Type': mimetype,
});

await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});
await this.prismaRepository.media.create({
data: {
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
});

const mediaUrl = await s3Service.getObjectUrl(fullName);
const mediaUrl = await s3Service.getObjectUrl(fullName);

messageRaw.message.mediaUrl = mediaUrl;
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
messageRaw.message.mediaUrl = mediaUrl;
} catch (error) {
this.logger.error(['Error on upload file to minio', error?.message, error?.stack]);
}
} else {
if (this.localWebhook.webhookBase64 === true) {
const buffer = await downloadMediaMessage(
{ key: received.key, message: received?.message },
'buffer',
{},
{
logger: P({ level: 'error' }) as any,
reuploadRequest: this.client.updateMediaMessage,
},
);

messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
}
}
}

Expand Down

0 comments on commit 484736f

Please sign in to comment.