@@ -389,6 +389,7 @@ export class ChatwootService {
389389 conversationId : number ,
390390 content : string ,
391391 messageType : 'incoming' | 'outgoing' | undefined ,
392+ privateMessage ?: boolean ,
392393 attachments ?: {
393394 content : unknown ;
394395 encoding : string ;
@@ -404,6 +405,7 @@ export class ChatwootService {
404405 content : content ,
405406 message_type : messageType ,
406407 attachments : attachments ,
408+ private : privateMessage ,
407409 } ,
408410 } ) ;
409411
@@ -414,6 +416,7 @@ export class ChatwootService {
414416 instance : InstanceDto ,
415417 content : string ,
416418 messageType : 'incoming' | 'outgoing' | undefined ,
419+ privateMessage ?: boolean ,
417420 attachments ?: {
418421 content : unknown ;
419422 encoding : string ;
@@ -436,13 +439,18 @@ export class ChatwootService {
436439 conversation ?. meta ?. sender ?. id === contact . id && conversation . status === 'open' ,
437440 ) ;
438441
442+ if ( ! conversation ) {
443+ return ;
444+ }
445+
439446 const message = await client . messages . create ( {
440447 accountId : this . provider . account_id ,
441448 conversationId : conversation . id ,
442449 data : {
443450 content : content ,
444451 message_type : messageType ,
445452 attachments : attachments ,
453+ private : privateMessage ,
446454 } ,
447455 } ) ;
448456
@@ -453,6 +461,7 @@ export class ChatwootService {
453461 conversationId : number ,
454462 file : string ,
455463 messageType : 'incoming' | 'outgoing' | undefined ,
464+ privateMessage : boolean ,
456465 content ?: string ,
457466 ) {
458467 const data = new FormData ( ) ;
@@ -463,6 +472,8 @@ export class ChatwootService {
463472
464473 data . append ( 'message_type' , messageType ) ;
465474
475+ data . append ( 'private' , privateMessage ) ;
476+
466477 data . append ( 'attachments[]' , createReadStream ( file ) ) ;
467478
468479 const config = {
@@ -478,10 +489,12 @@ export class ChatwootService {
478489
479490 try {
480491 const { data } = await axios . request ( config ) ;
492+
481493 unlinkSync ( file ) ;
482494 return data ;
483495 } catch ( error ) {
484496 console . log ( error ) ;
497+ unlinkSync ( file ) ;
485498 }
486499 }
487500
@@ -635,6 +648,7 @@ export class ChatwootService {
635648 instance ,
636649 `🚨 Instância ${ body . inbox . name } já está conectada.` ,
637650 'incoming' ,
651+ false ,
638652 ) ;
639653 }
640654 }
@@ -647,6 +661,7 @@ export class ChatwootService {
647661 instance ,
648662 `⚠️ Instância ${ body . inbox . name } não existe.` ,
649663 'incoming' ,
664+ false ,
650665 ) ;
651666 }
652667
@@ -655,14 +670,15 @@ export class ChatwootService {
655670 instance ,
656671 `⚠️ Status da instância ${ body . inbox . name } : *${ state } *` ,
657672 'incoming' ,
673+ false ,
658674 ) ;
659675 }
660676 }
661677
662678 if ( command === 'desconectar' ) {
663679 const msgLogout = `🚨 Desconectando Whatsapp da caixa de entrada *${ body . inbox . name } *: ` ;
664680
665- await this . createBotMessage ( instance , msgLogout , 'incoming' ) ;
681+ await this . createBotMessage ( instance , msgLogout , 'incoming' , false ) ;
666682 await waInstance ?. client ?. logout ( 'Log out instance: ' + instance . instanceName ) ;
667683 await waInstance ?. client ?. ws ?. close ( ) ;
668684 }
@@ -819,7 +835,7 @@ export class ChatwootService {
819835
820836 const bodyMessage = await this . getConversationMessage ( body . message ) ;
821837
822- if ( ! bodyMessage ) {
838+ if ( ! bodyMessage && ! isMedia ) {
823839 return ;
824840 }
825841
@@ -835,25 +851,57 @@ export class ChatwootService {
835851
836852 const fileData = Buffer . from ( downloadBase64 . base64 , 'base64' ) ;
837853
838- const fileName = `${ path . join (
839- waInstance ?. storePath ,
840- 'chatwoot' ,
841- `${ nameFile } ` ,
842- ) } `;
854+ const fileName = `${ path . join ( waInstance ?. storePath , 'temp' , `${ nameFile } ` ) } ` ;
843855
844- writeFileSync ( fileName , fileData , 'utf8' ) ;
856+ writeFileSync ( fileName , fileData ) ;
845857
846858 if ( body . key . remoteJid . includes ( '@g.us' ) && ! body . key . fromMe ) {
847859 const participantName = body . pushName ;
848860
849861 const content = `**${ participantName } **\n\n${ bodyMessage } ` ;
850- return await this . sendData ( getConversion , fileName , messageType , content ) ;
862+ const send = await this . sendData (
863+ getConversion ,
864+ fileName ,
865+ messageType ,
866+ false ,
867+ content ,
868+ ) ;
869+
870+ if ( ! send ) {
871+ return ;
872+ }
873+
874+ this . messageCache = this . loadMessageCache ( ) ;
875+
876+ this . messageCache . add ( send . id . toString ( ) ) ;
877+
878+ this . saveMessageCache ( ) ;
879+
880+ return send ;
851881 } else {
852- return await this . sendData ( getConversion , fileName , messageType , bodyMessage ) ;
882+ const send = await this . sendData (
883+ getConversion ,
884+ fileName ,
885+ messageType ,
886+ false ,
887+ bodyMessage ,
888+ ) ;
889+
890+ if ( ! send ) {
891+ return ;
892+ }
893+
894+ this . messageCache = this . loadMessageCache ( ) ;
895+
896+ this . messageCache . add ( send . id . toString ( ) ) ;
897+
898+ this . saveMessageCache ( ) ;
899+
900+ return send ;
853901 }
854902 }
855903
856- if ( body . key . remoteJid . includes ( '@g.us' ) ) {
904+ if ( body . key . remoteJid . includes ( '@g.us' ) && ! body . key . fromMe ) {
857905 const participantName = body . pushName ;
858906
859907 const content = `**${ participantName } **\n\n${ bodyMessage } ` ;
@@ -863,6 +911,7 @@ export class ChatwootService {
863911 getConversion ,
864912 content ,
865913 messageType ,
914+ false ,
866915 ) ;
867916
868917 this . messageCacheFile = path . join (
@@ -885,6 +934,7 @@ export class ChatwootService {
885934 getConversion ,
886935 bodyMessage ,
887936 messageType ,
937+ false ,
888938 ) ;
889939
890940 this . messageCacheFile = path . join (
@@ -899,7 +949,6 @@ export class ChatwootService {
899949 this . messageCache . add ( send . id . toString ( ) ) ;
900950
901951 this . saveMessageCache ( ) ;
902-
903952 return send ;
904953 }
905954 }
@@ -913,13 +962,13 @@ export class ChatwootService {
913962 }
914963
915964 const msgStatus = `⚡️ Status da instância ${ inbox . name } : ${ data . status } ` ;
916- await this . createBotMessage ( instance , msgStatus , 'incoming' ) ;
965+ await this . createBotMessage ( instance , msgStatus , 'incoming' , false ) ;
917966 }
918967
919968 if ( event === 'connection.update' ) {
920969 if ( body . state === 'open' ) {
921970 const msgConnection = `🚀 Conexão realizada com sucesso!` ;
922- await this . createBotMessage ( instance , msgConnection , 'incoming' ) ;
971+ await this . createBotMessage ( instance , msgConnection , 'incoming' , false ) ;
923972 }
924973 }
925974
@@ -944,7 +993,7 @@ export class ChatwootService {
944993 if ( event === 'qrcode.updated' ) {
945994 if ( body . statusCode === 500 ) {
946995 const erroQRcode = `🚨 Limite de geração de QRCode atingido, para gerar um novo QRCode, envie a mensagem /iniciar novamente.` ;
947- return await this . createBotMessage ( instance , erroQRcode , 'incoming' ) ;
996+ return await this . createBotMessage ( instance , erroQRcode , 'incoming' , false ) ;
948997 } else {
949998 const fileData = Buffer . from (
950999 body ?. qrcode . base64 . replace ( 'data:image/png;base64,' , '' ) ,
0 commit comments