Skip to content

Commit 0f7c243

Browse files
committed
fix: status on send message
1 parent 1f6473c commit 0f7c243

File tree

5 files changed

+64
-74
lines changed

5 files changed

+64
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 2.1.2 (2024-10-06 09:47)
1+
# 2.1.2 (develop)
22

33
### Features
44

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

Lines changed: 17 additions & 29 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

@@ -484,10 +472,10 @@ export class EvolutionStartupService extends ChannelStartupService {
484472
const mediaData: SendAudioDto = { ...data };
485473

486474
if (file?.buffer) {
487-
mediaData.audio = file.buffer.toString('base64');
475+
mediaData.audio = file.buffer.toString('base64');
488476
} else {
489-
console.error("El archivo o buffer no está definido correctamente.");
490-
throw new Error("File or buffer is undefined.");
477+
console.error('El archivo o buffer no est� definido correctamente.');
478+
throw new Error('File or buffer is undefined.');
491479
}
492480

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

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

Lines changed: 5 additions & 5 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

@@ -1082,11 +1083,10 @@ export class BusinessStartupService extends ChannelStartupService {
10821083
const mediaData: SendAudioDto = { ...data };
10831084

10841085
if (file?.buffer) {
1085-
// Asegurarse de que file y buffer existen antes de usarlos
1086-
mediaData.audio = file.buffer.toString('base64');
1086+
mediaData.audio = file.buffer.toString('base64');
10871087
} else {
1088-
console.error('El archivo no tiene buffer o file es undefined');
1089-
throw new Error('File or buffer is undefined');
1088+
console.error('El archivo no tiene buffer o file es undefined');
1089+
throw new Error('File or buffer is undefined');
10901090
}
10911091

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

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

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import { Boom } from '@hapi/boom';
7272
import { Instance } from '@prisma/client';
7373
import { makeProxyAgent } from '@utils/makeProxyAgent';
7474
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
75+
import { status } from '@utils/renderStatus';
7576
import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma';
7677
import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files';
7778
import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db';
@@ -569,7 +570,6 @@ export class BaileysStartupService extends ChannelStartupService {
569570
const isGroupJid = this.localSettings.groupsIgnore && isJidGroup(jid);
570571
const isBroadcast = !this.localSettings.readStatus && isJidBroadcast(jid);
571572
const isNewsletter = isJidNewsletter(jid);
572-
// const isNewsletter = jid && jid.includes('newsletter');
573573

574574
return isGroupJid || isBroadcast || isNewsletter;
575575
},
@@ -1230,14 +1230,6 @@ export class BaileysStartupService extends ChannelStartupService {
12301230
},
12311231

12321232
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
1233-
const status: Record<number, wa.StatusMessage> = {
1234-
0: 'ERROR',
1235-
1: 'PENDING',
1236-
2: 'SERVER_ACK',
1237-
3: 'DELIVERY_ACK',
1238-
4: 'READ',
1239-
5: 'PLAYED',
1240-
};
12411233
for await (const { key, update } of args) {
12421234
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
12431235
return;
@@ -2575,46 +2567,46 @@ export class BaileysStartupService extends ChannelStartupService {
25752567
const mediaData: SendAudioDto = { ...data };
25762568

25772569
if (file?.buffer) {
2578-
mediaData.audio = file.buffer.toString('base64');
2570+
mediaData.audio = file.buffer.toString('base64');
25792571
} else if (!isURL(data.audio) && !isBase64(data.audio)) {
2580-
console.error('Invalid file or audio source');
2581-
throw new BadRequestException('File buffer, URL, or base64 audio is required');
2572+
console.error('Invalid file or audio source');
2573+
throw new BadRequestException('File buffer, URL, or base64 audio is required');
25822574
}
25832575

25842576
if (!data?.encoding && data?.encoding !== false) {
2585-
data.encoding = true;
2577+
data.encoding = true;
25862578
}
25872579

25882580
if (data?.encoding) {
2589-
const convert = await this.processAudio(mediaData.audio);
2590-
2591-
if (Buffer.isBuffer(convert)) {
2592-
const result = this.sendMessageWithTyping<AnyMessageContent>(
2593-
data.number,
2594-
{
2595-
audio: convert,
2596-
ptt: true,
2597-
mimetype: 'audio/ogg; codecs=opus',
2598-
},
2599-
{ presence: 'recording', delay: data?.delay },
2600-
isIntegration,
2601-
);
2581+
const convert = await this.processAudio(mediaData.audio);
26022582

2603-
return result;
2604-
} else {
2605-
throw new InternalServerErrorException('Failed to convert audio');
2606-
}
2583+
if (Buffer.isBuffer(convert)) {
2584+
const result = this.sendMessageWithTyping<AnyMessageContent>(
2585+
data.number,
2586+
{
2587+
audio: convert,
2588+
ptt: true,
2589+
mimetype: 'audio/ogg; codecs=opus',
2590+
},
2591+
{ presence: 'recording', delay: data?.delay },
2592+
isIntegration,
2593+
);
2594+
2595+
return result;
2596+
} else {
2597+
throw new InternalServerErrorException('Failed to convert audio');
2598+
}
26072599
}
26082600

26092601
return await this.sendMessageWithTyping<AnyMessageContent>(
2610-
data.number,
2611-
{
2612-
audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'),
2613-
ptt: true,
2614-
mimetype: 'audio/ogg; codecs=opus',
2615-
},
2616-
{ presence: 'recording', delay: data?.delay },
2617-
isIntegration,
2602+
data.number,
2603+
{
2604+
audio: isURL(data.audio) ? { url: data.audio } : Buffer.from(data.audio, 'base64'),
2605+
ptt: true,
2606+
mimetype: 'audio/ogg; codecs=opus',
2607+
},
2608+
{ presence: 'recording', delay: data?.delay },
2609+
isIntegration,
26182610
);
26192611
}
26202612

@@ -3659,7 +3651,7 @@ export class BaileysStartupService extends ChannelStartupService {
36593651
const messageRaw = {
36603652
key: message.key,
36613653
pushName: message.pushName,
3662-
status: message.status,
3654+
status: status[message.status],
36633655
message: { ...message.message },
36643656
contextInfo: contentMsg?.contextInfo,
36653657
messageType: contentType || 'unknown',

src/utils/renderStatus.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { wa } from '@api/types/wa.types';
2+
3+
export const status: Record<number, wa.StatusMessage> = {
4+
0: 'ERROR',
5+
1: 'PENDING',
6+
2: 'SERVER_ACK',
7+
3: 'DELIVERY_ACK',
8+
4: 'READ',
9+
5: 'PLAYED',
10+
};

0 commit comments

Comments
 (0)