mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-28 19:36:27 -06:00
Feature to send local file media
This commit is contained in:
parent
8366e7c1b2
commit
83134fe590
@ -1,4 +1,4 @@
|
|||||||
import { isBase64, isURL } from 'class-validator';
|
import { isBase64, isEmpty, isURL } from 'class-validator';
|
||||||
|
|
||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { BadRequestException } from '../../exceptions';
|
import { BadRequestException } from '../../exceptions';
|
||||||
@ -46,10 +46,10 @@ export class SendMessageController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logger.verbose('isURL: ' + isURL(data?.mediaMessage?.media) + ', isBase64: ' + isBase64(data?.mediaMessage?.media));
|
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);
|
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) {
|
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto) {
|
||||||
|
@ -895,24 +895,28 @@ export class BusinessStartupService extends WAStartupService {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getIdMedia(mediaMessage: any) {
|
private async getIdMedia(mediaMessage: any, mimetype: string) {
|
||||||
const integration = await this.findIntegration();
|
const integration = await this.findIntegration();
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
const fileBuffer = await fs.readFile(mediaMessage.media);
|
const fileBuffer = await fs.readFile(mediaMessage.media);
|
||||||
|
formData.append('file', fileBuffer, { filename: mediaMessage.fileName });
|
||||||
const fileBlob = new Blob([fileBuffer], { type: mediaMessage.mimetype });
|
formData.append('typeFile', mimetype);
|
||||||
formData.append('file', fileBlob);
|
|
||||||
formData.append('typeFile', mediaMessage.mimetype);
|
|
||||||
formData.append('messaging_product', 'whatsapp');
|
formData.append('messaging_product', 'whatsapp');
|
||||||
const headers = { Authorization: `Bearer ${integration.token}` };
|
const headers = { Authorization: `Bearer ${integration.token}` };
|
||||||
|
let urlServer = this.configService.get<WaBusiness>('WA_BUSINESS').URL;
|
||||||
|
const version = this.configService.get<WaBusiness>('WA_BUSINESS').VERSION;
|
||||||
|
urlServer = `${urlServer}/${version}/${integration.number}/media`;
|
||||||
|
try {
|
||||||
const res = await axios.post(
|
const res = await axios.post(
|
||||||
process.env.API_URL + '/' + process.env.VERSION + '/' + integration.number + '/media',
|
urlServer,
|
||||||
formData,
|
formData,
|
||||||
{ headers },
|
{ headers }
|
||||||
);
|
);
|
||||||
return res.data.id;
|
return res.data.id;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao enviar o arquivo:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async prepareMediaMessage(mediaMessage: MediaMessage) {
|
protected async prepareMediaMessage(mediaMessage: MediaMessage) {
|
||||||
@ -957,7 +961,7 @@ export class BusinessStartupService extends WAStartupService {
|
|||||||
prepareMedia.type = 'link';
|
prepareMedia.type = 'link';
|
||||||
} else {
|
} else {
|
||||||
mimetype = getMIMEType(mediaMessage.fileName);
|
mimetype = getMIMEType(mediaMessage.fileName);
|
||||||
const id = await this.getIdMedia(prepareMedia);
|
const id = await this.getIdMedia(prepareMedia, mimetype);
|
||||||
prepareMedia.id = id;
|
prepareMedia.id = id;
|
||||||
prepareMedia.type = 'id';
|
prepareMedia.type = 'id';
|
||||||
}
|
}
|
||||||
@ -1001,7 +1005,7 @@ export class BusinessStartupService extends WAStartupService {
|
|||||||
prepareMedia.type = 'link';
|
prepareMedia.type = 'link';
|
||||||
} else {
|
} else {
|
||||||
mimetype = getMIMEType(prepareMedia.fileName);
|
mimetype = getMIMEType(prepareMedia.fileName);
|
||||||
const id = await this.getIdMedia(prepareMedia);
|
const id = await this.getIdMedia(prepareMedia, mimetype);
|
||||||
prepareMedia.id = id;
|
prepareMedia.id = id;
|
||||||
prepareMedia.type = 'id';
|
prepareMedia.type = 'id';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user