From 5261691ba94708a9d75d9155898a13cf55137733 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sun, 4 Sep 2022 18:50:36 -0300 Subject: [PATCH] fix(whatsapp.gblib): SEND FILE and WPP providers. --- package-lock.json | 52 +++---- packages/core.gbapp/services/GBMinService.ts | 20 ++- .../services/WhatsappDirectLine.ts | 140 ++++++++++-------- src/app.ts | 3 +- 4 files changed, 116 insertions(+), 99 deletions(-) diff --git a/package-lock.json b/package-lock.json index 38ecec1d..94ce2ac0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "botserver", - "version": "2.0.167", + "version": "2.0.168", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5070,7 +5070,7 @@ "any-promise": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", - "integrity": "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==" + "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" }, "any-shell-escape": { "version": "0.1.1", @@ -5699,12 +5699,12 @@ "process-nextick-args": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", - "integrity": "sha512-yN0WQmuCX63LP/TMvAg31nvT6m4vDqJEiiv2CAZqWOGNWutc9DfDk1NPYYmKUFmaVM2UwDowH4u5AHWYP/jxKw==" + "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" }, "readable-stream": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.0.6.tgz", - "integrity": "sha512-TXcFfb63BQe1+ySzsHZI/5v1aJPCShfqvWJ64ayNImXMsN1Cd0YGk/wm8KB7/OeessgPc9QvS9Zou8QTkFzsLw==", + "integrity": "sha1-j5A0HmilPMySh4jaz80Rs265t44=", "requires": { "core-util-is": "~1.0.0", "inherits": "~2.0.1", @@ -5717,22 +5717,22 @@ "sax": { "version": "0.5.8", "resolved": "https://registry.npmjs.org/sax/-/sax-0.5.8.tgz", - "integrity": "sha512-c0YL9VcSfcdH3F1Qij9qpYJFpKFKMXNOkLWFssBL3RuF7ZS8oZhllR2rWlCRjDTJsfq3R6wbSsaRU6o0rkEdNw==" + "integrity": "sha1-1HLbIo6zMcJQaw6MFVJK25OdEsE=" }, "string_decoder": { "version": "0.10.31", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" }, "underscore": { "version": "1.8.3", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", - "integrity": "sha512-5WsVTFcH1ut/kkhAaHf4PVgI8c7++GiVcpCGxPouI6ZVjsqPnSDf8h/8HtVqc0t4fzRXwnMK70EcZeAs3PIddg==" + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" }, "xml2js": { "version": "0.2.8", "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.2.8.tgz", - "integrity": "sha512-ZHZBIAO55GHCn2jBYByVPHvHS+o3j8/a/qmpEe6kxO3cTnTCWC3Htq9RYJ5G4XMwMMClD2QkXA9SNdPadLyn3Q==", + "integrity": "sha1-m4FpCTFjH/CdGVdUn69U9PmAs8I=", "requires": { "sax": "0.5.x" } @@ -5740,7 +5740,7 @@ "xmlbuilder": { "version": "9.0.7", "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-9.0.7.tgz", - "integrity": "sha512-7YXTQc3P2l9+0rjaUbLwMKRhtmwg1M1eDf6nag7urC7pIPYLD9W/jmzQ4ptRSUbodw5S0jfoGTflLemQibSpeQ==" + "integrity": "sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0=" } } }, @@ -6245,7 +6245,7 @@ "binary-search-bounds": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/binary-search-bounds/-/binary-search-bounds-2.0.3.tgz", - "integrity": "sha512-GMGnMG7owGQwTBNWdOnWvU6KC2k/lULWdbdXq95kxJHdTzeDa9x8QNwMXCv/7pfPGx9Zdi1nmCDZ0j+Tmk+vRg==" + "integrity": "sha1-X/hhbW3SylOIvIWy1iZuK52lAtw=" }, "bl": { "version": "4.1.0", @@ -6388,7 +6388,7 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "tough-cookie": { "version": "2.5.0", @@ -7425,7 +7425,7 @@ "browserify-mime": { "version": "1.2.9", "resolved": "https://registry.npmjs.org/browserify-mime/-/browserify-mime-1.2.9.tgz", - "integrity": "sha512-uz+ItyJXBLb6wgon1ELEiVowJBEsy03PUWGRQU7cxxx9S+DW2hujPp+DaMYEOClRPzsn7NB99NtJ6pGnt8y+CQ==" + "integrity": "sha1-rrGvKN5sDXpqLOQK22j/GEIq8x8=" }, "browserslist": { "version": "4.19.1", @@ -8331,7 +8331,7 @@ "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha512-TIGnTpdo+E3+pCyAluZvtED5p5wCqLdezCyhPZzKPcxvFplEt4i+W7OONCKgeZFT3+y5NZZfOOS/Bdcanm1MYA==" + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" } } }, @@ -10745,7 +10745,7 @@ "semaphore": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/semaphore/-/semaphore-1.0.5.tgz", - "integrity": "sha512-15WnK4TxpOk33fL0UoDnJ5myIWwJiodIZHtPRBoSxcaADt1Tm7kxEERd8n0vsw6OWsXwCCeROjSKU9MqfHaS1A==" + "integrity": "sha1-tJJXbmavGT25XWXiXsU/Xxl5jWA=" }, "tunnel": { "version": "0.0.5", @@ -10755,7 +10755,7 @@ "underscore": { "version": "1.8.3", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.8.3.tgz", - "integrity": "sha512-5WsVTFcH1ut/kkhAaHf4PVgI8c7++GiVcpCGxPouI6ZVjsqPnSDf8h/8HtVqc0t4fzRXwnMK70EcZeAs3PIddg==" + "integrity": "sha1-Tz+1OxBuYJf8+ctBCfKl6b36UCI=" } } }, @@ -11641,7 +11641,7 @@ "event-emitter": { "version": "0.3.5", "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", - "integrity": "sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA==", + "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", "requires": { "d": "1", "es5-ext": "~0.10.14" @@ -13860,7 +13860,7 @@ "inflection": { "version": "1.12.0", "resolved": "https://registry.npmjs.org/inflection/-/inflection-1.12.0.tgz", - "integrity": "sha512-lRy4DxuIFWXlJU7ed8UiTJOSTqStqYdEb4CEbtXfNbkdj3nH1L+reUWiE10VWcJS2yR7tge8Z74pJjtBjNwj0w==" + "integrity": "sha1-ogCTVlbW9fa8TcdQLhrstwMihBY=" }, "inflight": { "version": "1.0.6", @@ -14082,7 +14082,7 @@ "int64-buffer": { "version": "0.1.10", "resolved": "https://registry.npmjs.org/int64-buffer/-/int64-buffer-0.1.10.tgz", - "integrity": "sha512-v7cSY1J8ydZ0GyjUHqF+1bshJ6cnEVLo9EnjB8p+4HDRPZc9N5jjmvUV7NvEsqQOKyH0pmIBFWXVQbiS0+OBbA==" + "integrity": "sha1-J3siiofZWtd30HwTgyAiQGpHNCM=" }, "internal-slot": { "version": "1.0.3", @@ -14930,7 +14930,7 @@ "json-edm-parser": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/json-edm-parser/-/json-edm-parser-0.1.2.tgz", - "integrity": "sha512-J1U9mk6lf8dPULcaMwALXB6yel3cJyyhk9Z8FQ4sMwiazNwjaUhegIcpZyZFNMvLRtnXwh+TkCjX9uYUObBBYA==", + "integrity": "sha1-HmCw/vG8CvZ7wNFG393lSGzWFbQ=", "requires": { "jsonparse": "~1.2.0" } @@ -16214,7 +16214,7 @@ "lru-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/lru-queue/-/lru-queue-0.1.0.tgz", - "integrity": "sha512-BpdYkt9EvGl8OfWHDQPISVpcl5xZthb+XPsbELj5AQXxIC8IriDZIQYjBJPEm5rS420sjZ0TLEzRcq5KdBhYrQ==", + "integrity": "sha1-Jzi9nw089PhEkMVzbEhpmsYyzaM=", "requires": { "es5-ext": "~0.10.2" } @@ -16328,7 +16328,7 @@ "md5.js": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.4.tgz", - "integrity": "sha512-cBB1zpoIg3GaI1IJyoc715xmWofOcXbMhVHAR/TLYk+RO+Y6xQfAp+UshPZTojAcju91Jyk5fw4CiLBuBVaQjw==", + "integrity": "sha1-6b296UogpawYsENA/Fdk1bCdkB0=", "requires": { "hash-base": "^3.0.0", "inherits": "^2.0.1" @@ -20986,7 +20986,7 @@ "priorityqueuejs": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/priorityqueuejs/-/priorityqueuejs-1.0.0.tgz", - "integrity": "sha512-lg++21mreCEOuGWTbO5DnJKAdxfjrdN0S9ysoW9SzdSJvbkWpkaDdpG/cdsPCsEnoLUwmd9m3WcZhngW7yKA2g==" + "integrity": "sha1-LuTyPCVgkT4IwHzlzN1t498sWvg=" }, "prism-media": { "version": "1.3.1", @@ -22897,7 +22897,7 @@ "is-fullwidth-code-point": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz", - "integrity": "sha512-VHskAKYM8RfSFXwee5t5cbN5PZeq1Wrh6qd5bkyiXIf6UQcN6w/A0eXM9r6t8d+GYOh+o6ZhiEnb88LN/Y8m2w==" + "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=" }, "locate-path": { "version": "3.0.0", @@ -22932,7 +22932,7 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha512-bpC7GYwiDYQ4wYLe+FA8lhRjhQCMcQGuSgGGqDkg/QerRWw9CmGRT0iSOVRSZJ29NMLZgIzqaljJ63oaL4NIJQ==" + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" }, "require-main-filename": { "version": "2.0.0", @@ -23414,7 +23414,7 @@ "simple-swizzle": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", - "integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==", + "integrity": "sha1-pNprY1/8zMoz9w0Xy5JZLeleVXo=", "requires": { "is-arrayish": "^0.3.1" } @@ -26654,7 +26654,7 @@ "y18n": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.1.tgz", - "integrity": "sha512-Vd1yWKYGMtzFB6bAuTI7/POwJnwQStQXOe1PW1GmjUZgkaKYGc6/Pl3IDGFgplEklF65niuwBHeS5yve4+U01Q==" + "integrity": "sha1-bRX7qITAhnnA136I53WegR4H+kE=" }, "yaeti": { "version": "0.0.6", diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 23853620..798f741d 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -637,17 +637,15 @@ export class GBMinService { await min.whatsAppDirectLine.setup(true); } else { const minBoot = GBServer.globals.minBoot as any; - if (minBoot.instance.whatsappServiceUrl) { - min.whatsAppDirectLine = new WhatsappDirectLine( - min, - min.botId, - min.instance.whatsappBotKey, - minBoot.instance.whatsappServiceKey, - minBoot.instance.whatsappServiceNumber, - minBoot.instance.whatsappServiceUrl - ); - await min.whatsAppDirectLine.setup(false); - } + min.whatsAppDirectLine = new WhatsappDirectLine( + min, + min.botId, + min.instance.whatsappBotKey, + minBoot.instance.whatsappServiceKey, + minBoot.instance.whatsappServiceNumber, + minBoot.instance.whatsappServiceUrl + ); + await min.whatsAppDirectLine.setup(false); } // Setups default BOT Framework dialogs. diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 2023621e..69b2764b 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -121,66 +121,76 @@ export class WhatsappDirectLine extends GBService { switch (this.provider) { case 'GeneralBots': - // Initialize the browser using a local profile for each bot. + const minBoot = GBServer.globals.minBoot as any; + if (minBoot.botId !== this.botId) { - const gbaiName = `${this.min.botId}.gbai`; - let localName = Path.join('work', gbaiName, 'profile'); - let client = this.customClient = new Client({ - authStrategy: new LocalAuth({ - clientId: this.min.botId, - dataPath: localName - }), - puppeteer: { - headless: false, args: ['--disable-features=site-per-process', - `--user-data-dir=${localName}`] - } - }); - client.initialize(); + this.customClient = minBoot.whatsAppDirectLine.customClient; - // Dispatches messages to the received method. + } + else { - client.on('message', (async message => { - await this.WhatsAppCallback(message, null); - }).bind(this)); + // Initialize the browser using a local profile for each bot. - client.on('qr', (async (qr) => { + const gbaiName = `${this.min.botId}.gbai`; + let localName = Path.join('work', gbaiName, 'profile'); + let client = this.customClient = new Client({ + authStrategy: new LocalAuth({ + clientId: this.min.botId, + dataPath: localName + }), + puppeteer: { + headless: false, args: ['--disable-features=site-per-process', + `--user-data-dir=${localName}`] + } + }); + client.initialize(); - const adminNumber = this.min.core.getParam(this.min.instance, 'Bot Admin Number', null); - const adminEmail = this.min.core.getParam(this.min.instance, 'Bot Admin E-mail', null); - - // Sends QR Code to boot bot admin. - - const msg = `Please, scan QR Code with for bot ${this.botId}.`; - GBLog.info(msg); - qrcode.generate(qr, { small: true, scale: 0.5 }); + // Dispatches messages to the received method. - // While handling other bots uses boot instance of this class to send QR Codes. + client.on('message', (async message => { + await this.WhatsAppCallback(message, null); + }).bind(this)); - if (this.botId !== GBServer.globals.minBoot.botId) { + client.on('qr', (async (qr) => { - const s = new DialogKeywords(null, null, null, null); - const qrBuf = await s.getQRCode(qr); - const gbaiName = `${this.min.botId}.gbai`; - const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`); - fs.writeFileSync(localName, qrBuf); - const url = urlJoin( - GBServer.globals.publicAddress, - this.min.botId, - 'cache', - Path.basename(localName) - ); - GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg); - s.sendEmail(adminEmail, `Check your WhatsApp for bot ${this.botId}`, msg); - } + const adminNumber = this.min.core.getParam(this.min.instance, 'Bot Admin Number', null); + const adminEmail = this.min.core.getParam(this.min.instance, 'Bot Admin E-mail', null); - }).bind(this)); + // Sends QR Code to boot bot admin. - client.on('authenticated', () => { - GBLog.info(`WhatsApp QR Code authenticated for ${this.botId}.`); - }); + const msg = `Please, scan QR Code with for bot ${this.botId}.`; + GBLog.info(msg); + qrcode.generate(qr, { small: true, scale: 0.5 }); - setUrl = false; + + // While handling other bots uses boot instance of this class to send QR Codes. + + if (this.botId !== GBServer.globals.minBoot.botId) { + + const s = new DialogKeywords(null, null, null, null); + const qrBuf = await s.getQRCode(qr); + const gbaiName = `${this.min.botId}.gbai`; + const localName = Path.join('work', gbaiName, 'cache', `qr${GBAdminService.getRndReadableIdentifier()}.png`); + fs.writeFileSync(localName, qrBuf); + const url = urlJoin( + GBServer.globals.publicAddress, + this.min.botId, + 'cache', + Path.basename(localName) + ); + GBServer.globals.minBoot.whatsAppDirectLine.sendFileToDevice(adminNumber, url, Path.basename(localName), msg); + s.sendEmail(adminEmail, `Check your WhatsApp for bot ${this.botId}`, msg); + } + + }).bind(this)); + + client.on('authenticated', () => { + GBLog.info(`WhatsApp QR Code authenticated for ${this.botId}.`); + }); + + setUrl = false; + } break; case 'chatapi': @@ -223,11 +233,11 @@ export class WhatsappDirectLine extends GBService { break; } - if (setUrl) { + if (setUrl && options) { const express = require('express'); GBServer.globals.server.use(`/audios`, express.static('work')); - + try { const res = await request.post(options); } catch (error) { @@ -647,8 +657,8 @@ export class WhatsappDirectLine extends GBService { let options; switch (this.provider) { case 'GeneralBots': - const attachment = MessageMedia.fromUrl(url); - await this.customClient.sendMessage(to += '@c.us', attachment, { caption: caption }); + const attachment = await MessageMedia.fromUrl(url); + await this.customClient.sendMessage(to + '@c.us', attachment, { caption: caption }); break; case 'chatapi': @@ -698,12 +708,14 @@ export class WhatsappDirectLine extends GBService { break; } - try { - // tslint:disable-next-line: await-promise - const result = await request.post(options); - GBLog.info(`File ${url} sent to ${to}: ${result}`); - } catch (error) { - GBLog.error(`Error sending file to Whatsapp provider ${error.message}`); + if (options) { + try { + // tslint:disable-next-line: await-promise + const result = await request.post(options); + GBLog.info(`File ${url} sent to ${to}: ${result}`); + } catch (error) { + GBLog.error(`Error sending file to Whatsapp provider ${error.message}`); + } } } @@ -860,7 +872,7 @@ export class WhatsappDirectLine extends GBService { switch (provider) { case "GeneralBots": - id = req.author.split('@')[0]; + id = req.from.split('@')[0]; senderName = req._data.notifyName; text = req.body; @@ -997,8 +1009,11 @@ export class WhatsappDirectLine extends GBService { id, `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.` , null); - res.end(); + if (res) { + res.end(); + } } + } else { // User wants to switch bots. @@ -1034,7 +1049,10 @@ export class WhatsappDirectLine extends GBService { null ); } - res.end(); + if (res) { + res.end(); + } + } else { activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; if (activeMin === undefined) { diff --git a/src/app.ts b/src/app.ts index 90638a58..3c0e53e9 100644 --- a/src/app.ts +++ b/src/app.ts @@ -81,7 +81,7 @@ export class GBServer { */ public static run() { - + GBLog.info(`The Bot Server is in STARTING mode...`); GBServer.globals = new RootData(); @@ -112,6 +112,7 @@ export class GBServer { try { GBLog.info(`Now accepting connections on ${port}...`); + process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // Reads basic configuration, initialize minimal services.