@tiledesk/tiledesk-whatsapp-connector 0.1.77 → 0.1.79

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "اختر خيارا"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "选择一个选项"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Wählen Sie eine Option"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Choose an option"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Elige una opción"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Choisir une option"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "एक विकल्प चुनें"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Scegli un'opzione"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "オプションを選択"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "옵션을 선택하세요"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Escolha uma opção"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ChooseOption": "Выберите вариант"
3
+ }
package/index.js CHANGED
@@ -33,6 +33,7 @@ const { MessageHandler } = require("./tiledesk/MessageHandler");
33
33
  const { TiledeskBotTester } = require("./tiledesk/TiledeskBotTester");
34
34
  const { TemplateManager } = require("./tiledesk/TemplateManager");
35
35
  const { WhatsappLogger } = require("./tiledesk/WhatsappLogger");
36
+ const translator = require('./utils/Translator')
36
37
 
37
38
  // mongo
38
39
  const { KVBaseMongo } = require("./tiledesk/KVBaseMongo");
@@ -1328,7 +1329,7 @@ router.get("/ext/:project_id", async (req, res) => {
1328
1329
 
1329
1330
  async function startApp(settings, callback) {
1330
1331
  winston.info("(wab) Starting Whatsapp App");
1331
-
1332
+
1332
1333
  if (!settings.MONGODB_URL) {
1333
1334
  winston.error("(wab) MONGODB_URL is mandatory. Exit...");
1334
1335
  return callback("Missing parameter: MONGODB_URL");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-whatsapp-connector",
3
- "version": "0.1.77",
3
+ "version": "0.1.79",
4
4
  "description": "Tiledesk Whatsapp connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,10 +23,12 @@
23
23
  "express": "^4.18.1",
24
24
  "form-data": "^4.0.0",
25
25
  "handlebars": "^4.7.7",
26
+ "i18next": "^24.2.2",
27
+ "i18next-fs-backend": "^2.6.0",
26
28
  "jobs-worker-queued": "^0.0.5",
27
29
  "jsonwebtoken": "^8.5.1",
28
- "mongoose": "^6.5.0",
29
30
  "mongodb": "^3.5.5",
31
+ "mongoose": "^6.5.0",
30
32
  "multer": "^1.4.5-lts.1",
31
33
  "redis": "^3.1.2",
32
34
  "uuid": "^8.3.2",
@@ -1,5 +1,7 @@
1
1
  const { v4: uuidv4 } = require('uuid');
2
2
  const winston = require('../winston')
3
+ const utils = require('../utils/utils');
4
+ const translator = require('../utils/Translator');
3
5
 
4
6
  class TiledeskWhatsappTranslator {
5
7
 
@@ -48,10 +50,12 @@ const path = require('path');
48
50
  * @param {Object} tiledeskChannelMessage - The message in Tiledesk format.
49
51
  * @param {string} whatsapp_receiver - The Whatsapp recipient fo the message.
50
52
  */
51
- toWhatsapp(tiledeskChannelMessage, whatsapp_receiver) {
53
+ async toWhatsapp(tiledeskChannelMessage, whatsapp_receiver) {
52
54
 
53
55
  winston.debug("(wab) [Translator] tiledesk message: ", tiledeskChannelMessage);
54
56
 
57
+ let lang = await utils.detectLanguageFromPhone(whatsapp_receiver);
58
+
55
59
  let text = '';
56
60
  if (tiledeskChannelMessage.text) {
57
61
  text = tiledeskChannelMessage.text;
@@ -250,7 +254,7 @@ const path = require('path');
250
254
  type: "list",
251
255
  body: { text: text },
252
256
  action: {
253
- button: "Choose an option",
257
+ button: translator.translate("ChooseOption", lang),
254
258
  sections: sections
255
259
  }
256
260
  }
@@ -264,7 +268,7 @@ const path = require('path');
264
268
  // Option 3: Send message with *buttons (questa)
265
269
 
266
270
  whatsapp_message.text = { body: tiledeskChannelMessage.attributes._raw_message };
267
- return whatsapp_message;
271
+ return null;
268
272
  }
269
273
 
270
274
  }
Binary file
@@ -0,0 +1,43 @@
1
+ const i18next = require('i18next');
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+
5
+
6
+ class Translator {
7
+
8
+ constructor(translationsPath) {
9
+
10
+ this.translationsPath = translationsPath;
11
+ this.resources = this.loadTranslations();
12
+
13
+ i18next.init({
14
+ fallbackLng: "EN",
15
+ resources: this.resources
16
+ })
17
+
18
+ }
19
+
20
+ loadTranslations() {
21
+ const resources = {};
22
+ const files = fs.readdirSync(this.translationsPath);
23
+
24
+ files.forEach(file => {
25
+ if (file.endsWith('.json')) {
26
+ const lang = path.basename(file, '.json').toUpperCase();
27
+ const filePath = path.join(this.translationsPath, file);
28
+ const translations = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
29
+ resources[lang] = { translation: translations };
30
+ }
31
+ })
32
+ return resources;
33
+ }
34
+
35
+ translate(key, language) {
36
+ return i18next.t(key, { lng: language.toUpperCase() });
37
+ }
38
+
39
+ }
40
+
41
+ const translator = new Translator(path.join(__dirname, '../assets/i18n'));
42
+
43
+ module.exports = translator;
package/utils/utils.js ADDED
@@ -0,0 +1,51 @@
1
+ const phonePrefixes = {
2
+ "+1": "EN", // USA, Canada
3
+ "+44": "EN", // UK
4
+ "+33": "FR", // France
5
+ "+34": "ES", // Spain
6
+ "+49": "DE", // Germany
7
+ "+39": "IT", // Italy
8
+ "+81": "JA", // Japan
9
+ "+86": "CH", // China
10
+ "+91": "HI", // India
11
+ "+55": "PT", // Brazil
12
+ "+7": "RU", // Russia
13
+ "+61": "EN", // Australia
14
+ "+966": "AR", // Saudi Arabia
15
+ "+20": "AR", // Egypt
16
+ "+27": "EN", // South Africa
17
+ "+82": "KO", // South Korea
18
+ "+52": "ES", // Mexico
19
+ };
20
+
21
+ class Utils {
22
+
23
+ async detectLanguageFromPhone(phone_number) {
24
+
25
+ phone_number = fixNumber(phone_number);
26
+ const sanitizedNumber = phone_number.replace(/[^+\d]/g, "");
27
+ const sortedPrefixes = Object.keys(phonePrefixes).sort(
28
+ (a, b) => b.length - a.length
29
+ );
30
+ const matchedPrefix = sortedPrefixes.find((prefix) =>
31
+ sanitizedNumber.startsWith(prefix)
32
+ );
33
+ if (!matchedPrefix) {
34
+ return "EN";
35
+ }
36
+ return phonePrefixes[matchedPrefix];
37
+ }
38
+
39
+ }
40
+
41
+ function fixNumber(phone_number) {
42
+ if (phone_number.startsWith("+")) {
43
+ return phone_number;
44
+ } else {
45
+ return "+" + phone_number;
46
+ }
47
+ }
48
+
49
+ let utils = new Utils();
50
+
51
+ module.exports = utils;