Skip to content

Commit b18e83d

Browse files
Merge pull request #986 from deivisonrpg/refactor-onSendMessageError
refactor: Remove unnecessary code for handling contact not in WhatsApp and onSendMessageError
2 parents 968cc25 + b38f3cd commit b18e83d

File tree

5 files changed

+22
-38
lines changed

5 files changed

+22
-38
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,17 +1898,6 @@ export class BaileysStartupService extends ChannelStartupService {
18981898
const isWA = (await this.whatsappNumber({ numbers: [number] }))?.shift();
18991899

19001900
if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) {
1901-
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
1902-
const body = {
1903-
key: { remoteJid: isWA.jid },
1904-
};
1905-
1906-
this.chatwootService.eventWhatsapp(
1907-
'contact.is_not_in_wpp',
1908-
{ instanceName: this.instance.name, instanceId: this.instance.id },
1909-
body,
1910-
);
1911-
}
19121901
throw new BadRequestException(isWA);
19131902
}
19141903

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,19 +1137,35 @@ export class ChatwootService {
11371137
}
11381138
}
11391139

1140-
public async onSendMessageError(instance: InstanceDto, conversation: number, error?: string) {
1140+
public async onSendMessageError(instance: InstanceDto, conversation: number, error?: any) {
1141+
this.logger.verbose(`onSendMessageError ${JSON.stringify(error)}`);
1142+
11411143
const client = await this.clientCw(instance);
11421144

11431145
if (!client) {
11441146
return;
11451147
}
11461148

1149+
if (error && error?.status === 400 && error?.message[0]?.exists === false) {
1150+
client.messages.create({
1151+
accountId: this.provider.accountId,
1152+
conversationId: conversation,
1153+
data: {
1154+
content: `${i18next.t('cw.message.numbernotinwhatsapp')}`,
1155+
message_type: 'outgoing',
1156+
private: true,
1157+
},
1158+
});
1159+
1160+
return;
1161+
}
1162+
11471163
client.messages.create({
11481164
accountId: this.provider.accountId,
11491165
conversationId: conversation,
11501166
data: {
11511167
content: i18next.t('cw.message.notsent', {
1152-
error: error?.length > 0 ? `_${error}_` : '',
1168+
error: error ? `_${error.toString()}_` : '',
11531169
}),
11541170
message_type: 'outgoing',
11551171
private: true,
@@ -1393,7 +1409,7 @@ export class ChatwootService {
13931409
);
13941410
} catch (error) {
13951411
if (!messageSent && body.conversation?.id) {
1396-
this.onSendMessageError(instance, body.conversation?.id, error.toString());
1412+
this.onSendMessageError(instance, body.conversation?.id, error);
13971413
}
13981414
throw error;
13991415
}
@@ -1853,27 +1869,6 @@ export class ChatwootService {
18531869
}
18541870
}
18551871

1856-
if (event === 'contact.is_not_in_wpp') {
1857-
const getConversation = await this.createConversation(instance, body);
1858-
1859-
if (!getConversation) {
1860-
this.logger.warn('conversation not found');
1861-
return;
1862-
}
1863-
1864-
client.messages.create({
1865-
accountId: this.provider.accountId,
1866-
conversationId: getConversation,
1867-
data: {
1868-
content: `🚨 ${i18next.t('numbernotinwhatsapp')}`,
1869-
message_type: 'outgoing',
1870-
private: true,
1871-
},
1872-
});
1873-
1874-
return;
1875-
}
1876-
18771872
if (event === 'messages.upsert' || event === 'send.message') {
18781873
if (body.key.remoteJid === 'status@broadcast') {
18791874
return;

src/utils/translations/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"qrgeneratedsuccesfully": "QRCode successfully generated!",
33
"scanqr": "Scan this QR code within the next 40 seconds.",
44
"qrlimitreached": "QRCode generation limit reached, to generate a new QRCode, send the 'init' message again.",
5-
"numbernotinwhatsapp": "The message was not sent as the contact is not a valid Whatsapp number.",
65
"cw.inbox.connected": "🚀 Connection successfully established!",
76
"cw.inbox.disconnect": "🚨 Disconnecting WhatsApp from inbox *{{inboxName}}*.",
87
"cw.inbox.alreadyConnected": "🚨 {{inboxName}} instance is connected.",
@@ -23,5 +22,6 @@
2322
"cw.contactMessage.name": "Name",
2423
"cw.contactMessage.number": "Number",
2524
"cw.message.notsent": "🚨 The message could not be sent. Please check your connection. {{error}}",
25+
"cw.message.numbernotinwhatsapp": "🚨 The message was not sent as the contact is not a valid Whatsapp number.",
2626
"cw.message.edited": "Edited Message"
2727
}

src/utils/translations/es.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"qrgeneratedsuccesfully": "Código QR generado exitosamente!",
33
"scanqr": "Escanea este código QR en los próximos 40 segundos.",
44
"qrlimitreached": "🚨 Se alcanzó el límite de generación de QRCode. Para generar un nuevo QRCode, envíe el mensaje 'init' nuevamente.",
5-
"numbernotinwhatsapp": "⚠️ El mensaje no fue enviado porque el contacto no es un número de Whatsapp válido..",
65
"cw.inbox.connected": "🚀 ¡Conexión establecida exitosamente!",
76
"cw.inbox.disconnect": "🚨 Instancia *{{inboxName}}* desconectado de Whatsapp.",
87
"cw.inbox.alreadyConnected": "🚨 La instancia {{inboxName}} está conectada.",
@@ -23,5 +22,6 @@
2322
"cw.contactMessage.name": "Nombre",
2423
"cw.contactMessage.number": "Numero",
2524
"cw.message.notsent": "🚨 El mensaje no se pudo enviar. Comprueba tu conexión. {{error}}",
25+
"cw.message.numbernotinwhatsapp": "🚨 El mensaje no fue enviado porque el contacto no es un número de Whatsapp válido.",
2626
"cw.message.edited": "Mensaje editado"
2727
}

src/utils/translations/pt-BR.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"qrgeneratedsuccesfully": "QRCode gerado com sucesso!",
33
"scanqr": "Escaneie o QRCode com o WhatsApp nos próximos 40 segundos.",
44
"qrlimitreached": "Limite de geração de QRCode atingido! Para gerar um novo QRCode, envie o texto 'init' nesta conversa.",
5-
"numbernotinwhatsapp": "A mensagem não foi enviada, pois o contato não é um número válido do WhatsApp.",
65
"cw.inbox.connected": "🚀 Conectado com sucesso!",
76
"cw.inbox.disconnect": "🚨 Instância *{{inboxName}}* desconectada do WhatsApp.",
87
"cw.inbox.alreadyConnected": "🚨 Instância *{{inboxName}}* já está conectada.",
@@ -23,5 +22,6 @@
2322
"cw.contactMessage.name": "Nome",
2423
"cw.contactMessage.number": "Número",
2524
"cw.message.notsent": "🚨 Não foi possível enviar a mensagem. Verifique sua conexão. {{error}}",
25+
"cw.message.numbernotinwhatsapp": "🚨 A mensagem não foi enviada, pois o contato não é um número válido do WhatsApp.",
2626
"cw.message.edited": "Mensagem editada"
2727
}

0 commit comments

Comments
 (0)