@@ -8,6 +8,7 @@ import ChatwootClient, {
88 inbox ,
99} from '@figuro/chatwoot-sdk' ;
1010import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request' ;
11+ import { proto } from '@whiskeysockets/baileys' ;
1112import axios from 'axios' ;
1213import FormData from 'form-data' ;
1314import { createReadStream , unlinkSync , writeFileSync } from 'fs' ;
@@ -628,7 +629,7 @@ export class ChatwootService {
628629 id : contactId ,
629630 } ) ) as any ;
630631
631- if ( contactConversations ) {
632+ if ( contactConversations ?. payload ?. length ) {
632633 let conversation : any ;
633634 if ( this . provider . reopen_conversation ) {
634635 conversation = contactConversations . payload . find ( ( conversation ) => conversation . inbox_id == filterInbox . id ) ;
@@ -1106,6 +1107,26 @@ export class ChatwootService {
11061107 }
11071108 }
11081109
1110+ public async onSendMessageError ( instance : InstanceDto , conversation : number , error ?: string ) {
1111+ const client = await this . clientCw ( instance ) ;
1112+
1113+ if ( ! client ) {
1114+ return ;
1115+ }
1116+
1117+ client . messages . create ( {
1118+ accountId : this . provider . account_id ,
1119+ conversationId : conversation ,
1120+ data : {
1121+ content : i18next . t ( 'cw.message.notsent' , {
1122+ error : error ?. length > 0 ? `_${ error } _` : '' ,
1123+ } ) ,
1124+ message_type : 'outgoing' ,
1125+ private : true ,
1126+ } ,
1127+ } ) ;
1128+ }
1129+
11091130 public async receiveWebhook ( instance : InstanceDto , body : any ) {
11101131 try {
11111132 await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
@@ -1274,6 +1295,11 @@ export class ChatwootService {
12741295 return { message : 'bot' } ;
12751296 }
12761297
1298+ if ( ! waInstance && body . conversation ?. id ) {
1299+ this . onSendMessageError ( instance , body . conversation ?. id , 'Instance not found' ) ;
1300+ return { message : 'bot' } ;
1301+ }
1302+
12771303 this . logger . verbose ( 'Format message to send' ) ;
12781304 let formatText : string ;
12791305 if ( senderName === null || senderName === undefined ) {
@@ -1310,6 +1336,9 @@ export class ChatwootService {
13101336 formatText ,
13111337 options ,
13121338 ) ;
1339+ if ( ! messageSent && body . conversation ?. id ) {
1340+ this . onSendMessageError ( instance , body . conversation ?. id ) ;
1341+ }
13131342
13141343 this . updateChatwootMessageId (
13151344 {
@@ -1343,23 +1372,34 @@ export class ChatwootService {
13431372 } ,
13441373 } ;
13451374
1346- const messageSent = await waInstance ?. textMessage ( data , true ) ;
1375+ let messageSent : MessageRaw | proto . WebMessageInfo ;
1376+ try {
1377+ messageSent = await waInstance ?. textMessage ( data , true ) ;
1378+ if ( ! messageSent ) {
1379+ throw new Error ( 'Message not sent' ) ;
1380+ }
13471381
1348- this . updateChatwootMessageId (
1349- {
1350- ...messageSent ,
1351- owner : instance . instanceName ,
1352- } ,
1353- {
1354- messageId : body . id ,
1355- inboxId : body . inbox ?. id ,
1356- conversationId : body . conversation ?. id ,
1357- contactInbox : {
1358- sourceId : body . conversation ?. contact_inbox ?. source_id ,
1382+ this . updateChatwootMessageId (
1383+ {
1384+ ...messageSent ,
1385+ owner : instance . instanceName ,
13591386 } ,
1360- } ,
1361- instance ,
1362- ) ;
1387+ {
1388+ messageId : body . id ,
1389+ inboxId : body . inbox ?. id ,
1390+ conversationId : body . conversation ?. id ,
1391+ contactInbox : {
1392+ sourceId : body . conversation ?. contact_inbox ?. source_id ,
1393+ } ,
1394+ } ,
1395+ instance ,
1396+ ) ;
1397+ } catch ( error ) {
1398+ if ( ! messageSent && body . conversation ?. id ) {
1399+ this . onSendMessageError ( instance , body . conversation ?. id , error . toString ( ) ) ;
1400+ }
1401+ throw error ;
1402+ }
13631403 }
13641404 }
13651405
0 commit comments