Skip to content

Commit 070840d

Browse files
authored
Merge branch 'EvolutionAPI:v2.0.0' into v2.0.0
2 parents 0af0058 + 29a1d99 commit 070840d

11 files changed

Lines changed: 70 additions & 55 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
# 2.1.2 (develop)
1+
# 2.1.3 (develop)
2+
3+
### Fixed
4+
5+
* Fixed prefilledVariables in startTypebot
6+
7+
# 2.1.2 (2024-10-06 10:09)
28

39
### Features
410

prisma/mysql-schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ model Message {
159159
MessageUpdate MessageUpdate[]
160160
Media Media?
161161
webhookUrl String? @db.VarChar(500)
162-
status Int? @db.Int
162+
status String? @db.VarChar(30)
163163
164164
sessionId String?
165165
session IntegrationSession? @relation(fields: [sessionId], references: [id])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Message" ALTER COLUMN "status" SET DATA TYPE VARCHAR(30);

prisma/postgresql-schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ model Message {
158158
MessageUpdate MessageUpdate[]
159159
Media Media?
160160
webhookUrl String? @db.VarChar(500)
161-
status Int? @db.Integer
161+
status String? @db.VarChar(30)
162162
163163
sessionId String?
164164
session IntegrationSession? @relation(fields: [sessionId], references: [id])

src/api/controllers/sendMessage.controller.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ export class SendMessageController {
4848
}
4949

5050
public async sendWhatsAppAudio({ instanceName }: InstanceDto, data: SendAudioDto, file?: any) {
51-
if (file || isURL(data.audio) || isBase64(data.audio)) {
52-
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
51+
if (file?.buffer || isURL(data.audio) || isBase64(data.audio)) {
52+
// Si file existe y tiene buffer, o si es una URL o Base64, continúa
53+
return await this.waMonitor.waInstances[instanceName].audioWhatsapp(data, file);
54+
} else {
55+
console.error('El archivo no tiene buffer o el audio no es una URL o Base64 válida');
56+
throw new BadRequestException('Owned media must be a url, base64, or valid file with buffer');
5357
}
54-
throw new BadRequestException('Owned media must be a url or base64');
55-
}
58+
}
5659

5760
public async sendButtons({ instanceName }: InstanceDto, data: SendButtonDto) {
5861
return await this.waMonitor.waInstances[instanceName].buttonMessage(data);

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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 { status } from '@utils/renderStatus';
1011
import { isURL } from 'class-validator';
1112
import EventEmitter2 from 'eventemitter2';
1213
import mime from 'mime';
@@ -273,72 +274,59 @@ export class EvolutionStartupService extends ChannelStartupService {
273274

274275
const messageId = v4();
275276

276-
let messageRaw: any;
277+
let messageRaw: any = {
278+
key: { fromMe: true, id: messageId, remoteJid: number },
279+
messageTimestamp: Math.round(new Date().getTime() / 1000),
280+
webhookUrl,
281+
source: 'unknown',
282+
instanceId: this.instanceId,
283+
status: status[1],
284+
};
277285

278286
if (message?.mediaType === 'image') {
279287
messageRaw = {
280-
key: { fromMe: true, id: messageId, remoteJid: number },
288+
...messageRaw,
281289
message: {
282290
mediaUrl: message.media,
283291
quoted,
284292
},
285293
messageType: 'imageMessage',
286-
messageTimestamp: Math.round(new Date().getTime() / 1000),
287-
webhookUrl,
288-
source: 'unknown',
289-
instanceId: this.instanceId,
290294
};
291295
} else if (message?.mediaType === 'video') {
292296
messageRaw = {
293-
key: { fromMe: true, id: messageId, remoteJid: number },
297+
...messageRaw,
294298
message: {
295299
mediaUrl: message.media,
296300
quoted,
297301
},
298302
messageType: 'videoMessage',
299-
messageTimestamp: Math.round(new Date().getTime() / 1000),
300-
webhookUrl,
301-
source: 'unknown',
302-
instanceId: this.instanceId,
303303
};
304304
} else if (message?.mediaType === 'audio') {
305305
messageRaw = {
306-
key: { fromMe: true, id: messageId, remoteJid: number },
306+
...messageRaw,
307307
message: {
308308
mediaUrl: message.media,
309309
quoted,
310310
},
311311
messageType: 'audioMessage',
312-
messageTimestamp: Math.round(new Date().getTime() / 1000),
313-
webhookUrl,
314-
source: 'unknown',
315-
instanceId: this.instanceId,
316312
};
317313
} else if (message?.mediaType === 'document') {
318314
messageRaw = {
319-
key: { fromMe: true, id: messageId, remoteJid: number },
315+
...messageRaw,
320316
message: {
321317
mediaUrl: message.media,
322318
quoted,
323319
},
324320
messageType: 'documentMessage',
325-
messageTimestamp: Math.round(new Date().getTime() / 1000),
326-
webhookUrl,
327-
source: 'unknown',
328-
instanceId: this.instanceId,
329321
};
330322
} else {
331323
messageRaw = {
332-
key: { fromMe: true, id: messageId, remoteJid: number },
324+
...messageRaw,
333325
message: {
334326
...message,
335327
quoted,
336328
},
337329
messageType: 'conversation',
338-
messageTimestamp: Math.round(new Date().getTime() / 1000),
339-
webhookUrl,
340-
source: 'unknown',
341-
instanceId: this.instanceId,
342330
};
343331
}
344332

@@ -483,7 +471,12 @@ export class EvolutionStartupService extends ChannelStartupService {
483471
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
484472
const mediaData: SendAudioDto = { ...data };
485473

486-
if (file) mediaData.audio = file.buffer.toString('base64');
474+
if (file?.buffer) {
475+
mediaData.audio = file.buffer.toString('base64');
476+
} else {
477+
console.error('El archivo o buffer no est� definido correctamente.');
478+
throw new Error('File or buffer is undefined.');
479+
}
487480

488481
const message = await this.processAudio(mediaData.audio, data.number);
489482

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ 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 { status } from '@utils/renderStatus';
2526
import axios from 'axios';
2627
import { arrayUnique, isURL } from 'class-validator';
2728
import EventEmitter2 from 'eventemitter2';
@@ -895,12 +896,12 @@ export class BusinessStartupService extends ChannelStartupService {
895896

896897
const messageRaw: any = {
897898
key: { fromMe: true, id: messageSent?.messages[0]?.id, remoteJid: this.createJid(number) },
898-
//pushName: messageSent.pushName,
899899
message: this.convertMessageToRaw(message, content),
900900
messageType: this.renderMessageType(content.type),
901901
messageTimestamp: (messageSent?.messages[0]?.timestamp as number) || Math.round(new Date().getTime() / 1000),
902902
instanceId: this.instanceId,
903903
webhookUrl,
904+
status: status[1],
904905
source: 'unknown',
905906
};
906907

@@ -1081,7 +1082,12 @@ export class BusinessStartupService extends ChannelStartupService {
10811082
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
10821083
const mediaData: SendAudioDto = { ...data };
10831084

1084-
if (file) mediaData.audio = file.buffer.toString('base64');
1085+
if (file?.buffer) {
1086+
mediaData.audio = file.buffer.toString('base64');
1087+
} else {
1088+
console.error('El archivo no tiene buffer o file es undefined');
1089+
throw new Error('File or buffer is undefined');
1090+
}
10851091

10861092
const message = await this.processAudio(mediaData.audio, data.number);
10871093

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import { Boom } from '@hapi/boom';
7373
import { Instance } from '@prisma/client';
7474
import { makeProxyAgent } from '@utils/makeProxyAgent';
7575
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
76+
import { status } from '@utils/renderStatus';
7677
import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma';
7778
import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files';
7879
import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db';
@@ -570,7 +571,6 @@ export class BaileysStartupService extends ChannelStartupService {
570571
const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid);
571572
const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid);
572573
const isNewsletter = isJidNewsletter(jid);
573-
// const isNewsletter = jid && jid.includes('newsletter');
574574

575575
return isGroupJid || isBroadcast || isNewsletter;
576576
},
@@ -1231,14 +1231,6 @@ export class BaileysStartupService extends ChannelStartupService {
12311231
},
12321232

12331233
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
1234-
const status: Record<number, wa.StatusMessage> = {
1235-
0: 'ERROR',
1236-
1: 'PENDING',
1237-
2: 'SERVER_ACK',
1238-
3: 'DELIVERY_ACK',
1239-
4: 'READ',
1240-
5: 'PLAYED',
1241-
};
12421234
for await (const { key, update } of args) {
12431235
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
12441236
return;
@@ -2590,7 +2582,12 @@ export class BaileysStartupService extends ChannelStartupService {
25902582
public async audioWhatsapp(data: SendAudioDto, file?: any, isIntegration = false) {
25912583
const mediaData: SendAudioDto = { ...data };
25922584

2593-
if (file) mediaData.audio = file.buffer.toString('base64');
2585+
if (file?.buffer) {
2586+
mediaData.audio = file.buffer.toString('base64');
2587+
} else if (!isURL(data.audio) && !isBase64(data.audio)) {
2588+
console.error('Invalid file or audio source');
2589+
throw new BadRequestException('File buffer, URL, or base64 audio is required');
2590+
}
25942591

25952592
if (!data?.encoding && data?.encoding !== false) {
25962593
data.encoding = true;
@@ -3670,7 +3667,7 @@ export class BaileysStartupService extends ChannelStartupService {
36703667
const messageRaw = {
36713668
key: message.key,
36723669
pushName: message.pushName,
3673-
status: message.status,
3670+
status: status[message.status],
36743671
message: { ...message.message },
36753672
contextInfo: contentMsg?.contextInfo,
36763673
messageType: contentType || 'unknown',

src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TypebotService } from '@api/integrations/chatbot/typebot/services/typeb
55
import { PrismaRepository } from '@api/repository/repository.service';
66
import { WAMonitoringService } from '@api/services/monitor.service';
77
import { Events } from '@api/types/wa.types';
8-
import { Auth, configService, HttpServer, Typebot } from '@config/env.config';
8+
import { configService, Typebot } from '@config/env.config';
99
import { Logger } from '@config/logger.config';
1010
import { BadRequestException } from '@exceptions';
1111
import { Typebot as TypebotModel } from '@prisma/client';
@@ -609,13 +609,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr
609609
}
610610
}
611611

612-
const prefilledVariables = {
613-
remoteJid: remoteJid,
614-
instanceName: instance.instanceName,
615-
serverUrl: configService.get<HttpServer>('SERVER').URL,
616-
apiKey: configService.get<Auth>('AUTHENTICATION').API_KEY.KEY,
617-
ownerJid: instanceData.number,
618-
};
612+
const prefilledVariables: any = {};
619613

620614
if (variables?.length) {
621615
variables.forEach((variable: { name: string | number; value: string }) => {
@@ -674,6 +668,7 @@ export class TypebotController extends ChatbotController implements ChatbotContr
674668
stopBotFromMe,
675669
keepOpen,
676670
'init',
671+
prefilledVariables,
677672
);
678673

679674
// const response = await this.typebotService.createNewSession(instanceData, {

src/api/integrations/chatbot/typebot/services/typebot.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ export class TypebotService {
356356
stopBotFromMe: boolean,
357357
keepOpen: boolean,
358358
content: string,
359+
prefilledVariables?: any,
359360
) {
360361
if (session && expire && expire > 0) {
361362
const now = Date.now();
@@ -397,6 +398,7 @@ export class TypebotService {
397398
remoteJid: remoteJid,
398399
pushName: msg.pushName,
399400
botId: findTypebot.id,
401+
prefilledVariables: prefilledVariables,
400402
});
401403

402404
if (data.session) {
@@ -524,6 +526,7 @@ export class TypebotService {
524526
remoteJid: remoteJid,
525527
pushName: msg?.pushName,
526528
botId: findTypebot.id,
529+
prefilledVariables: prefilledVariables,
527530
});
528531

529532
if (data?.session) {

0 commit comments

Comments
 (0)