@@ -6,7 +6,7 @@ import Jimp from 'jimp';
66import mimeTypes from 'mime-types' ;
77import path from 'path' ;
88
9- import { ConfigService } from '../../config/env.config' ;
9+ import { Chatwoot , ConfigService } from '../../config/env.config' ;
1010import { Logger } from '../../config/logger.config' ;
1111import { ROOT_DIR } from '../../config/path.config' ;
1212import { ChatwootDto } from '../dto/chatwoot.dto' ;
@@ -613,6 +613,8 @@ export class ChatwootService {
613613 conversationId : number ,
614614 content : string ,
615615 messageType : 'incoming' | 'outgoing' | undefined ,
616+ source_id ?: string ,
617+ source_reply_id ?: string ,
616618 privateMessage ?: boolean ,
617619 attachments ?: {
618620 content : unknown ;
@@ -638,6 +640,8 @@ export class ChatwootService {
638640 message_type : messageType ,
639641 attachments : attachments ,
640642 private : privateMessage || false ,
643+ source_id : source_id ,
644+ source_reply_id : source_reply_id ,
641645 } ,
642646 } ) ;
643647
@@ -733,6 +737,8 @@ export class ChatwootService {
733737 file : string ,
734738 messageType : 'incoming' | 'outgoing' | undefined ,
735739 content ?: string ,
740+ source_id ?: string ,
741+ source_reply_id ?: string ,
736742 ) {
737743 this . logger . verbose ( 'send data to chatwoot' ) ;
738744
@@ -749,6 +755,16 @@ export class ChatwootService {
749755 this . logger . verbose ( 'temp file found' ) ;
750756 data . append ( 'attachments[]' , createReadStream ( file ) ) ;
751757
758+ if ( source_id ) {
759+ this . logger . verbose ( 'source_id found' ) ;
760+ data . append ( 'source_id' , source_id ) ;
761+ }
762+
763+ if ( source_reply_id ) {
764+ this . logger . verbose ( 'source_reply_id found' ) ;
765+ data . append ( 'source_reply_id' , source_reply_id ) ;
766+ }
767+
752768 this . logger . verbose ( 'get client to instance: ' + this . provider . instanceName ) ;
753769 const config = {
754770 method : 'post' ,
@@ -914,10 +930,10 @@ export class ChatwootService {
914930 } ,
915931 } ;
916932
917- await waInstance ?. audioWhatsapp ( data ) ;
933+ const audioWhatsapp = await waInstance ?. audioWhatsapp ( data ) ;
918934
919935 this . logger . verbose ( 'audio sent' ) ;
920- return ;
936+ return audioWhatsapp ;
921937 }
922938
923939 this . logger . verbose ( 'send media to instance: ' + waInstance . instanceName ) ;
@@ -939,15 +955,48 @@ export class ChatwootService {
939955 data . mediaMessage . caption = caption ;
940956 }
941957
942- await waInstance ?. mediaMessage ( data ) ;
958+ const mediaMessage = await waInstance ?. mediaMessage ( data ) ;
943959
944960 this . logger . verbose ( 'media sent' ) ;
945- return ;
961+ return mediaMessage ;
946962 } catch ( error ) {
947963 this . logger . error ( error ) ;
948964 }
949965 }
950966
967+ public async updateMessage (
968+ instance : InstanceDto ,
969+ accountId : number ,
970+ conversationId : number ,
971+ messageId : number ,
972+ sourceId : string | null ,
973+ ) {
974+ // const useReplyId = this.configService.get<DelInstance>('DEL_INSTANCE');
975+ const useReplyId = this . configService . get < Chatwoot > ( 'CHATWOOT' ) ?. USE_REPLY_ID ;
976+ if ( useReplyId === true ) {
977+ this . logger . verbose ( 'update message to chatwoot instance: ' + instance . instanceName ) ;
978+ const client = await this . clientCw ( instance ) ;
979+
980+ if ( ! client ) {
981+ this . logger . warn ( 'client not found' ) ;
982+ return null ;
983+ }
984+ this . logger . verbose ( 'check if sourceId to update' ) ;
985+ if ( sourceId ) {
986+ this . logger . verbose ( 'update message to chatwoot' ) ;
987+ const dataUpdated = {
988+ source_id : sourceId ,
989+ } ;
990+ await client . messages . update ( {
991+ accountId,
992+ conversationId,
993+ data : dataUpdated ,
994+ messageId,
995+ } ) ;
996+ }
997+ }
998+ }
999+
9511000 public async receiveWebhook ( instance : InstanceDto , body : any ) {
9521001 try {
9531002// espera 500ms para evitar duplicidade de mensagens
@@ -1048,6 +1097,9 @@ export class ChatwootService {
10481097 }
10491098
10501099 for ( const message of body . conversation . messages ) {
1100+ const messageId = message ?. id ;
1101+ const conversationId = message ?. conversation_id ;
1102+ const accountId = message ?. account_id ;
10511103 this . logger . verbose ( 'check if message is media' ) ;
10521104 if ( message . attachments && message . attachments . length > 0 ) {
10531105 this . logger . verbose ( 'message is media' ) ;
@@ -1058,7 +1110,8 @@ export class ChatwootService {
10581110 formatText = null ;
10591111 }
10601112
1061- await this . sendAttachment ( waInstance , chatId , attachment . data_url , formatText ) ;
1113+ const mediaMessage = await this . sendAttachment ( waInstance , chatId , attachment . data_url , formatText ) ;
1114+ await this . updateMessage ( instance , accountId , conversationId , messageId , mediaMessage ?. key ?. id ) ;
10621115 }
10631116 } else {
10641117 this . logger . verbose ( 'message is text' ) ;
@@ -1075,7 +1128,8 @@ export class ChatwootService {
10751128 } ,
10761129 } ;
10771130
1078- await waInstance ?. textMessage ( data ) ;
1131+ const message = await waInstance ?. textMessage ( data ) ;
1132+ await this . updateMessage ( instance , accountId , conversationId , messageId , message ?. key ?. id ) ;
10791133 }
10801134 }
10811135 }
@@ -1164,6 +1218,38 @@ export class ChatwootService {
11641218 return types ;
11651219 }
11661220
1221+ private getContextIdTypeMessage ( msg : any ) {
1222+ this . logger . verbose ( 'get type message' ) ;
1223+
1224+ const types = {
1225+ conversation : msg . conversation ?. contextInfo ?. stanzaId ,
1226+ imageMessage : msg . imageMessage ?. contextInfo ?. stanzaId ,
1227+ videoMessage : msg . videoMessage ?. contextInfo ?. stanzaId ,
1228+ extendedTextMessage : msg . extendedTextMessage ?. contextInfo ?. stanzaId ,
1229+ messageContextInfo : msg . messageContextInfo ?. stanzaId ,
1230+ stickerMessage : undefined ,
1231+ documentMessage : msg . documentMessage ?. contextInfo ?. stanzaId ,
1232+ documentWithCaptionMessage : msg . documentWithCaptionMessage ?. message ?. documentMessage ?. contextInfo ?. stanzaId ,
1233+ audioMessage : msg . audioMessage ?. contextInfo ?. stanzaId ,
1234+ contactMessage : msg . contactMessage ?. contextInfo ?. stanzaId ,
1235+ contactsArrayMessage : msg . contactsArrayMessage ?. contextInfo ?. stanzaId ,
1236+ locationMessage : msg . locationMessage ?. contextInfo ?. stanzaId ,
1237+ liveLocationMessage : msg . liveLocationMessage ?. contextInfo ?. stanzaId ,
1238+ } ;
1239+
1240+ this . logger . verbose ( 'type message: ' + types ) ;
1241+
1242+ return types ;
1243+ }
1244+
1245+ private getContextMessageContent ( types : any ) {
1246+ this . logger . verbose ( 'get message context content' ) ;
1247+ const typeKey = Object . keys ( types ) . find ( ( key ) => types [ key ] !== undefined && types [ key ] !== '' ) ;
1248+
1249+ const result = typeKey ? types [ typeKey ] : undefined ;
1250+ return result ;
1251+ }
1252+
11671253 private getMessageContent ( types : any ) {
11681254 this . logger . verbose ( 'get message content' ) ;
11691255 const typeKey = Object . keys ( types ) . find ( ( key ) => types [ key ] !== undefined ) ;
@@ -1263,6 +1349,18 @@ export class ChatwootService {
12631349 return messageContent ;
12641350 }
12651351
1352+ private getContextConversationMessage ( msg : any ) {
1353+ this . logger . verbose ( 'get context conversation message' ) ;
1354+
1355+ const types = this . getContextIdTypeMessage ( msg ) ;
1356+
1357+ const messageContext = this . getContextMessageContent ( types ) ;
1358+
1359+ this . logger . verbose ( 'context conversation message: ' + messageContext ) ;
1360+
1361+ return messageContext ;
1362+ }
1363+
12661364 public async eventWhatsapp ( event : string , instance : InstanceDto , body : any ) {
12671365 this . logger . verbose ( 'event whatsapp to instance: ' + instance . instanceName ) ;
12681366 try {
@@ -1291,6 +1389,8 @@ export class ChatwootService {
12911389 this . logger . verbose ( 'get conversation message' ) ;
12921390 const bodyMessage = await this . getConversationMessage ( body . message ) ;
12931391
1392+ const source_reply_id = this . getContextConversationMessage ( body . message ) ;
1393+
12941394 const isMedia = this . isMediaMessage ( body . message ) ;
12951395
12961396 const adsMessage = this . getAdsMessage ( body . message ) ;
@@ -1310,6 +1410,8 @@ export class ChatwootService {
13101410
13111411 const messageType = body . key . fromMe ? 'outgoing' : 'incoming' ;
13121412
1413+ const source_id = body . key ?. id ;
1414+
13131415 this . logger . verbose ( 'message type: ' + messageType ) ;
13141416
13151417 this . logger . verbose ( 'is media: ' + isMedia ) ;
@@ -1354,7 +1456,7 @@ export class ChatwootService {
13541456 }
13551457
13561458 this . logger . verbose ( 'send data to chatwoot' ) ;
1357- const send = await this . sendData ( getConversation , fileName , messageType , content ) ;
1459+ const send = await this . sendData ( getConversation , fileName , messageType , content , source_id , source_reply_id ) ;
13581460
13591461 if ( ! send ) {
13601462 this . logger . warn ( 'message not sent' ) ;
@@ -1375,7 +1477,14 @@ export class ChatwootService {
13751477 this . logger . verbose ( 'message is not group' ) ;
13761478
13771479 this . logger . verbose ( 'send data to chatwoot' ) ;
1378- const send = await this . sendData ( getConversation , fileName , messageType , bodyMessage ) ;
1480+ const send = await this . sendData (
1481+ getConversation ,
1482+ fileName ,
1483+ messageType ,
1484+ bodyMessage ,
1485+ source_id ,
1486+ source_reply_id ,
1487+ ) ;
13791488
13801489 if ( ! send ) {
13811490 this . logger . warn ( 'message not sent' ) ;
@@ -1472,7 +1581,14 @@ export class ChatwootService {
14721581 }
14731582
14741583 this . logger . verbose ( 'send data to chatwoot' ) ;
1475- const send = await this . createMessage ( instance , getConversation , content , messageType ) ;
1584+ const send = await this . createMessage (
1585+ instance ,
1586+ getConversation ,
1587+ content ,
1588+ messageType ,
1589+ source_id ,
1590+ source_reply_id ,
1591+ ) ;
14761592
14771593 if ( ! send ) {
14781594 this . logger . warn ( 'message not sent' ) ;
@@ -1493,7 +1609,14 @@ export class ChatwootService {
14931609 this . logger . verbose ( 'message is not group' ) ;
14941610
14951611 this . logger . verbose ( 'send data to chatwoot' ) ;
1496- const send = await this . createMessage ( instance , getConversation , bodyMessage , messageType ) ;
1612+ const send = await this . createMessage (
1613+ instance ,
1614+ getConversation ,
1615+ bodyMessage ,
1616+ messageType ,
1617+ source_id ,
1618+ source_reply_id ,
1619+ ) ;
14971620
14981621 if ( ! send ) {
14991622 this . logger . warn ( 'message not sent' ) ;
0 commit comments