@@ -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' ;
@@ -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 ) ) ;
@@ -1310,6 +1331,9 @@ export class ChatwootService {
13101331 formatText ,
13111332 options ,
13121333 ) ;
1334+ if ( ! messageSent && body . conversation ?. id ) {
1335+ this . onSendMessageError ( instance , body . conversation ?. id ) ;
1336+ }
13131337
13141338 this . updateChatwootMessageId (
13151339 {
@@ -1343,23 +1367,31 @@ export class ChatwootService {
13431367 } ,
13441368 } ;
13451369
1346- const messageSent = await waInstance ?. textMessage ( data , true ) ;
1370+ let messageSent : MessageRaw | proto . WebMessageInfo ;
1371+ try {
1372+ messageSent = await waInstance ?. textMessage ( data , true ) ;
13471373
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 ,
1374+ this . updateChatwootMessageId (
1375+ {
1376+ ...messageSent ,
1377+ owner : instance . instanceName ,
13591378 } ,
1360- } ,
1361- instance ,
1362- ) ;
1379+ {
1380+ messageId : body . id ,
1381+ inboxId : body . inbox ?. id ,
1382+ conversationId : body . conversation ?. id ,
1383+ contactInbox : {
1384+ sourceId : body . conversation ?. contact_inbox ?. source_id ,
1385+ } ,
1386+ } ,
1387+ instance ,
1388+ ) ;
1389+ } catch ( error ) {
1390+ if ( ! messageSent && body . conversation ?. id ) {
1391+ this . onSendMessageError ( instance , body . conversation ?. id , error . toString ( ) ) ;
1392+ }
1393+ throw error ;
1394+ }
13631395 }
13641396 }
13651397
0 commit comments