@@ -9,6 +9,7 @@ import ChatwootClient, {
99} from '@figuro/chatwoot-sdk' ;
1010import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request' ;
1111import axios from 'axios' ;
12+ import { proto } from 'baileys' ;
1213import FormData from 'form-data' ;
1314import { createReadStream , unlinkSync , writeFileSync } from 'fs' ;
1415import Jimp from 'jimp' ;
@@ -85,12 +86,13 @@ export class ChatwootService {
8586 return client ;
8687 }
8788
88- public getClientCwConfig ( ) : ChatwootAPIConfig {
89+ public getClientCwConfig ( ) : ChatwootAPIConfig & { name_inbox : string } {
8990 return {
9091 basePath : this . provider . url ,
9192 with_credentials : true ,
9293 credentials : 'include' ,
9394 token : this . provider . token ,
95+ name_inbox : this . provider . name_inbox ,
9496 } ;
9597 }
9698
@@ -110,7 +112,7 @@ export class ChatwootService {
110112
111113 await this . initInstanceChatwoot (
112114 instance ,
113- instance . instanceName . split ( '-cwId-' ) [ 0 ] ,
115+ data . name_inbox ?? instance . instanceName . split ( '-cwId-' ) [ 0 ] ,
114116 `${ urlServer } /chatwoot/webhook/${ encodeURIComponent ( instance . instanceName ) } ` ,
115117 true ,
116118 data . number ,
@@ -628,7 +630,7 @@ export class ChatwootService {
628630 id : contactId ,
629631 } ) ) as any ;
630632
631- if ( contactConversations ) {
633+ if ( contactConversations ?. payload ?. length ) {
632634 let conversation : any ;
633635 if ( this . provider . reopen_conversation ) {
634636 conversation = contactConversations . payload . find ( ( conversation ) => conversation . inbox_id == filterInbox . id ) ;
@@ -710,7 +712,7 @@ export class ChatwootService {
710712 }
711713
712714 this . logger . verbose ( 'find inbox by name' ) ;
713- const findByName = inbox . payload . find ( ( inbox ) => inbox . name === instance . instanceName . split ( '-cwId-' ) [ 0 ] ) ;
715+ const findByName = inbox . payload . find ( ( inbox ) => inbox . name === this . getClientCwConfig ( ) . name_inbox ) ;
714716
715717 if ( ! findByName ) {
716718 this . logger . warn ( 'inbox not found' ) ;
@@ -1106,6 +1108,26 @@ export class ChatwootService {
11061108 }
11071109 }
11081110
1111+ public async onSendMessageError ( instance : InstanceDto , conversation : number , error ?: string ) {
1112+ const client = await this . clientCw ( instance ) ;
1113+
1114+ if ( ! client ) {
1115+ return ;
1116+ }
1117+
1118+ client . messages . create ( {
1119+ accountId : this . provider . account_id ,
1120+ conversationId : conversation ,
1121+ data : {
1122+ content : i18next . t ( 'cw.message.notsent' , {
1123+ error : error ?. length > 0 ? `_${ error } _` : '' ,
1124+ } ) ,
1125+ message_type : 'outgoing' ,
1126+ private : true ,
1127+ } ,
1128+ } ) ;
1129+ }
1130+
11091131 public async receiveWebhook ( instance : InstanceDto , body : any ) {
11101132 try {
11111133 await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
@@ -1274,6 +1296,11 @@ export class ChatwootService {
12741296 return { message : 'bot' } ;
12751297 }
12761298
1299+ if ( ! waInstance && body . conversation ?. id ) {
1300+ this . onSendMessageError ( instance , body . conversation ?. id , 'Instance not found' ) ;
1301+ return { message : 'bot' } ;
1302+ }
1303+
12771304 this . logger . verbose ( 'Format message to send' ) ;
12781305 let formatText : string ;
12791306 if ( senderName === null || senderName === undefined ) {
@@ -1310,6 +1337,9 @@ export class ChatwootService {
13101337 formatText ,
13111338 options ,
13121339 ) ;
1340+ if ( ! messageSent && body . conversation ?. id ) {
1341+ this . onSendMessageError ( instance , body . conversation ?. id ) ;
1342+ }
13131343
13141344 this . updateChatwootMessageId (
13151345 {
@@ -1343,23 +1373,34 @@ export class ChatwootService {
13431373 } ,
13441374 } ;
13451375
1346- const messageSent = await waInstance ?. textMessage ( data , true ) ;
1376+ let messageSent : MessageRaw | proto . WebMessageInfo ;
1377+ try {
1378+ messageSent = await waInstance ?. textMessage ( data , true ) ;
1379+ if ( ! messageSent ) {
1380+ throw new Error ( 'Message not sent' ) ;
1381+ }
13471382
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 ,
1383+ this . updateChatwootMessageId (
1384+ {
1385+ ...messageSent ,
1386+ owner : instance . instanceName ,
13591387 } ,
1360- } ,
1361- instance ,
1362- ) ;
1388+ {
1389+ messageId : body . id ,
1390+ inboxId : body . inbox ?. id ,
1391+ conversationId : body . conversation ?. id ,
1392+ contactInbox : {
1393+ sourceId : body . conversation ?. contact_inbox ?. source_id ,
1394+ } ,
1395+ } ,
1396+ instance ,
1397+ ) ;
1398+ } catch ( error ) {
1399+ if ( ! messageSent && body . conversation ?. id ) {
1400+ this . onSendMessageError ( instance , body . conversation ?. id , error . toString ( ) ) ;
1401+ }
1402+ throw error ;
1403+ }
13631404 }
13641405 }
13651406
0 commit comments