Skip to content

Commit ff21cf4

Browse files
Merge pull request #558 from jaison-x/pr2
feat(chatwoot): send private message on error message sent from chatwoot
2 parents dc19c7f + 6c8ffd8 commit ff21cf4

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

src/api/integrations/chatwoot/services/chatwoot.service.ts

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ChatwootClient, {
88
inbox,
99
} from '@figuro/chatwoot-sdk';
1010
import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request';
11+
import { proto } from '@whiskeysockets/baileys';
1112
import axios from 'axios';
1213
import FormData from 'form-data';
1314
import { 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));
@@ -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

src/utils/translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"cw.contactMessage.contact": "Contact",
2323
"cw.contactMessage.name": "Name",
2424
"cw.contactMessage.number": "Number",
25+
"cw.message.notsent": "🚨 The message could not be sent. Please check your connection. {{error}}",
2526
"cw.message.edited": "Edited Message"
2627
}

src/utils/translations/es.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"cw.contactMessage.contact": "Contacto",
2323
"cw.contactMessage.name": "Nombre",
2424
"cw.contactMessage.number": "Numero",
25+
"cw.message.notsent": "🚨 El mensaje no se pudo enviar. Comprueba tu conexión. {{error}}",
2526
"cw.message.edited": "Mensaje editado"
2627
}

src/utils/translations/pt-BR.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"cw.contactMessage.contact": "Contato",
2323
"cw.contactMessage.name": "Nome",
2424
"cw.contactMessage.number": "Número",
25+
"cw.message.notsent": "🚨 Não foi possível enviar a mensagem. Verifique sua conexão. {{error}}",
2526
"cw.message.edited": "Mensagem editada"
2627
}

0 commit comments

Comments
 (0)