diff --git a/src/whatsapp/controllers/sendMessage.controller.ts b/src/whatsapp/controllers/sendMessage.controller.ts index d23ba345..91792d8d 100644 --- a/src/whatsapp/controllers/sendMessage.controller.ts +++ b/src/whatsapp/controllers/sendMessage.controller.ts @@ -1,4 +1,4 @@ -import { isBase64, isURL } from 'class-validator'; +import { isBase64, isEmpty, isURL } from 'class-validator'; import { Logger } from '../../config/logger.config'; import { BadRequestException } from '../../exceptions'; @@ -46,10 +46,10 @@ export class SendMessageController { } logger.verbose('isURL: ' + isURL(data?.mediaMessage?.media) + ', isBase64: ' + isBase64(data?.mediaMessage?.media)); - if (isURL(data?.mediaMessage?.media) || isBase64(data?.mediaMessage?.media)) { + if (!isEmpty(data?.mediaMessage?.media)) { return await this.waMonitor.waInstances[instanceName].mediaMessage(data); } - throw new BadRequestException('Owned media must be a url or base64'); + throw new BadRequestException('Owned media must be a url or base64 or file'); } public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto) { diff --git a/src/whatsapp/services/whatsapp.business.service.ts b/src/whatsapp/services/whatsapp.business.service.ts index 0855106a..ef20f6e5 100644 --- a/src/whatsapp/services/whatsapp.business.service.ts +++ b/src/whatsapp/services/whatsapp.business.service.ts @@ -895,24 +895,28 @@ export class BusinessStartupService extends WAStartupService { return res; } - private async getIdMedia(mediaMessage: any) { + private async getIdMedia(mediaMessage: any, mimetype: string) { const integration = await this.findIntegration(); const formData = new FormData(); - const fileBuffer = await fs.readFile(mediaMessage.media); - - const fileBlob = new Blob([fileBuffer], { type: mediaMessage.mimetype }); - formData.append('file', fileBlob); - formData.append('typeFile', mediaMessage.mimetype); + formData.append('file', fileBuffer, { filename: mediaMessage.fileName }); + formData.append('typeFile', mimetype); formData.append('messaging_product', 'whatsapp'); const headers = { Authorization: `Bearer ${integration.token}` }; + let urlServer = this.configService.get('WA_BUSINESS').URL; + const version = this.configService.get('WA_BUSINESS').VERSION; + urlServer = `${urlServer}/${version}/${integration.number}/media`; + try { const res = await axios.post( - process.env.API_URL + '/' + process.env.VERSION + '/' + integration.number + '/media', + urlServer, formData, - { headers }, + { headers } ); return res.data.id; + } catch (error) { + console.error('Erro ao enviar o arquivo:', error); + } } protected async prepareMediaMessage(mediaMessage: MediaMessage) { @@ -957,7 +961,7 @@ export class BusinessStartupService extends WAStartupService { prepareMedia.type = 'link'; } else { mimetype = getMIMEType(mediaMessage.fileName); - const id = await this.getIdMedia(prepareMedia); + const id = await this.getIdMedia(prepareMedia, mimetype); prepareMedia.id = id; prepareMedia.type = 'id'; } @@ -1001,7 +1005,7 @@ export class BusinessStartupService extends WAStartupService { prepareMedia.type = 'link'; } else { mimetype = getMIMEType(prepareMedia.fileName); - const id = await this.getIdMedia(prepareMedia); + const id = await this.getIdMedia(prepareMedia, mimetype); prepareMedia.id = id; prepareMedia.type = 'id'; }