Skip to content

Commit a6fb8cb

Browse files
committed
refactor: Remove unnecessary code for handling contact not in WhatsApp
The code changes in `whatsapp.baileys.service.ts` remove the unnecessary code that handles the case when a contact is not in WhatsApp. The code was previously sending a notification to Chatwoot, but it is no longer needed. This change improves the code by removing unnecessary complexity.
1 parent ec2b7f9 commit a6fb8cb

3 files changed

Lines changed: 20 additions & 36 deletions

File tree

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
@@ -1136,19 +1136,35 @@ export class ChatwootService {
11361136
}
11371137
}
11381138

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

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

1148+
if (error && error?.status === 400 && error?.message[0]?.exists === false) {
1149+
client.messages.create({
1150+
accountId: this.provider.accountId,
1151+
conversationId: conversation,
1152+
data: {
1153+
content: `${i18next.t('cw.message.numbernotinwhatsapp')}`,
1154+
message_type: 'outgoing',
1155+
private: true,
1156+
},
1157+
});
1158+
1159+
return;
1160+
}
1161+
11461162
client.messages.create({
11471163
accountId: this.provider.accountId,
11481164
conversationId: conversation,
11491165
data: {
11501166
content: i18next.t('cw.message.notsent', {
1151-
error: error?.length > 0 ? `_${error}_` : '',
1167+
error: error ? `_${error.toString()}_` : '',
11521168
}),
11531169
message_type: 'outgoing',
11541170
private: true,
@@ -1392,7 +1408,7 @@ export class ChatwootService {
13921408
);
13931409
} catch (error) {
13941410
if (!messageSent && body.conversation?.id) {
1395-
this.onSendMessageError(instance, body.conversation?.id, error.toString());
1411+
this.onSendMessageError(instance, body.conversation?.id, error);
13961412
}
13971413
throw error;
13981414
}
@@ -1852,27 +1868,6 @@ export class ChatwootService {
18521868
}
18531869
}
18541870

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

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)