Skip to content

Commit f3b04d8

Browse files
committed
Merge branch 'release/2.1.2'
2 parents c4ebacb + 5846c34 commit f3b04d8

File tree

8 files changed

+71
-33
lines changed

8 files changed

+71
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Sync lost messages on chatwoot
66
* Set the maximum number of listeners that can be registered for events
7+
* Now is possible send medias with form-data
78

89
### Fixed
910

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "evolution-api-lite",
3-
"version": "2.1.2-lite",
3+
"version": "2.1.2",
44
"description": "Rest api for communication with WhatsApp",
55
"main": "./dist/main.js",
66
"type": "commonjs",
@@ -71,6 +71,7 @@
7171
"link-preview-js": "^3.0.4",
7272
"long": "^5.2.3",
7373
"mime": "^3.0.0",
74+
"multer": "^1.4.5-lts.1",
7475
"node-cache": "^5.1.2",
7576
"pino": "^8.11.0",
7677
"qrcode": "^1.5.1",

src/@types/express.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Multer } from 'multer';
2+
3+
declare global {
4+
namespace Express {
5+
interface Request {
6+
file?: Multer.File;
7+
}
8+
}
9+
}

src/api/controllers/sendMessage.controller.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@ export class SendMessageController {
2828
return await this.waMonitor.waInstances[instanceName].textMessage(data);
2929
}
3030

31-
public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto) {
31+
public async sendMedia({ instanceName }: InstanceDto, data: SendMediaDto, file?: any) {
3232
if (isBase64(data?.media) && !data?.fileName && data?.mediatype === 'document') {
3333
throw new BadRequestException('For base64 the file name must be informed.');
3434
}
3535

36-
if (isURL(data?.media) || isBase64(data?.media)) {
37-
return await this.waMonitor.waInstances[instanceName].mediaMessage(data);
36+
if (file || isURL(data?.media) || isBase64(data?.media)) {
37+
return await this.waMonitor.waInstances[instanceName].mediaMessage(data, file);
3838
}
3939
throw new BadRequestException('Owned media must be a url or base64');
4040
}
4141

42-
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto) {
43-
if (isURL(data.sticker) || isBase64(data.sticker)) {
44-
return await this.waMonitor.waInstances[instanceName].mediaSticker(data);
42+
public async sendSticker({ instanceName }: InstanceDto, data: SendStickerDto, file?: any) {
43+
if (file || isURL(data.sticker) || isBase64(data.sticker)) {
44+
return await this.waMonitor.waInstances[instanceName].mediaSticker(data, file);
4545
}
4646
throw new BadRequestException('Owned media must be a url or base64');
4747
}
4848

49-
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto) {
50-
if (isURL(data.audio) || isBase64(data.audio)) {
51-
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data);
49+
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {
50+
if (file || isURL(data.audio) || isBase64(data.audio)) {
51+
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
5252
}
5353
throw new BadRequestException('Owned media must be a url or base64');
5454
}
@@ -80,7 +80,7 @@ export class SendMessageController {
8080
return await this.waMonitor.waInstances[instanceName].pollMessage(data);
8181
}
8282

83-
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto) {
84-
return await this.waMonitor.waInstances[instanceName].statusMessage(data);
83+
public async sendStatus({ instanceName }: InstanceDto, data: SendStatusDto, file?: any) {
84+
return await this.waMonitor.waInstances[instanceName].statusMessage(data, file);
8585
}
8686
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,8 +1853,12 @@ export class BaileysStartupService extends ChannelStartupService {
18531853
throw new BadRequestException('Type not found');
18541854
}
18551855

1856-
public async statusMessage(data: SendStatusDto) {
1857-
const status = await this.formatStatusMessage(data);
1856+
public async statusMessage(data: SendStatusDto, file?: any) {
1857+
const mediaData: SendStatusDto = { ...data };
1858+
1859+
if (file) mediaData.content = file.buffer.toString('base64');
1860+
1861+
const status = await this.formatStatusMessage(mediaData);
18581862

18591863
return await this.sendMessageWithTyping('status@broadcast', {
18601864
status,
@@ -1982,8 +1986,12 @@ export class BaileysStartupService extends ChannelStartupService {
19821986
}
19831987
}
19841988

1985-
public async mediaSticker(data: SendStickerDto) {
1986-
const convert = await this.convertToWebP(data.sticker);
1989+
public async mediaSticker(data: SendStickerDto, file?: any) {
1990+
const mediaData: SendStickerDto = { ...data };
1991+
1992+
if (file) mediaData.sticker = file.buffer.toString('base64');
1993+
1994+
const convert = await this.convertToWebP(mediaData.sticker);
19871995
const gifPlayback = data.sticker.includes('.gif');
19881996
const result = await this.sendMessageWithTyping(
19891997
data.number,
@@ -2003,8 +2011,12 @@ export class BaileysStartupService extends ChannelStartupService {
20032011
return result;
20042012
}
20052013

2006-
public async mediaMessage(data: SendMediaDto) {
2007-
const generate = await this.prepareMediaMessage(data);
2014+
public async mediaMessage(data: SendMediaDto, file?: any) {
2015+
const mediaData: SendMediaDto = { ...data };
2016+
2017+
if (file) mediaData.media = file.buffer.toString('base64');
2018+
2019+
const generate = await this.prepareMediaMessage(mediaData);
20082020

20092021
return await this.sendMessageWithTyping(
20102022
data.number,
@@ -2019,15 +2031,19 @@ export class BaileysStartupService extends ChannelStartupService {
20192031
);
20202032
}
20212033

2022-
public async audioWhatsapp(data: SendAudioDto) {
2034+
public async audioWhatsapp(data: SendAudioDto, file?: any) {
2035+
const mediaData: SendAudioDto = { ...data };
2036+
2037+
if (file) mediaData.audio = file.buffer.toString('base64');
2038+
20232039
if (!data?.encoding && data?.encoding !== false) {
20242040
data.encoding = true;
20252041
}
20262042

20272043
return await this.sendMessageWithTyping<AnyMessageContent>(
20282044
data.number,
20292045
{
2030-
audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'),
2046+
audio: isURL(mediaData.audio) ? { url: mediaData.audio } : Buffer.from(mediaData.audio, 'base64'),
20312047
ptt: true,
20322048
mimetype: 'audio/ogg; codecs=opus',
20332049
},

src/api/routes/sendMessage.router.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ import {
2929
textMessageSchema,
3030
} from '@validate/validate.schema';
3131
import { RequestHandler, Router } from 'express';
32+
import multer from 'multer';
3233

3334
import { HttpStatus } from './index.router';
3435

36+
const upload = multer({ storage: multer.memoryStorage() });
37+
3538
export class MessageRouter extends RouterBroker {
3639
constructor(...guards: RequestHandler[]) {
3740
super();
@@ -56,43 +59,51 @@ export class MessageRouter extends RouterBroker {
5659

5760
return res.status(HttpStatus.CREATED).json(response);
5861
})
59-
.post(this.routerPath('sendMedia'), ...guards, async (req, res) => {
62+
.post(this.routerPath('sendMedia'), ...guards, upload.single('file'), async (req, res) => {
63+
const bodyData = req.body;
64+
6065
const response = await this.dataValidate<SendMediaDto>({
6166
request: req,
6267
schema: mediaMessageSchema,
6368
ClassRef: SendMediaDto,
64-
execute: (instance, data) => sendMessageController.sendMedia(instance, data),
69+
execute: (instance) => sendMessageController.sendMedia(instance, bodyData, req.file as any),
6570
});
6671

6772
return res.status(HttpStatus.CREATED).json(response);
6873
})
69-
.post(this.routerPath('sendWhatsAppAudio'), ...guards, async (req, res) => {
74+
.post(this.routerPath('sendWhatsAppAudio'), ...guards, upload.single('file'), async (req, res) => {
75+
const bodyData = req.body;
76+
7077
const response = await this.dataValidate<SendAudioDto>({
7178
request: req,
7279
schema: audioMessageSchema,
7380
ClassRef: SendMediaDto,
74-
execute: (instance, data) => sendMessageController.sendWhatsAppAudio(instance, data),
81+
execute: (instance) => sendMessageController.sendWhatsAppAudio(instance, bodyData, req.file as any),
7582
});
7683

7784
return res.status(HttpStatus.CREATED).json(response);
7885
})
7986
// TODO: Revisar funcionamento do envio de Status
80-
.post(this.routerPath('sendStatus'), ...guards, async (req, res) => {
87+
.post(this.routerPath('sendStatus'), ...guards, upload.single('file'), async (req, res) => {
88+
const bodyData = req.body;
89+
8190
const response = await this.dataValidate<SendStatusDto>({
8291
request: req,
8392
schema: statusMessageSchema,
8493
ClassRef: SendStatusDto,
85-
execute: (instance, data) => sendMessageController.sendStatus(instance, data),
94+
execute: (instance) => sendMessageController.sendStatus(instance, bodyData, req.file as any),
8695
});
8796

8897
return res.status(HttpStatus.CREATED).json(response);
8998
})
90-
.post(this.routerPath('sendSticker'), ...guards, async (req, res) => {
99+
.post(this.routerPath('sendSticker'), ...guards, upload.single('file'), async (req, res) => {
100+
const bodyData = req.body;
101+
91102
const response = await this.dataValidate<SendStickerDto>({
92103
request: req,
93104
schema: stickerMessageSchema,
94105
ClassRef: SendStickerDto,
95-
execute: (instance, data) => sendMessageController.sendSticker(instance, data),
106+
execute: (instance) => sendMessageController.sendSticker(instance, bodyData, req.file as any),
96107
});
97108

98109
return res.status(HttpStatus.CREATED).json(response);

src/config/logger.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class Logger {
8484
if (configService.get<Log>('LOG').COLOR) {
8585
console.log(
8686
/*Command.UNDERSCORE +*/ Command.BRIGHT + Level[type],
87-
'[Evolution API]',
87+
'[Evolution API Lite]',
8888
Command.BRIGHT + Color[type],
8989
this.instance ? `[${this.instance}]` : '',
9090
Command.BRIGHT + Color[type],
@@ -110,7 +110,7 @@ export class Logger {
110110
typeValue === 'object' ? console.log(/*Level.DARK,*/ value, '\n') : '';
111111
} else {
112112
console.log(
113-
'[Evolution API]',
113+
'[Evolution API Lite]',
114114
this.instance ? `[${this.instance}]` : '',
115115
process.pid.toString(),
116116
'-',

src/validate/message.schema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const mediaMessageSchema: JSONSchema7 = {
108108
},
109109
},
110110
},
111-
required: ['number', 'mediatype', 'media'],
111+
required: ['number', 'mediatype'],
112112
};
113113

114114
export const audioMessageSchema: JSONSchema7 = {
@@ -134,7 +134,7 @@ export const audioMessageSchema: JSONSchema7 = {
134134
},
135135
},
136136
},
137-
required: ['number', 'audio'],
137+
required: ['number'],
138138
};
139139

140140
export const statusMessageSchema: JSONSchema7 = {
@@ -158,7 +158,7 @@ export const statusMessageSchema: JSONSchema7 = {
158158
},
159159
allContacts: { type: 'boolean', enum: [true, false] },
160160
},
161-
required: ['type', 'content'],
161+
required: ['type'],
162162
};
163163

164164
export const stickerMessageSchema: JSONSchema7 = {

0 commit comments

Comments
 (0)