@open-wa/wa-automate 4.54.4 → 4.54.6

Sign up to get free protection for your applications and to get access to all the features.
package/dist/cli/index.js CHANGED
@@ -20,7 +20,6 @@ const axios_1 = __importDefault(require("axios"));
20
20
  const setup_1 = require("./setup");
21
21
  const collections_1 = require("./collections");
22
22
  const server_1 = require("./server");
23
- const localtunnel_1 = __importDefault(require("localtunnel"));
24
23
  const chatwoot_1 = require("./integrations/chatwoot");
25
24
  let checkUrl = (s) => (typeof s === "string") && (0, is_url_superb_1.default)(s);
26
25
  const ready = (config) => __awaiter(void 0, void 0, void 0, function* () {
@@ -105,6 +104,7 @@ function start() {
105
104
  }
106
105
  try {
107
106
  const client = yield (0, index_1.create)(Object.assign({}, createConfig));
107
+ (0, server_1.setupMetaProcessMiddleware)(client, cliConfig);
108
108
  yield (0, server_1.setupHttpServer)(cliConfig);
109
109
  if (cliConfig.autoReject) {
110
110
  yield client.autoReject(cliConfig.onCall);
@@ -201,13 +201,8 @@ function start() {
201
201
  });
202
202
  if (cliConfig.tunnel) {
203
203
  spinner.info(`\n• Setting up external tunnel`);
204
- const tunnel = yield (0, localtunnel_1.default)({
205
- port: PORT,
206
- host: process.env.WA_TUNNEL_SERVER || "https://public.openwa.cloud",
207
- subdomain: yield client.getTunnelCode()
208
- });
209
- cliConfig.apiHost = cliConfig.tunnel = tunnel.url;
210
- spinner.succeed(`\n\t${(0, terminal_link_1.default)('External address', tunnel.url)}`);
204
+ const tunnelUrl = yield (0, server_1.setupTunnel)(cliConfig, yield client.getTunnelCode(), PORT);
205
+ spinner.succeed(`\n\t${(0, terminal_link_1.default)('External address', tunnelUrl)}`);
211
206
  }
212
207
  const apiDocsUrl = cliConfig.apiHost ? `${cliConfig.apiHost}/api-docs/ ` : `${cliConfig.host.includes('http') ? '' : 'http://'}${cliConfig.host}:${PORT}/api-docs/ `;
213
208
  const link = (0, terminal_link_1.default)('API Explorer', apiDocsUrl);
@@ -168,10 +168,10 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
168
168
  __1.log.info(`CHATWOOT INTEGRATION: inbox created. id: ${inboxId}`);
169
169
  }
170
170
  }
171
- const cwReq = (method, path, data, _headers) => {
171
+ const cwReq = (method, path, data, _headers) => __awaiter(void 0, void 0, void 0, function* () {
172
172
  const url = `${origin}/api/v1/accounts/${accountId}/${path}`.replace('app.bentonow.com', 'chat.bentonow.com');
173
173
  // console.log(url,method,data)
174
- return (0, axios_1.default)({
174
+ const response = yield (0, axios_1.default)({
175
175
  method,
176
176
  data,
177
177
  url,
@@ -181,7 +181,9 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
181
181
  __1.log.error(`CW REQ ERROR: ${(_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.status} ${(_b = error === null || error === void 0 ? void 0 : error.response) === null || _b === void 0 ? void 0 : _b.message}`, error === null || error === void 0 ? void 0 : error.toJSON());
182
182
  throw error;
183
183
  });
184
- };
184
+ __1.log.info(`CW REQUEST: ${response.status} ${method} ${url} ${JSON.stringify(data)}`);
185
+ return response;
186
+ });
185
187
  let { data: get_inbox } = yield cwReq('get', `inboxes/${inboxId}`);
186
188
  /**
187
189
  * Update the webhook
@@ -13,9 +13,11 @@ export declare const setupAuthenticationLayer: (cliConfig: cliFlags) => void;
13
13
  export declare const setupApiDocs: (cliConfig: cliFlags) => void;
14
14
  export declare const setupSwaggerStatsMiddleware: (cliConfig: cliFlags) => Promise<void>;
15
15
  export declare const setupRefocusDisengageMiddleware: (cliConfig: cliFlags) => void;
16
+ export declare const setupMetaProcessMiddleware: (client: Client, cliConfig: any) => void;
16
17
  export declare const getCommands: () => any;
17
18
  export declare const listListeners: () => string[];
18
19
  export declare const setupMediaMiddleware: () => void;
20
+ export declare const setupTunnel: (cliConfig: any, tunnelCode: string, PORT: number) => Promise<string>;
19
21
  export declare const setupTwilioCompatibleWebhook: (cliConfig: cliFlags, client: Client) => void;
20
22
  export declare const setupChatwoot: (cliConfig: cliFlags, client: Client) => void;
21
23
  export declare const setupBotPressHandler: (cliConfig: cliFlags, client: Client) => void;
@@ -35,7 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.setupHttpServer = exports.server = exports.app = void 0;
38
+ exports.setupSocketServer = exports.setupBotPressHandler = exports.setupChatwoot = exports.setupTwilioCompatibleWebhook = exports.setupTunnel = exports.setupMediaMiddleware = exports.listListeners = exports.getCommands = exports.setupMetaProcessMiddleware = exports.setupRefocusDisengageMiddleware = exports.setupSwaggerStatsMiddleware = exports.setupApiDocs = exports.setupAuthenticationLayer = exports.enableCORSRequests = exports.setUpExpressApp = exports.setupHttpServer = exports.server = exports.app = void 0;
39
39
  //@ts-ignore
40
40
  const express_1 = __importDefault(require("express"));
41
41
  const https_1 = __importDefault(require("https"));
@@ -52,8 +52,11 @@ const xmlbuilder2_1 = require("xmlbuilder2");
52
52
  const chatwoot_1 = require("./integrations/chatwoot");
53
53
  const express_ipfilter_1 = require("express-ipfilter");
54
54
  const helmet_1 = __importDefault(require("helmet"));
55
+ const localtunnel_1 = __importDefault(require("localtunnel"));
56
+ const child_process_1 = require("child_process");
55
57
  exports.app = (0, express_1.default)();
56
58
  exports.server = http_1.default.createServer(exports.app);
59
+ let tunnel;
57
60
  const trimChatId = (chatId) => chatId.replace("@c.us", "").replace("@g.us", "");
58
61
  const socketListenerCallbacks = {};
59
62
  // const existingListeners = () => Object.keys(Object.keys(socketListenerCallbacks).flatMap(id=>Object.keys(socketListenerCallbacks[id])).reduce((acc,curr)=>{acc[curr]=true;return acc},{}))
@@ -243,6 +246,69 @@ const setupMetaMiddleware = () => {
243
246
  }
244
247
  }));
245
248
  };
249
+ const setupMetaProcessMiddleware = (client, cliConfig) => {
250
+ /**
251
+ * Kill the client. End the process.
252
+ */
253
+ let closing = false;
254
+ const nuke = (req, res, restart) => __awaiter(void 0, void 0, void 0, function* () {
255
+ res.set("Connection", "close");
256
+ res.send(closing ? `Already closing! Stop asking!!` : 'Closing after connections closed. Waiting max 5 seconds');
257
+ res.end();
258
+ res.connection.end();
259
+ res.connection.destroy();
260
+ if (closing)
261
+ return;
262
+ closing = true;
263
+ yield client.kill("API_KILL");
264
+ __1.log.info("Waiting for maximum ");
265
+ if (tunnel && tunnel.close && typeof tunnel.close === 'function')
266
+ tunnel.close();
267
+ yield Promise.race([
268
+ new Promise((resolve) => exports.server.close(() => {
269
+ console.log('Server closed');
270
+ resolve(true);
271
+ })),
272
+ new Promise(resolve => setTimeout(resolve, 5000, 'timeout'))
273
+ ]);
274
+ if (process.env.pm_id && process.env.PM2_USAGE) {
275
+ const cmd = `pm2 ${restart ? 'restart' : 'stop'} ${process.env.pm_id}`;
276
+ __1.log.info(`PM2 DETECTED, RUNNING COMMAND: ${cmd}`);
277
+ const cmda = cmd.split(' ');
278
+ (0, child_process_1.spawn)(cmda[0], cmda.splice(1), { stdio: 'inherit' });
279
+ }
280
+ else {
281
+ if (restart)
282
+ setTimeout(function () {
283
+ process.on("exit", function () {
284
+ (0, child_process_1.spawn)(process.argv.shift(), process.argv, {
285
+ cwd: process.cwd(),
286
+ detached: true,
287
+ stdio: "inherit"
288
+ });
289
+ });
290
+ process.exit();
291
+ }, 5000);
292
+ else
293
+ process.exit(restart ? 0 : 10);
294
+ }
295
+ });
296
+ /**
297
+ * Exit code 10 will prevent pm2 process from auto-restarting
298
+ */
299
+ exports.app.post('/meta/process/exit', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
300
+ return yield nuke(req, res, false);
301
+ }));
302
+ /**
303
+ * Will only restart if the process is managed by pm2
304
+ *
305
+ * Note: Only works when `--pm2` flag is enabled.
306
+ */
307
+ exports.app.post('/meta/process/restart', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
308
+ return yield nuke(req, res, true);
309
+ }));
310
+ };
311
+ exports.setupMetaProcessMiddleware = setupMetaProcessMiddleware;
246
312
  const getCommands = () => Object.entries(collections_1.collections['swagger'].paths).reduce((acc, [key, value]) => { var _a, _b, _c, _d; acc[key.replace("/", "")] = ((_d = (_c = (_b = (_a = value === null || value === void 0 ? void 0 : value.post) === null || _a === void 0 ? void 0 : _a.requestBody) === null || _b === void 0 ? void 0 : _b.content["application/json"]) === null || _c === void 0 ? void 0 : _c.example) === null || _d === void 0 ? void 0 : _d.args) || {}; return acc; }, {});
247
313
  exports.getCommands = getCommands;
248
314
  const listListeners = () => {
@@ -253,6 +319,16 @@ const setupMediaMiddleware = () => {
253
319
  exports.app.use("/media", express_1.default.static('media'));
254
320
  };
255
321
  exports.setupMediaMiddleware = setupMediaMiddleware;
322
+ const setupTunnel = (cliConfig, tunnelCode, PORT) => __awaiter(void 0, void 0, void 0, function* () {
323
+ tunnel = yield (0, localtunnel_1.default)({
324
+ port: PORT,
325
+ host: process.env.WA_TUNNEL_SERVER || "https://public.openwa.cloud",
326
+ subdomain: tunnelCode
327
+ });
328
+ cliConfig.apiHost = cliConfig.tunnel = tunnel.url;
329
+ return tunnel.url;
330
+ });
331
+ exports.setupTunnel = setupTunnel;
256
332
  const setupTwilioCompatibleWebhook = (cliConfig, client) => {
257
333
  const url = cliConfig.twilioWebhook;
258
334
  client.onMessage((message) => __awaiter(void 0, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.54.4",
3
+ "version": "4.54.6",
4
4
  "licenseCheckUrl": "https://funcs.openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://funcs.openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",