Add translate capabilities to QRMessages in CW

This commit is contained in:
Douglas Rauber at Nitro 2024-01-25 16:19:08 -03:00
parent 7f74de07ed
commit 0edc8a9284
9 changed files with 68 additions and 6 deletions

3
.gitignore vendored
View File

@ -44,4 +44,5 @@ docker-compose.yaml
/temp/*
.DS_Store
*.DS_Store
*.DS_Store
.tool-versions

View File

@ -62,6 +62,7 @@
"express-async-errors": "^3.1.1",
"hbs": "^4.2.0",
"https-proxy-agent": "^7.0.2",
"i18next": "^23.7.19",
"jimp": "^0.16.13",
"join": "^3.0.0",
"js-yaml": "^4.1.0",

View File

@ -127,6 +127,8 @@ export type Auth = {
export type DelInstance = number | boolean;
export type Language = string | 'en';
export type GlobalWebhook = {
URL: string;
ENABLED: boolean;
@ -163,6 +165,7 @@ export interface Env {
WEBSOCKET: Websocket;
LOG: Log;
DEL_INSTANCE: DelInstance;
LANGUAGE: Language;
WEBHOOK: Webhook;
CONFIG_SESSION_PHONE: ConfigSessionPhone;
QRCODE: QrCode;

View File

@ -179,3 +179,6 @@ AUTHENTICATION:
JWT:
EXPIRIN_IN: 0 # seconds - 3600s === 1h | zero (0) - never expires
SECRET: L=0YWt]b2w[WF>#>:&E`
LANGUAGE: "pt-BR" # pt-BR, en

36
src/utils/i18n.ts Normal file
View File

@ -0,0 +1,36 @@
import fs from 'fs';
import i18next from 'i18next';
import path from 'path';
import { ConfigService, Language } from '../config/env.config';
// export class i18n {
// constructor(private readonly configService: ConfigService) {
const languages = ['en', 'pt-BR'];
const translationsPath = path.join(__dirname, 'translations');
const configService: ConfigService = new ConfigService();
const resources: any = {};
languages.forEach((language) => {
const languagePath = path.join(translationsPath, `${language}.json`);
if (fs.existsSync(languagePath)) {
resources[language] = {
translation: require(languagePath),
};
}
});
i18next.init({
resources,
fallbackLng: 'en',
lng: configService.get<Language>('LANGUAGE'),
debug: false,
interpolation: {
escapeValue: false,
},
});
// }
// }
export default i18next;

View File

@ -0,0 +1,5 @@
{
"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."
}

View File

@ -0,0 +1,5 @@
{
"qrgeneratedsuccesfully": "QRCode gerado com sucesso!",
"scanqr": "Escanei 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."
}

View File

@ -9,6 +9,7 @@ import path from 'path';
import { ConfigService, HttpServer } from '../../config/env.config';
import { Logger } from '../../config/logger.config';
import i18next from '../../utils/i18n';
import { ICache } from '../abstract/abstract.cache';
import { ChatwootDto } from '../dto/chatwoot.dto';
import { InstanceDto } from '../dto/instance.dto';
@ -1994,7 +1995,8 @@ export class ChatwootService {
this.logger.verbose('event qrcode.updated');
if (body.statusCode === 500) {
this.logger.verbose('qrcode error');
const erroQRcode = `🚨 QRCode generation limit reached, to generate a new QRCode, send the 'init' message again.`;
const erroQRcode = `🚨 ${i18next.t('qrlimitreached')}`;
this.logger.verbose('send message to chatwoot');
return await this.createBotMessage(instance, erroQRcode, 'incoming');
@ -2010,9 +2012,9 @@ export class ChatwootService {
writeFileSync(fileName, fileData, 'utf8');
this.logger.verbose('send qrcode to chatwoot');
await this.createBotQr(instance, 'QRCode successfully generated!', 'incoming', fileName);
await this.createBotQr(instance, i18next.t('qrgeneratedsuccesfully'), 'incoming', fileName);
let msgQrCode = `⚡️ QRCode successfully generated!\n\nScan this QR code within the next 40 seconds.`;
let msgQrCode = `⚡️${i18next.t('qrgeneratedsuccesfully')}\n\n${i18next.t('scanqr')}`;
if (body?.qrcode?.pairingCode) {
msgQrCode =

View File

@ -274,7 +274,7 @@ export class TypebotService {
const types = {
conversation: msg.conversation,
extendedTextMessage: msg.extendedTextMessage?.text,
responseRowId: msg.listResponseMessage.singleSelectReply?.selectedRowId,
responseRowId: msg.listResponseMessage?.singleSelectReply?.selectedRowId,
};
this.logger.verbose('type message: ' + types);
@ -413,7 +413,13 @@ export class TypebotService {
text += element.text;
}
if (element.children && (element.type === 'p' || element.type === 'a' || element.type === 'inline-variable' || element.type === 'variable')) {
if (
element.children &&
(element.type === 'p' ||
element.type === 'a' ||
element.type === 'inline-variable' ||
element.type === 'variable')
) {
for (const child of element.children) {
text += applyFormatting(child);
}