Fixes media messaging saving and uploading to S3 on WPP Biz API

This commit is contained in:
Júlio Paulillo 2024-09-03 20:18:26 -03:00
parent df1c805ef0
commit acb4268bc9

View File

@ -300,12 +300,13 @@ export class BusinessStartupService extends ChannelStartupService {
remoteJid: this.phoneNumber,
fromMe: received.messages[0].from === received.metadata.phone_number_id,
};
if (
const isMedia =
received?.messages[0].document ||
received?.messages[0].image ||
received?.messages[0].audio ||
received?.messages[0].video
) {
received?.messages[0].video;
messageRaw = {
key,
pushName,
@ -317,11 +318,17 @@ export class BusinessStartupService extends ChannelStartupService {
instanceId: this.instanceId,
};
if (this.configService.get<Database>('DATABASE').SAVE_DATA.NEW_MESSAGE) {
const msg = await this.prismaRepository.message.create({
data: messageRaw,
});
if (isMedia) {
if (this.configService.get<S3>('S3').ENABLE) {
try {
const message: any = received;
const id = message.messages[0][message.messages[0].type].id;
const id = messageRaw.message[messageRaw.messageType].id;
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
urlServer = `${urlServer}/${version}/${id}`;
@ -338,7 +345,7 @@ export class BusinessStartupService extends ChannelStartupService {
? 'audio'
: 'video';
const mimetype = result.headers['content-type'];
const mimetype = result.data.mime_type;
const contentDisposition = result.headers['content-disposition'];
let fileName = `${message.messages[0].id}.${mimetype.split('/')[1]}`;
@ -359,17 +366,17 @@ export class BusinessStartupService extends ChannelStartupService {
await this.prismaRepository.media.create({
data: {
messageId: received.messages[0].id,
messageId: msg.id,
instanceId: this.instanceId,
type: mediaType,
fileName: fullName,
mimetype,
},
mimetype
}
});
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]);
}
@ -378,6 +385,7 @@ export class BusinessStartupService extends ChannelStartupService {
messageRaw.message.base64 = buffer.toString('base64');
}
}
} else if (received?.messages[0].interactive) {
messageRaw = {
key,