From 238b7618b4a3df20698f2833da1f11173548f6a6 Mon Sep 17 00:00:00 2001 From: jaison-x Date: Wed, 21 Feb 2024 12:02:12 -0300 Subject: [PATCH] feat(chatwoot): add some translations on chatwoot messages Add typescript to transpile .json files inside src folder. --- src/utils/translations/en.json | 21 ++++- src/utils/translations/pt-BR.json | 23 +++++- src/whatsapp/services/chatwoot.service.ts | 98 ++++++++++++++--------- tsconfig.json | 6 +- 4 files changed, 107 insertions(+), 41 deletions(-) diff --git a/src/utils/translations/en.json b/src/utils/translations/en.json index d8566c72..2eeeccb1 100644 --- a/src/utils/translations/en.json +++ b/src/utils/translations/en.json @@ -2,5 +2,24 @@ "qrgeneratedsuccesfully": "QRCode successfully generated!", "scanqr": "Scan this QR code within the next 40 seconds.", "qrlimitreached": "QRCode generation limit reached, to generate a new QRCode, send the 'init' message again.", - "numbernotinwhatsapp": "The message was not sent as the contact is not a valid Whatsapp number." + "numbernotinwhatsapp": "The message was not sent as the contact is not a valid Whatsapp number.", + "cw.inbox.connected": "🚀 Connection successfully established!", + "cw.inbox.disconnect": "🚨 Disconnecting WhatsApp from inbox *{{inboxName}}*.", + "cw.inbox.alreadyConnected": "🚨 {{inboxName}} instance is connected.", + "cw.inbox.clearCache": "✅ {{inboxName}} instance cache cleared.", + "cw.inbox.notFound": "⚠️ {{inboxName}} instance not found.", + "cw.inbox.status": "⚠️ {{inboxName}} instance status: *{{state}}*.", + "cw.import.startImport": "💬 Starting to import messages. Please wait...", + "cw.import.importingMessages": "💬 Importing messages. More one moment...", + "cw.import.messagesImported": "💬 {{totalMessagesImported}} messages imported. Refresh page to see the new messages.", + "cw.import.messagesException": "💬 Something went wrong in importing messages.", + "cw.locationMessage.location": "Location", + "cw.locationMessage.latitude": "Latitude", + "cw.locationMessage.longitude": "Longitude", + "cw.locationMessage.locationName": "Name", + "cw.locationMessage.locationAddress": "Address", + "cw.locationMessage.locationUrl": "URL", + "cw.contactMessage.contact": "Contact", + "cw.contactMessage.name": "Name", + "cw.contactMessage.number": "Number" } \ No newline at end of file diff --git a/src/utils/translations/pt-BR.json b/src/utils/translations/pt-BR.json index a9668848..52bfcd69 100644 --- a/src/utils/translations/pt-BR.json +++ b/src/utils/translations/pt-BR.json @@ -1,6 +1,25 @@ { "qrgeneratedsuccesfully": "QRCode gerado com sucesso!", - "scanqr": "Escanei o QRCode com o Whatsapp nos próximos 40 segundos.", + "scanqr": "Escaneie o QRCode com o WhatsApp nos próximos 40 segundos.", "qrlimitreached": "Limite de geração de QRCode atingido! Para gerar um novo QRCode, envie o texto 'init' nesta conversa.", - "numbernotinwhatsapp": "A mensagem não foi enviada, pois o contato não é um número válido do Whatsapp." + "numbernotinwhatsapp": "A mensagem não foi enviada, pois o contato não é um número válido do WhatsApp.", + "cw.inbox.connected": "🚀 Conectado com sucesso!", + "cw.inbox.disconnect": "🚨 Instância *{{inboxName}}* desconectada do WhatsApp.", + "cw.inbox.alreadyConnected": "🚨 Instância *{{inboxName}}* já está conectada.", + "cw.inbox.clearCache": "✅ Instância *{{inboxName}}* cache removido.", + "cw.inbox.notFound": "⚠️ Instância *{{inboxName}}* não encontrada.", + "cw.inbox.status": "⚠️ Status da instância {{inboxName}}: *{{state}}*.", + "cw.import.startImport": "💬 Iniciando importação de mensagens. Por favor, aguarde...", + "cw.import.importingMessages": "💬 Importando mensagens. Mais um momento...", + "cw.import.messagesImported": "💬 {{totalMessagesImported}} mensagens importadas. Atualize a página para ver as novas mensagens.", + "cw.import.messagesException": "💬 Não foi possível importar as mensagens.", + "cw.locationMessage.location": "Localização", + "cw.locationMessage.latitude": "Latitude", + "cw.locationMessage.longitude": "Longitude", + "cw.locationMessage.locationName": "Nome", + "cw.locationMessage.locationAddress": "Endereço", + "cw.locationMessage.locationUrl": "URL", + "cw.contactMessage.contact": "Contato", + "cw.contactMessage.name": "Nome", + "cw.contactMessage.number": "Número" } \ No newline at end of file diff --git a/src/whatsapp/services/chatwoot.service.ts b/src/whatsapp/services/chatwoot.service.ts index 9998c6a7..3f50f06d 100644 --- a/src/whatsapp/services/chatwoot.service.ts +++ b/src/whatsapp/services/chatwoot.service.ts @@ -1191,14 +1191,26 @@ export class ChatwootService { await waInstance.connectToWhatsapp(number); } else { this.logger.verbose('whatsapp already connected'); - await this.createBotMessage(instance, `🚨 ${body.inbox.name} instance is connected.`, 'incoming'); + await this.createBotMessage( + instance, + i18next.t('cw.inbox.alreadyConnected', { + inboxName: body.inbox.name, + }), + 'incoming', + ); } } if (command === 'clearcache') { this.logger.verbose('command clearcache found'); waInstance.clearCacheChatwoot(); - await this.createBotMessage(instance, `✅ ${body.inbox.name} instance cache cleared.`, 'incoming'); + await this.createBotMessage( + instance, + i18next.t('cw.inbox.clearCache', { + inboxName: body.inbox.name, + }), + 'incoming', + ); } if (command === 'status') { @@ -1208,19 +1220,34 @@ export class ChatwootService { if (!state) { this.logger.verbose('state not found'); - await this.createBotMessage(instance, `⚠️ ${body.inbox.name} instance not found.`, 'incoming'); + await this.createBotMessage( + instance, + i18next.t('cw.inbox.notFound', { + inboxName: body.inbox.name, + }), + 'incoming', + ); } if (state) { this.logger.verbose('state: ' + state + ' found'); - await this.createBotMessage(instance, `⚠️ ${body.inbox.name} instance status: *${state}*`, 'incoming'); + await this.createBotMessage( + instance, + i18next.t('cw.inbox.status', { + inboxName: body.inbox.name, + state: state, + }), + 'incoming', + ); } } if (command === 'disconnect' || command === 'desconectar') { this.logger.verbose('command disconnect found'); - const msgLogout = `🚨 Disconnecting Whatsapp from inbox *${body.inbox.name}*: `; + const msgLogout = i18next.t('cw.inbox.disconnect', { + inboxName: body.inbox.name, + }); this.logger.verbose('send message to chatwoot'); await this.createBotMessage(instance, msgLogout, 'incoming'); @@ -1509,27 +1536,17 @@ export class ChatwootService { const latitude = result.degreesLatitude; const longitude = result.degreesLongitude; - const locationName = result?.name || 'Unknown'; - const locationAddress = result?.address || 'Unknown'; + const locationName = result?.name; + const locationAddress = result?.address; const formattedLocation = - '*Localização:*\n\n' + - '_Latitude:_ ' + - latitude + - '\n' + - '_Longitude:_ ' + - longitude + - '\n' + - '_Nome:_ ' + - locationName + - '\n' + - '_Endereço:_ ' + - locationAddress + - '\n' + - '_Url:_ https://www.google.com/maps/search/?api=1&query=' + - latitude + - ',' + - longitude; + `*${i18next.t('cw.locationMessage.location')}:*\n\n` + + `_${i18next.t('cw.locationMessage.latitude')}:_ ${latitude} \n` + + `_${i18next.t('cw.locationMessage.longitude')}:_ ${longitude} \n` + + (locationName ? `_${i18next.t('cw.locationMessage.locationName')}:_ ${locationName}\n` : '') + + (locationAddress ? `_${i18next.t('cw.locationMessage.locationAddress')}:_ ${locationAddress} \n` : '') + + `_${i18next.t('cw.locationMessage.locationUrl')}:_ ` + + `https://www.google.com/maps/search/?api=1&query=${latitude},${longitude}`; this.logger.verbose('message content: ' + formattedLocation); @@ -1547,17 +1564,19 @@ export class ChatwootService { } }); - let formattedContact = '*Contact:*\n\n' + '_Name:_ ' + contactInfo['FN']; + let formattedContact = + `*${i18next.t('cw.contactMessage.contact')}:*\n\n` + + `_${i18next.t('cw.contactMessage.name')}:_ ${contactInfo['FN']}`; let numberCount = 1; Object.keys(contactInfo).forEach((key) => { if (key.startsWith('item') && key.includes('TEL')) { const phoneNumber = contactInfo[key]; - formattedContact += '\n_Number (' + numberCount + '):_ ' + phoneNumber; + formattedContact += `\n_${i18next.t('cw.contactMessage.number')} (${numberCount}):_ ${phoneNumber}`; numberCount++; } else if (key.includes('TEL')) { const phoneNumber = contactInfo[key]; - formattedContact += '\n_Number (' + numberCount + '):_ ' + phoneNumber; + formattedContact += `\n_${i18next.t('cw.contactMessage.number')} (${numberCount}):_ ${phoneNumber}`; numberCount++; } }); @@ -1578,17 +1597,19 @@ export class ChatwootService { } }); - let formattedContact = '*Contact:*\n\n' + '_Name:_ ' + contact.displayName; + let formattedContact = `*${i18next.t('cw.contactMessage.contact')}:*\n\n_${i18next.t( + 'cw.contactMessage.name', + )}:_ ${contact.displayName}`; let numberCount = 1; Object.keys(contactInfo).forEach((key) => { if (key.startsWith('item') && key.includes('TEL')) { const phoneNumber = contactInfo[key]; - formattedContact += '\n_Number (' + numberCount + '):_ ' + phoneNumber; + formattedContact += `\n_${i18next.t('cw.contactMessage.number')} (${numberCount}):_ ${phoneNumber}`; numberCount++; } else if (key.includes('TEL')) { const phoneNumber = contactInfo[key]; - formattedContact += '\n_Number (' + numberCount + '):_ ' + phoneNumber; + formattedContact += `\n_${i18next.t('cw.contactMessage.number')} (${numberCount}):_ ${phoneNumber}`; numberCount++; } }); @@ -2074,7 +2095,10 @@ export class ChatwootService { return; } - const msgStatus = `⚡️ Instance status ${inbox.name}: ${data.status}`; + const msgStatus = i18next.t('cw.inbox.status', { + inboxName: inbox.name, + state: data.status, + }); this.logger.verbose('send message to chatwoot'); await this.createBotMessage(instance, msgStatus, 'incoming'); @@ -2086,7 +2110,7 @@ export class ChatwootService { if (body.status === 'open') { // if we have qrcode count then we understand that a new connection was established if (this.waMonitor.waInstances[instance.instanceName].qrCode.count > 0) { - const msgConnection = `🚀 Connection successfully established!`; + const msgConnection = i18next.t('cw.inbox.connected'); this.logger.verbose('send message to chatwoot'); await this.createBotMessage(instance, msgConnection, 'incoming'); this.waMonitor.waInstances[instance.instanceName].qrCode.count = 0; @@ -2147,7 +2171,7 @@ export class ChatwootService { return; } - this.createBotMessage(instance, `💬 Starting to import messages. Please wait...`, 'incoming'); + this.createBotMessage(instance, i18next.t('cw.import.startImport'), 'incoming'); } public isImportHistoryAvailable() { @@ -2180,7 +2204,7 @@ export class ChatwootService { return; } - this.createBotMessage(instance, '💬 Importing messages. More one moment...', 'incoming'); + this.createBotMessage(instance, i18next.t('cw.import.importingMessages'), 'incoming'); const totalMessagesImported = await chatwootImport.importHistoryMessages( instance, @@ -2191,10 +2215,10 @@ export class ChatwootService { this.updateContactAvatarInRecentConversations(instance); const msg = Number.isInteger(totalMessagesImported) - ? `${totalMessagesImported} messages imported. Refresh page to see the new messages` - : `Something went wrong in importing messages`; + ? i18next.t('cw.import.messagesImported', { totalMessagesImported }) + : i18next.t('cw.import.messagesException'); - this.createBotMessage(instance, `💬 ${msg}`, 'incoming'); + this.createBotMessage(instance, msg, 'incoming'); return totalMessagesImported; } diff --git a/tsconfig.json b/tsconfig.json index 0c89eea1..156bb77c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,5 +18,9 @@ "incremental": true, "noImplicitAny": false }, - "exclude": ["node_modules", "./test", "./dist", "./prisma"] + "exclude": ["node_modules", "./test", "./dist", "./prisma"], + "include": [ + "src/**/*", + "src/**/*.json" + ] } \ No newline at end of file