@@ -750,11 +750,15 @@ export class ChatwootService {
750750 this . logger . verbose ( 'temp file found' ) ;
751751 data . append ( 'attachments[]' , createReadStream ( file ) ) ;
752752
753- this . logger . verbose ( 'message context: ' + source_id ) ;
754- data . append ( 'source_id' , source_id ) ;
753+ if ( source_id ) {
754+ this . logger . verbose ( 'source_id found' ) ;
755+ data . append ( 'source_id' , source_id ) ;
756+ }
755757
756- this . logger . verbose ( 'message reply context: ' + source_reply_id ) ;
757- data . append ( 'source_reply_id' , source_reply_id ) ;
758+ if ( source_reply_id ) {
759+ this . logger . verbose ( 'source_reply_id found' ) ;
760+ data . append ( 'source_reply_id' , source_reply_id ) ;
761+ }
758762
759763 this . logger . verbose ( 'get client to instance: ' + this . provider . instanceName ) ;
760764 const config = {
@@ -955,6 +959,35 @@ export class ChatwootService {
955959 }
956960 }
957961
962+ public async updateMessage (
963+ instance : InstanceDto ,
964+ accountId : number ,
965+ conversationId : number ,
966+ messageId : number ,
967+ sourceId : string | null ,
968+ ) {
969+ this . logger . verbose ( 'update message to chatwoot instance: ' + instance . instanceName ) ;
970+ const client = await this . clientCw ( instance ) ;
971+
972+ if ( ! client ) {
973+ this . logger . warn ( 'client not found' ) ;
974+ return null ;
975+ }
976+ this . logger . verbose ( 'check if sourceId to update' ) ;
977+ if ( sourceId ) {
978+ this . logger . verbose ( 'update message to chatwoot' ) ;
979+ const dataUpdated = {
980+ source_id : sourceId ,
981+ } ;
982+ await client . messages . update ( {
983+ accountId,
984+ conversationId,
985+ data : dataUpdated ,
986+ messageId,
987+ } ) ;
988+ }
989+ }
990+
958991 public async receiveWebhook ( instance : InstanceDto , body : any ) {
959992 try {
960993 this . logger . verbose ( 'receive webhook to chatwoot instance: ' + instance . instanceName ) ;
@@ -1052,6 +1085,9 @@ export class ChatwootService {
10521085 }
10531086
10541087 for ( const message of body . conversation . messages ) {
1088+ const messageId = message ?. id ;
1089+ const conversationId = message ?. conversation_id ;
1090+ const accountId = message ?. account_id ;
10551091 this . logger . verbose ( 'check if message is media' ) ;
10561092 if ( message . attachments && message . attachments . length > 0 ) {
10571093 this . logger . verbose ( 'message is media' ) ;
@@ -1062,7 +1098,8 @@ export class ChatwootService {
10621098 formatText = null ;
10631099 }
10641100
1065- await this . sendAttachment ( waInstance , chatId , attachment . data_url , formatText ) ;
1101+ const mediaMessage = await this . sendAttachment ( waInstance , chatId , attachment . data_url , formatText ) ;
1102+ await this . updateMessage ( instance , accountId , conversationId , messageId , mediaMessage ?. key ?. id ) ;
10661103 }
10671104 } else {
10681105 this . logger . verbose ( 'message is text' ) ;
@@ -1080,17 +1117,7 @@ export class ChatwootService {
10801117 } ;
10811118
10821119 const message = await waInstance ?. textMessage ( data ) ;
1083- const conversationId = body ?. conversation ?. id ;
1084- const messageId = body ?. id ;
1085- const dataUpdated = {
1086- source_id : message . key . id ,
1087- } ;
1088- await client . messages . update ( {
1089- accountId : this . provider . account_id ,
1090- conversationId,
1091- data : dataUpdated ,
1092- messageId,
1093- } ) ;
1120+ await this . updateMessage ( instance , accountId , conversationId , messageId , message ?. key ?. id ) ;
10941121 }
10951122 }
10961123 }
@@ -1191,7 +1218,7 @@ export class ChatwootService {
11911218
11921219 private getContextMessageContent ( types : any ) {
11931220 this . logger . verbose ( 'get message context content' ) ;
1194- const typeKey = Object . keys ( types ) . find ( ( key ) => types [ key ] !== undefined ) ;
1221+ const typeKey = Object . keys ( types ) . find ( ( key ) => types [ key ] !== undefined && types [ key ] !== '' ) ;
11951222
11961223 const result = typeKey ? types [ typeKey ] : undefined ;
11971224 return result ;
@@ -1301,11 +1328,11 @@ export class ChatwootService {
13011328
13021329 const types = this . getContextIdTypeMessage ( msg ) ;
13031330
1304- const messageContent = this . getContextMessageContent ( types ) ;
1331+ const messageContext = this . getContextMessageContent ( types ) ;
13051332
1306- this . logger . verbose ( 'context conversation message: ' + messageContent ) ;
1333+ this . logger . verbose ( 'context conversation message: ' + messageContext ) ;
13071334
1308- return messageContent ;
1335+ return messageContext ;
13091336 }
13101337
13111338 public async eventWhatsapp ( event : string , instance : InstanceDto , body : any ) {
0 commit comments