Skip to content

Commit 2565b93

Browse files
committed
fix: test duplicate message media in groups chatwoot
1 parent 7e88a90 commit 2565b93

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

src/whatsapp/services/chatwoot.service.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ export class ChatwootService {
389389
conversationId: number,
390390
content: string,
391391
messageType: 'incoming' | 'outgoing' | undefined,
392-
privateMessage?: boolean,
393392
attachments?: {
394393
content: unknown;
395394
encoding: string;
@@ -405,7 +404,6 @@ export class ChatwootService {
405404
content: content,
406405
message_type: messageType,
407406
attachments: attachments,
408-
private: privateMessage,
409407
},
410408
});
411409

@@ -416,7 +414,6 @@ export class ChatwootService {
416414
instance: InstanceDto,
417415
content: string,
418416
messageType: 'incoming' | 'outgoing' | undefined,
419-
privateMessage?: boolean,
420417
attachments?: {
421418
content: unknown;
422419
encoding: string;
@@ -450,7 +447,6 @@ export class ChatwootService {
450447
content: content,
451448
message_type: messageType,
452449
attachments: attachments,
453-
private: privateMessage,
454450
},
455451
});
456452

@@ -461,7 +457,6 @@ export class ChatwootService {
461457
conversationId: number,
462458
file: string,
463459
messageType: 'incoming' | 'outgoing' | undefined,
464-
privateMessage: boolean,
465460
content?: string,
466461
) {
467462
const data = new FormData();
@@ -472,8 +467,6 @@ export class ChatwootService {
472467

473468
data.append('message_type', messageType);
474469

475-
data.append('private', privateMessage);
476-
477470
data.append('attachments[]', createReadStream(file));
478471

479472
const config = {
@@ -489,7 +482,6 @@ export class ChatwootService {
489482

490483
try {
491484
const { data } = await axios.request(config);
492-
493485
unlinkSync(file);
494486
return data;
495487
} catch (error) {
@@ -648,7 +640,6 @@ export class ChatwootService {
648640
instance,
649641
`🚨 Instância ${body.inbox.name} já está conectada.`,
650642
'incoming',
651-
false,
652643
);
653644
}
654645
}
@@ -661,7 +652,6 @@ export class ChatwootService {
661652
instance,
662653
`⚠️ Instância ${body.inbox.name} não existe.`,
663654
'incoming',
664-
false,
665655
);
666656
}
667657

@@ -670,15 +660,14 @@ export class ChatwootService {
670660
instance,
671661
`⚠️ Status da instância ${body.inbox.name}: *${state}*`,
672662
'incoming',
673-
false,
674663
);
675664
}
676665
}
677666

678667
if (command === 'desconectar') {
679668
const msgLogout = `🚨 Desconectando Whatsapp da caixa de entrada *${body.inbox.name}*: `;
680669

681-
await this.createBotMessage(instance, msgLogout, 'incoming', false);
670+
await this.createBotMessage(instance, msgLogout, 'incoming');
682671
await waInstance?.client?.logout('Log out instance: ' + instance.instanceName);
683672
await waInstance?.client?.ws?.close();
684673
}
@@ -853,24 +842,37 @@ export class ChatwootService {
853842

854843
const fileName = `${path.join(waInstance?.storePath, 'temp', `${nameFile}`)}`;
855844

856-
writeFileSync(fileName, fileData);
845+
writeFileSync(fileName, fileData, 'utf8');
857846

858-
if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) {
847+
if (body.key.remoteJid.includes('@g.us')) {
859848
const participantName = body.pushName;
860849

861-
const content = `**${participantName}**\n\n${bodyMessage}`;
850+
let content: string;
851+
852+
if (!body.key.fromMe) {
853+
content = `**${participantName}**\n\n${bodyMessage}`;
854+
} else {
855+
content = `${bodyMessage}`;
856+
}
857+
862858
const send = await this.sendData(
863859
getConversion,
864860
fileName,
865861
messageType,
866-
false,
867862
content,
868863
);
869864

870865
if (!send) {
871866
return;
872867
}
873868

869+
this.messageCacheFile = path.join(
870+
ROOT_DIR,
871+
'store',
872+
'chatwoot',
873+
`${instance.instanceName}_cache.txt`,
874+
);
875+
874876
this.messageCache = this.loadMessageCache();
875877

876878
this.messageCache.add(send.id.toString());
@@ -883,14 +885,20 @@ export class ChatwootService {
883885
getConversion,
884886
fileName,
885887
messageType,
886-
false,
887888
bodyMessage,
888889
);
889890

890891
if (!send) {
891892
return;
892893
}
893894

895+
this.messageCacheFile = path.join(
896+
ROOT_DIR,
897+
'store',
898+
'chatwoot',
899+
`${instance.instanceName}_cache.txt`,
900+
);
901+
894902
this.messageCache = this.loadMessageCache();
895903

896904
this.messageCache.add(send.id.toString());
@@ -901,19 +909,28 @@ export class ChatwootService {
901909
}
902910
}
903911

904-
if (body.key.remoteJid.includes('@g.us') && !body.key.fromMe) {
912+
if (body.key.remoteJid.includes('@g.us')) {
905913
const participantName = body.pushName;
906914

907-
const content = `**${participantName}**\n\n${bodyMessage}`;
915+
let content: string;
916+
917+
if (!body.key.fromMe) {
918+
content = `**${participantName}**\n\n${bodyMessage}`;
919+
} else {
920+
content = `${bodyMessage}`;
921+
}
908922

909923
const send = await this.createMessage(
910924
instance,
911925
getConversion,
912926
content,
913927
messageType,
914-
false,
915928
);
916929

930+
if (!send) {
931+
return;
932+
}
933+
917934
this.messageCacheFile = path.join(
918935
ROOT_DIR,
919936
'store',
@@ -934,9 +951,12 @@ export class ChatwootService {
934951
getConversion,
935952
bodyMessage,
936953
messageType,
937-
false,
938954
);
939955

956+
if (!send) {
957+
return;
958+
}
959+
940960
this.messageCacheFile = path.join(
941961
ROOT_DIR,
942962
'store',
@@ -949,6 +969,7 @@ export class ChatwootService {
949969
this.messageCache.add(send.id.toString());
950970

951971
this.saveMessageCache();
972+
952973
return send;
953974
}
954975
}
@@ -962,13 +983,13 @@ export class ChatwootService {
962983
}
963984

964985
const msgStatus = `⚡️ Status da instância ${inbox.name}: ${data.status}`;
965-
await this.createBotMessage(instance, msgStatus, 'incoming', false);
986+
await this.createBotMessage(instance, msgStatus, 'incoming');
966987
}
967988

968989
if (event === 'connection.update') {
969990
if (body.state === 'open') {
970991
const msgConnection = `🚀 Conexão realizada com sucesso!`;
971-
await this.createBotMessage(instance, msgConnection, 'incoming', false);
992+
await this.createBotMessage(instance, msgConnection, 'incoming');
972993
}
973994
}
974995

@@ -993,7 +1014,7 @@ export class ChatwootService {
9931014
if (event === 'qrcode.updated') {
9941015
if (body.statusCode === 500) {
9951016
const erroQRcode = `🚨 Limite de geração de QRCode atingido, para gerar um novo QRCode, envie a mensagem /iniciar novamente.`;
996-
return await this.createBotMessage(instance, erroQRcode, 'incoming', false);
1017+
return await this.createBotMessage(instance, erroQRcode, 'incoming');
9971018
} else {
9981019
const fileData = Buffer.from(
9991020
body?.qrcode.base64.replace('data:image/png;base64,', ''),

0 commit comments

Comments
 (0)