Skip to content

Commit 161abf1

Browse files
committed
feat: send media with form-data
1 parent ec2d7e3 commit 161abf1

File tree

4 files changed

+8
-61
lines changed

4 files changed

+8
-61
lines changed

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { ChannelStartupService } from '@api/services/channel.service';
77
import { Events, wa } from '@api/types/wa.types';
88
import { Chatwoot, ConfigService, Openai } from '@config/env.config';
99
import { BadRequestException, InternalServerErrorException } from '@exceptions';
10-
import { deleteTempFile, getTempFile } from '@utils/getTempFile';
1110
import { isURL } from 'class-validator';
1211
import EventEmitter2 from 'eventemitter2';
1312
import mime from 'mime';
@@ -437,7 +436,7 @@ export class EvolutionStartupService extends ChannelStartupService {
437436
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
438437
const mediaData: SendMediaDto = { ...data };
439438

440-
if (file) mediaData.media = await getTempFile(file, this.instanceId);
439+
if (file) mediaData.media = file.buffer.toString('base64');
441440

442441
const message = await this.prepareMediaMessage(mediaData);
443442

@@ -455,8 +454,6 @@ export class EvolutionStartupService extends ChannelStartupService {
455454
isIntegration,
456455
);
457456

458-
if (file) await deleteTempFile(file, this.instanceId);
459-
460457
return mediaSent;
461458
}
462459

@@ -486,7 +483,7 @@ export class EvolutionStartupService extends ChannelStartupService {
486483
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
487484
const mediaData: SendAudioDto = { ...data };
488485

489-
if (file) mediaData.audio = await getTempFile(file, this.instanceId);
486+
if (file) mediaData.audio = file.buffer.toString('base64');
490487

491488
const message = await this.processAudio(mediaData.audio, data.number);
492489

@@ -504,8 +501,6 @@ export class EvolutionStartupService extends ChannelStartupService {
504501
isIntegration,
505502
);
506503

507-
if (file) await deleteTempFile(file, this.instanceId);
508-
509504
return audioSent;
510505
}
511506

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { ChannelStartupService } from '@api/services/channel.service';
2222
import { Events, wa } from '@api/types/wa.types';
2323
import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config';
2424
import { BadRequestException, InternalServerErrorException } from '@exceptions';
25-
import { deleteTempFile, getTempFile } from '@utils/getTempFile';
2625
import axios from 'axios';
2726
import { arrayUnique, isURL } from 'class-validator';
2827
import EventEmitter2 from 'eventemitter2';
@@ -1030,7 +1029,7 @@ export class BusinessStartupService extends ChannelStartupService {
10301029
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
10311030
const mediaData: SendMediaDto = { ...data };
10321031

1033-
if (file) mediaData.media = await getTempFile(file, this.instanceId);
1032+
if (file) mediaData.media = file.buffer.toString('base64');
10341033

10351034
const message = await this.prepareMediaMessage(mediaData);
10361035

@@ -1048,8 +1047,6 @@ export class BusinessStartupService extends ChannelStartupService {
10481047
isIntegration,
10491048
);
10501049

1051-
if (file) await deleteTempFile(file, this.instanceId);
1052-
10531050
return mediaSent;
10541051
}
10551052

@@ -1084,7 +1081,7 @@ export class BusinessStartupService extends ChannelStartupService {
10841081
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
10851082
const mediaData: SendAudioDto = { ...data };
10861083

1087-
if (file) mediaData.audio = await getTempFile(file, this.instanceId);
1084+
if (file) mediaData.audio = file.buffer.toString('base64');
10881085

10891086
const message = await this.processAudio(mediaData.audio, data.number);
10901087

@@ -1102,8 +1099,6 @@ export class BusinessStartupService extends ChannelStartupService {
11021099
isIntegration,
11031100
);
11041101

1105-
if (file) await deleteTempFile(file, this.instanceId);
1106-
11071102
return audioSent;
11081103
}
11091104

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import { BadRequestException, InternalServerErrorException, NotFoundException }
7070
import ffmpegPath from '@ffmpeg-installer/ffmpeg';
7171
import { Boom } from '@hapi/boom';
7272
import { Instance } from '@prisma/client';
73-
import { deleteTempFile, getTempFile } from '@utils/getTempFile';
7473
import { makeProxyAgent } from '@utils/makeProxyAgent';
7574
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
7675
import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma';
@@ -2270,16 +2269,14 @@ export class BaileysStartupService extends ChannelStartupService {
22702269
public async statusMessage(data: SendStatusDto, file?: any) {
22712270
const mediaData: SendStatusDto = { ...data };
22722271

2273-
if (file) mediaData.content = await getTempFile(file, this.instanceId);
2272+
if (file) mediaData.content = file.buffer.toString('base64');
22742273

22752274
const status = await this.formatStatusMessage(mediaData);
22762275

22772276
const statusSent = await this.sendMessageWithTyping('status@broadcast', {
22782277
status,
22792278
});
22802279

2281-
if (file) await deleteTempFile(file, this.instanceId);
2282-
22832280
return statusSent;
22842281
}
22852282

@@ -2407,7 +2404,7 @@ export class BaileysStartupService extends ChannelStartupService {
24072404
public async mediaSticker(data: SendStickerDto, file?: any) {
24082405
const mediaData: SendStickerDto = { ...data };
24092406

2410-
if (file) mediaData.sticker = await getTempFile(file, this.instanceId);
2407+
if (file) mediaData.sticker = file.buffer.toString('base64');
24112408

24122409
const convert = await this.convertToWebP(data.sticker);
24132410
const gifPlayback = data.sticker.includes('.gif');
@@ -2426,15 +2423,13 @@ export class BaileysStartupService extends ChannelStartupService {
24262423
},
24272424
);
24282425

2429-
if (file) await deleteTempFile(file, this.instanceId);
2430-
24312426
return result;
24322427
}
24332428

24342429
public async mediaMessage(data: SendMediaDto, file?: any, isIntegration = false) {
24352430
const mediaData: SendMediaDto = { ...data };
24362431

2437-
if (file) mediaData.media = await getTempFile(file, this.instanceId);
2432+
if (file) mediaData.media = file.buffer.toString('base64');
24382433

24392434
const generate = await this.prepareMediaMessage(mediaData);
24402435

@@ -2451,8 +2446,6 @@ export class BaileysStartupService extends ChannelStartupService {
24512446
isIntegration,
24522447
);
24532448

2454-
if (file) await deleteTempFile(file, this.instanceId);
2455-
24562449
return mediaSent;
24572450
}
24582451

@@ -2560,7 +2553,7 @@ export class BaileysStartupService extends ChannelStartupService {
25602553
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
25612554
const mediaData: SendAudioDto = { ...data };
25622555

2563-
if (file) mediaData.audio = await getTempFile(file, this.instanceId);
2556+
if (file) mediaData.audio = file.buffer.toString('base64');
25642557

25652558
if (!data?.encoding && data?.encoding !== false) {
25662559
data.encoding = true;
@@ -2581,8 +2574,6 @@ export class BaileysStartupService extends ChannelStartupService {
25812574
isIntegration,
25822575
);
25832576

2584-
if (file) await deleteTempFile(file, this.instanceId);
2585-
25862577
return result;
25872578
} else {
25882579
throw new InternalServerErrorException('Failed to convert audio');

src/utils/getTempFile.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)