@open-wa/wa-automate 4.28.3 → 4.28.7

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) => {
@@ -40,7 +40,6 @@ const tools_1 = require("../utils/tools");
40
40
  const browser_1 = require("./browser");
41
41
  const axios_1 = __importDefault(require("axios"));
42
42
  const logging_1 = require("../logging/logging");
43
- const timeout = ms => new Promise(resolve => setTimeout(resolve, ms, 'timeout'));
44
43
  /**
45
44
  * Validates if client is authenticated
46
45
  * @returns true if is authenticated, false otherwise
@@ -132,7 +131,6 @@ function smartQr(waPage, config, spinner) {
132
131
  (0, tools_1.processSend)('ready');
133
132
  if (config.qrMax && qrNum >= config.qrMax) {
134
133
  spinner.info('QR Code limit reached, exiting...');
135
- yield timeout(3000);
136
134
  yield (0, browser_1.kill)(waPage, null, true, null, "QR_LIMIT_REACHED");
137
135
  }
138
136
  if (config.ezqr || config.inDocker) {
@@ -169,7 +167,7 @@ function smartQr(waPage, config, spinner) {
169
167
  const _hasDefaultStateYet = yield waPage.evaluate("!!(window.Store && window.Store.State && window.Store.State.Socket)");
170
168
  if (!_hasDefaultStateYet) {
171
169
  //expecting issue, take a screenshot then wait a few seconds before continuing
172
- yield timeout(2000);
170
+ yield (0, tools_1.timeout)(2000);
173
171
  }
174
172
  return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
175
173
  const funcName = '_smartQr';
@@ -380,9 +380,9 @@ function getWAPage(browser) {
380
380
  */
381
381
  const kill = (p, b, exit, pid, reason = "LAUNCH_KILL") => __awaiter(void 0, void 0, void 0, function* () {
382
382
  (0, tools_1.processSendData)({
383
- type: "close",
384
383
  reason
385
384
  });
385
+ (0, tools_1.timeout)(3000);
386
386
  const killBrowser = (browser) => __awaiter(void 0, void 0, void 0, function* () {
387
387
  if (!browser)
388
388
  return;
@@ -1,5 +1,6 @@
1
1
  import { DataURL } from '../api/model';
2
2
  import { AxiosRequestConfig } from 'axios';
3
+ export declare const timeout: (ms: any) => Promise<unknown>;
3
4
  /**
4
5
  * Use this to generate a more likely valid user agent. It makes sure it has the WA part and replaces any windows or linux os info with mac.
5
6
  * @param useragent Your custom user agent
@@ -25,4 +26,4 @@ export declare const getDUrl: (url: string, optionsOverride?: AxiosRequestConfig
25
26
  */
26
27
  export declare const base64MimeType: (dUrl: DataURL) => string;
27
28
  export declare const processSend: (message: string) => void;
28
- export declare const processSendData: (data?: any) => boolean;
29
+ export declare const processSendData: (data?: any) => void;
@@ -23,8 +23,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.processSendData = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = void 0;
26
+ exports.processSendData = exports.processSend = exports.base64MimeType = exports.getDUrl = exports.isDataURL = exports.isBase64 = exports.camelize = exports.without = exports.getConfigFromProcessEnv = exports.smartUserAgent = exports.timeout = void 0;
27
27
  const axios_1 = __importDefault(require("axios"));
28
+ //@ts-ignore
29
+ process.send = process.send || function () { };
30
+ const timeout = ms => new Promise(resolve => setTimeout(resolve, ms, 'timeout'));
31
+ exports.timeout = timeout;
28
32
  /**
29
33
  * Use this to generate a more likely valid user agent. It makes sure it has the WA part and replaces any windows or linux os info with mac.
30
34
  * @param useragent Your custom user agent
@@ -134,9 +138,10 @@ const processSend = (message) => {
134
138
  };
135
139
  exports.processSend = processSend;
136
140
  const processSendData = (data = {}) => {
137
- return process.send({
141
+ process.send({
138
142
  type: 'process:msg',
139
143
  data
140
144
  });
145
+ return;
141
146
  };
142
147
  exports.processSendData = processSendData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.28.3",
3
+ "version": "4.28.7",
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",