@open-wa/wa-automate 4.28.4 → 4.28.8

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.
@@ -799,6 +799,12 @@ export declare class Client {
799
799
  * @returns Boolean
800
800
  */
801
801
  isConnected(): Promise<boolean>;
802
+ /**
803
+ * Logs out from the session.
804
+ *
805
+ * Please be careful when using this as it can exit the whole process depending on your config
806
+ */
807
+ logout(): Promise<boolean>;
802
808
  /**
803
809
  * Retrieves Battery Level
804
810
  * @returns Number
@@ -427,9 +427,9 @@ class Client {
427
427
  return rest;
428
428
  }
429
429
  pup(pageFunction, ...args) {
430
- var _a, _b;
430
+ var _a, _b, _c, _d, _e;
431
431
  return __awaiter(this, void 0, void 0, function* () {
432
- const { safeMode, callTimeout, idChecking, logFile, logging } = this._createConfig;
432
+ const { safeMode, callTimeout, idCorrection, logging } = this._createConfig;
433
433
  let _t;
434
434
  if (safeMode) {
435
435
  if (!this._page || this._page.isClosed())
@@ -438,19 +438,34 @@ class Client {
438
438
  if (state !== model_1.STATE.CONNECTED)
439
439
  throw new errors_1.CustomError(errors_1.ERROR_NAME.STATE_ERROR, `state: ${state}`);
440
440
  }
441
- if (idChecking && args[0]) {
442
- Object.entries(args[0]).map(([k, v]) => {
443
- if (["to", "chatId", "groupChatId", "groupId", "contactId"].includes(k) && typeof v == "string" && v) {
444
- args[0][k] = (v === null || v === void 0 ? void 0 : v.includes('-')) ?
445
- //it is a group chat, make sure it has a @g.us at the end
446
- `${v === null || v === void 0 ? void 0 : v.replace(/@(c|g).us/g, '')}@g.us` :
447
- //it is a normal chat, make sure it has a @c.us at the end
448
- `${v === null || v === void 0 ? void 0 : v.replace(/@(c|g).us/g, '')}@c.us`;
449
- }
450
- });
441
+ if (idCorrection && args[0]) {
442
+ const fixId = (id) => {
443
+ var _a;
444
+ let isGroup = false;
445
+ let scrubbedId = (_a = id === null || id === void 0 ? void 0 : id.match(/\d|-/g)) === null || _a === void 0 ? void 0 : _a.join('');
446
+ scrubbedId = scrubbedId.match(/-/g) && scrubbedId.match(/-/g).length == 1 && scrubbedId.split('-')[1].length === 10 ? scrubbedId : scrubbedId.replace(/-/g, '');
447
+ if (scrubbedId.includes('-') || scrubbedId.length === 18)
448
+ isGroup = true;
449
+ const fixed = isGroup ?
450
+ `${scrubbedId === null || scrubbedId === void 0 ? void 0 : scrubbedId.replace(/@(c|g).us/g, '')}@g.us` :
451
+ `${scrubbedId === null || scrubbedId === void 0 ? void 0 : scrubbedId.replace(/@(c|g).us/g, '')}@c.us`;
452
+ logging_1.log.info('Fixed ID', { id, fixed });
453
+ return fixed;
454
+ };
455
+ if (typeof args[0] === 'string' && args[0] && !(args[0].includes("@g.us") || args[0].includes("@c.us")) && ((_c = (_b = (((_a = pageFunction === null || pageFunction === void 0 ? void 0 : pageFunction.toString()) === null || _a === void 0 ? void 0 : _a.match(/[^(]*\(([^)]*)\)/)[1]) || "")) === null || _b === void 0 ? void 0 : _b.replace(/\s/g, '')) === null || _c === void 0 ? void 0 : _c.split(','))) {
456
+ const p = ((pageFunction === null || pageFunction === void 0 ? void 0 : pageFunction.toString().match(/[^(]*\(([^)]*)\)/)[1]) || "").replace(/\s/g, '').split(',');
457
+ if (["to", "chatId", "groupChatId", "groupId", "contactId"].includes(p[0]))
458
+ args[0] = fixId(args[0]);
459
+ }
460
+ else if (typeof args[0] === 'object')
461
+ Object.entries(args[0]).map(([k, v]) => {
462
+ if (["to", "chatId", "groupChatId", "groupId", "contactId"].includes(k) && typeof v == "string" && v && !(v.includes("@g.us") || v.includes("@c.us"))) {
463
+ args[0][k] = fixId(v);
464
+ }
465
+ });
451
466
  }
452
467
  if (logging) {
453
- const wapis = (_b = (((_a = pageFunction === null || pageFunction === void 0 ? void 0 : pageFunction.toString()) === null || _a === void 0 ? void 0 : _a.match(/WAPI\.(\w*)\(/g)) || [])) === null || _b === void 0 ? void 0 : _b.map(s => s.replace(/WAPI|\.|\(/g, ''));
468
+ const wapis = (_e = (((_d = pageFunction === null || pageFunction === void 0 ? void 0 : pageFunction.toString()) === null || _d === void 0 ? void 0 : _d.match(/WAPI\.(\w*)\(/g)) || [])) === null || _e === void 0 ? void 0 : _e.map(s => s.replace(/WAPI|\.|\(/g, ''));
454
469
  _t = Date.now();
455
470
  logging_1.log.info(`Request ${_t}`, Object.assign({ _method: (wapis === null || wapis === void 0 ? void 0 : wapis.length) === 1 ? wapis[0] : wapis }, args[0]));
456
471
  }
@@ -1810,6 +1825,16 @@ class Client {
1810
1825
  return yield this.pup(() => WAPI.isConnected());
1811
1826
  });
1812
1827
  }
1828
+ /**
1829
+ * Logs out from the session.
1830
+ *
1831
+ * Please be careful when using this as it can exit the whole process depending on your config
1832
+ */
1833
+ logout() {
1834
+ return __awaiter(this, void 0, void 0, function* () {
1835
+ return yield this.pup(() => WAPI.logout());
1836
+ });
1837
+ }
1813
1838
  /**
1814
1839
  * Retrieves Battery Level
1815
1840
  * @returns Number
@@ -84,7 +84,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
84
84
  const _u = new URL(u);
85
85
  const origin = _u.origin;
86
86
  const port = _u.port || 80;
87
- const [accountId, inboxId] = u.match(/\/(app|(api\/v1))\/accounts\/\d*\/inbox\/\d*/g)[0].split('/').filter(Number);
87
+ const [accountId, inboxId] = u.match(/\/(app|(api\/v1))\/accounts\/\d*\/(inbox|inboxes)\/\d*/g)[0].split('/').filter(Number);
88
88
  // const accountId = u.match(/accounts\/\d*/g) && u.match(/accounts\/\d*/g)[0].replace('accounts/', '')
89
89
  const resolvedInbox = inboxId || u.match(/inboxes\/\d*/g) && u.match(/inboxes\/\d*/g)[0].replace('inboxes/', '');
90
90
  const cwReq = (path, method, data, _headers) => {
@@ -115,6 +115,8 @@ function smartQr(waPage, config, spinner) {
115
115
  return true;
116
116
  let hash = 'START';
117
117
  const grabAndEmit = (qrData) => __awaiter(this, void 0, void 0, function* () {
118
+ if (!qrNum && browser_1.BROWSER_START_TS)
119
+ spinner.info(`First QR: ${Date.now() - browser_1.BROWSER_START_TS} ms`);
118
120
  if (qrData) {
119
121
  if (!config.qrLogSkip)
120
122
  qrcode.generate(qrData, { small: true });
@@ -1,6 +1,7 @@
1
1
  import { Browser, Page } from 'puppeteer';
2
2
  import { Spin } from './events';
3
3
  import { ConfigObject } from '../api/model';
4
+ export declare let BROWSER_START_TS: number;
4
5
  export declare function initPage(sessionId?: string, config?: ConfigObject, customUserAgent?: string, spinner?: Spin, _page?: Page, skipAuth?: boolean): Promise<Page>;
5
6
  export declare const deleteSessionData: (config: ConfigObject) => boolean;
6
7
  export declare const getSessionDataFilePath: (sessionId: string, config: ConfigObject) => string | boolean;
@@ -31,7 +31,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.kill = exports.injectApi = exports.addScript = exports.getSessionDataFilePath = exports.deleteSessionData = exports.initPage = void 0;
34
+ exports.kill = exports.injectApi = exports.addScript = exports.getSessionDataFilePath = exports.deleteSessionData = exports.initPage = exports.BROWSER_START_TS = void 0;
35
35
  const path = __importStar(require("path"));
36
36
  const fs = __importStar(require("fs"));
37
37
  const death_1 = __importDefault(require("death"));
@@ -45,6 +45,7 @@ const tree_kill_1 = __importDefault(require("tree-kill"));
45
45
  const logging_1 = require("../logging/logging");
46
46
  const tools_1 = require("../utils/tools");
47
47
  let browser;
48
+ exports.BROWSER_START_TS = 0;
48
49
  function initPage(sessionId, config, customUserAgent, spinner, _page, skipAuth) {
49
50
  var _a, _b, _c, _d, _e;
50
51
  return __awaiter(this, void 0, void 0, function* () {
@@ -58,7 +59,7 @@ function initPage(sessionId, config, customUserAgent, spinner, _page, skipAuth)
58
59
  let waPage = _page;
59
60
  if (!waPage) {
60
61
  spinner === null || spinner === void 0 ? void 0 : spinner.info('Launching Browser');
61
- browser = yield initBrowser(sessionId, config);
62
+ browser = yield initBrowser(sessionId, config, spinner);
62
63
  waPage = yield getWAPage(browser);
63
64
  }
64
65
  spinner === null || spinner === void 0 ? void 0 : spinner.info('Setting Up Page');
@@ -281,7 +282,7 @@ function injectApi(page) {
281
282
  });
282
283
  }
283
284
  exports.injectApi = injectApi;
284
- function initBrowser(sessionId, config = {}) {
285
+ function initBrowser(sessionId, config = {}, spinner) {
285
286
  var _a, _b, _c;
286
287
  return __awaiter(this, void 0, void 0, function* () {
287
288
  if (config === null || config === void 0 ? void 0 : config.raspi) {
@@ -327,13 +328,17 @@ function initBrowser(sessionId, config = {}) {
327
328
  if (config === null || config === void 0 ? void 0 : config.multiDevice) {
328
329
  args = args.filter(x => x != '--incognito');
329
330
  config["userDataDir"] = config["userDataDir"] || `${(config === null || config === void 0 ? void 0 : config.inDocker) ? '/sessions' : (config === null || config === void 0 ? void 0 : config.sessionDataPath) || '.'}/_IGNORE_${(config === null || config === void 0 ? void 0 : config.sessionId) || 'session'}`;
331
+ spinner === null || spinner === void 0 ? void 0 : spinner.info('MD Enabled, turning off incognito mode.');
332
+ spinner === null || spinner === void 0 ? void 0 : spinner.info(`Data dir: ${config["userDataDir"]}`);
330
333
  }
331
334
  if (config === null || config === void 0 ? void 0 : config.corsFix)
332
335
  args.push('--disable-web-security');
333
336
  if (config["userDataDir"] && !fs.existsSync(config["userDataDir"])) {
337
+ spinner === null || spinner === void 0 ? void 0 : spinner.info(`Data dir doesnt exist, creating...: ${config["userDataDir"]}`);
334
338
  fs.mkdirSync(config["userDataDir"], { recursive: true });
335
339
  }
336
340
  const browser = (config === null || config === void 0 ? void 0 : config.browserWSEndpoint) ? yield puppeteer.connect(Object.assign({}, config)) : yield puppeteer.launch(Object.assign(Object.assign({ headless: true, args }, config), { devtools: false }));
341
+ exports.BROWSER_START_TS = Date.now();
337
342
  //devtools
338
343
  if (config === null || config === void 0 ? void 0 : config.devtools) {
339
344
  const _dt = yield Promise.resolve().then(() => __importStar(require('puppeteer-extra-plugin-devtools')));
@@ -352,11 +357,10 @@ function initBrowser(sessionId, config = {}) {
352
357
  // const tunnel = await devtools.createTunnel(browser);
353
358
  const tunnel = config.devtools == 'local' ? devtools.getLocalDevToolsUrl(browser) : (yield devtools.createTunnel(browser)).url;
354
359
  const l = `\ndevtools URL: ${typeof config.devtools == 'object' ? Object.assign(Object.assign({}, config.devtools), { tunnel }) : tunnel}`;
355
- console.log(l);
356
- logging_1.log.info(l);
360
+ spinner.info(l);
357
361
  }
358
362
  catch (error) {
359
- console.error("initBrowser -> error", error);
363
+ spinner.fail(error);
360
364
  logging_1.log.error("initBrowser -> error", error);
361
365
  }
362
366
  }
@@ -171,7 +171,7 @@ function create(config = {}) {
171
171
  config.multiDevice = true;
172
172
  }
173
173
  if ((config === null || config === void 0 ? void 0 : config.multiDevice) && (config === null || config === void 0 ? void 0 : config.chromiumArgs))
174
- spinner.info(`Using custom chromium args with multi device will cause issues! Please remove themm`);
174
+ spinner.info(`Using custom chromium args with multi device will cause issues! Please remove them: ${config === null || config === void 0 ? void 0 : config.chromiumArgs}`);
175
175
  if ((config === null || config === void 0 ? void 0 : config.multiDevice) && !(config === null || config === void 0 ? void 0 : config.useChrome))
176
176
  spinner.info(`It is recommended to set useChrome: true or use the --use-chrome flag if you are experiencing issues with Multi device support`);
177
177
  waPage = yield (0, browser_1.initPage)(sessionId, config, customUserAgent, spinner);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.28.4",
3
+ "version": "4.28.8",
4
4
  "licenseCheckUrl": "https://openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",