@@ -949,7 +949,7 @@ export class ChatwootService {
949949 return message ;
950950 }
951951
952- private async sendData (
952+ public async sendData (
953953 conversationId : number ,
954954 fileStream : Readable ,
955955 fileName : string ,
@@ -959,36 +959,33 @@ export class ChatwootService {
959959 messageBody ?: any ,
960960 sourceId ?: string ,
961961 quotedMsg ?: MessageModel ,
962+ retryCount = 0 ,
963+ maxRetries = 1 ,
962964 ) {
963965 if ( sourceId && this . isImportHistoryAvailable ( ) ) {
964966 const messageAlreadySaved = await chatwootImport . getExistingSourceIds ( [ sourceId ] , conversationId ) ;
965- if ( messageAlreadySaved ) {
966- if ( messageAlreadySaved . size > 0 ) {
967- this . logger . warn ( 'Message already saved on chatwoot' ) ;
968- return null ;
969- }
967+ if ( messageAlreadySaved && messageAlreadySaved . size > 0 ) {
968+ this . logger . warn ( 'Message already saved on chatwoot' ) ;
969+ return null ;
970970 }
971971 }
972+
972973 const data = new FormData ( ) ;
973974
974975 if ( content ) {
975976 data . append ( 'content' , content ) ;
976977 }
977978
978979 data . append ( 'message_type' , messageType ) ;
979-
980980 data . append ( 'attachments[]' , fileStream , { filename : fileName } ) ;
981981
982982 const sourceReplyId = quotedMsg ?. chatwootMessageId || null ;
983983
984984 if ( messageBody && instance ) {
985985 const replyToIds = await this . getReplyToIds ( messageBody , instance ) ;
986-
987986 if ( replyToIds . in_reply_to || replyToIds . in_reply_to_external_id ) {
988- const content = JSON . stringify ( {
989- ...replyToIds ,
990- } ) ;
991- data . append ( 'content_attributes' , content ) ;
987+ const contentAttributes = JSON . stringify ( { ...replyToIds } ) ;
988+ data . append ( 'content_attributes' , contentAttributes ) ;
992989 }
993990 }
994991
@@ -1013,10 +1010,25 @@ export class ChatwootService {
10131010
10141011 try {
10151012 const { data } = await axios . request ( config ) ;
1016-
10171013 return data ;
10181014 } catch ( error ) {
1019- this . logger . error ( error ) ;
1015+ if (
1016+ error . response &&
1017+ error . response . status === 404 &&
1018+ error . response . data . error === 'Resource could not be found' &&
1019+ retryCount < maxRetries
1020+ ) {
1021+ const remoteJid = messageBody . key . remoteJid ;
1022+ const cacheKey = `${ instance . instanceName } :createConversation-${ remoteJid } ` ;
1023+ this . logger . verbose ( `Conversation not found. Deleting cache key: ${ cacheKey } ` ) ;
1024+ await this . cache . delete ( cacheKey ) ;
1025+
1026+ this . logger . verbose ( `Retrying eventWhatsapp for ${ remoteJid } , attempt ${ retryCount + 1 } of ${ maxRetries } ` ) ;
1027+ return await this . eventWhatsapp ( 'messages.upsert' , instance , messageBody , retryCount + 1 ) ;
1028+ } else {
1029+ this . logger . error ( `Failed to send message: ${ error } ` ) ;
1030+ return null ;
1031+ }
10201032 }
10211033 }
10221034
@@ -1864,7 +1876,7 @@ export class ChatwootService {
18641876 return messageContent ;
18651877 }
18661878
1867- public async eventWhatsapp ( event : string , instance : InstanceDto , body : any ) {
1879+ public async eventWhatsapp ( event : string , instance : InstanceDto , body : any , retryCount : number = 0 ) {
18681880 try {
18691881 const waInstance = this . waMonitor . waInstances [ instance . instanceName ] ;
18701882
@@ -2030,6 +2042,7 @@ export class ChatwootService {
20302042 body ,
20312043 'WAID:' + body . key . id ,
20322044 quotedMsg ,
2045+ retryCount ,
20332046 ) ;
20342047
20352048 if ( ! send ) {
@@ -2049,6 +2062,7 @@ export class ChatwootService {
20492062 body ,
20502063 'WAID:' + body . key . id ,
20512064 quotedMsg ,
2065+ retryCount ,
20522066 ) ;
20532067
20542068 if ( ! send ) {
@@ -2128,6 +2142,7 @@ export class ChatwootService {
21282142 instance ,
21292143 body ,
21302144 'WAID:' + body . key . id ,
2145+ retryCount ,
21312146 ) ;
21322147
21332148 if ( ! send ) {
0 commit comments