@open-wa/wa-automate 4.27.1 → 4.27.5

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.
package/dist/cli/index.js CHANGED
@@ -113,12 +113,6 @@ function start() {
113
113
  process.exit();
114
114
  }
115
115
  }));
116
- if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.chatwootUrl) {
117
- spinner.info('Setting Up Chatwoot handler');
118
- spinner.info('Make sure to set up the Chatwoot inbox webhook to the following path on this process: /chatwoot');
119
- yield server_1.setupChatwoot(cliConfig, client);
120
- spinner.succeed('Chatwoot handler set up successfully');
121
- }
122
116
  if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.botPressUrl) {
123
117
  spinner.info('Setting Up Botpress handler');
124
118
  server_1.setupBotPressHandler(cliConfig, client);
@@ -150,6 +144,12 @@ function start() {
150
144
  console.log(`Please use the following api key for requests as a header:\napi_key: ${cliConfig.key}`);
151
145
  server_1.setupAuthenticationLayer(cliConfig);
152
146
  }
147
+ if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.chatwootUrl) {
148
+ spinner.info('Setting Up Chatwoot handler');
149
+ spinner.info(`Make sure to set up the Chatwoot inbox webhook to the following path on this process: /chatwoot${cliConfig.key ? `?api_key=YOUR-API-KEY` : ''}`);
150
+ yield server_1.setupChatwoot(cliConfig, client);
151
+ spinner.succeed('Chatwoot handler set up successfully');
152
+ }
153
153
  server_1.setupRefocusDisengageMiddleware(cliConfig);
154
154
  if (cliConfig && cliConfig.generateApiDocs && collections_1.collections["swagger"]) {
155
155
  spinner.info('Setting Up API Explorer');
@@ -14,11 +14,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.setupChatwootOutgoingMessageHandler = exports.chatwootMiddleware = void 0;
16
16
  const axios_1 = __importDefault(require("axios"));
17
+ const form_data_1 = __importDefault(require("form-data"));
18
+ const mime_types_1 = __importDefault(require("mime-types"));
17
19
  const chatwootMiddleware = (cliConfig, client) => {
18
20
  return (req, res) => __awaiter(void 0, void 0, void 0, function* () {
19
21
  const processMesssage = () => __awaiter(void 0, void 0, void 0, function* () {
20
22
  const promises = [];
21
23
  const { body } = req;
24
+ if (!body)
25
+ return;
26
+ if (!body.conversation)
27
+ return;
22
28
  const m = body.conversation.messages[0];
23
29
  const contact = (body.conversation.meta.sender.phone_number || "").replace('+', '');
24
30
  if (body.message_type === "incoming" ||
@@ -81,16 +87,14 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
81
87
  const [accountId, inboxId] = u.match(/\/(app|(api\/v1))\/accounts\/\d*\/inbox\/\d*/g)[0].split('/').filter(Number);
82
88
  // const accountId = u.match(/accounts\/\d*/g) && u.match(/accounts\/\d*/g)[0].replace('accounts/', '')
83
89
  const resolvedInbox = inboxId || u.match(/inboxes\/\d*/g) && u.match(/inboxes\/\d*/g)[0].replace('inboxes/', '');
84
- const cwReq = (path, method, data) => {
90
+ const cwReq = (path, method, data, _headers) => {
85
91
  const url = `${origin}/api/v1/accounts/${accountId}/${path}`.replace('app.bentonow.com', 'chat.bentonow.com');
86
- console.log(url, method, data);
92
+ // console.log(url,method,data)
87
93
  return axios_1.default({
88
94
  method,
89
95
  data,
90
96
  url,
91
- headers: {
92
- api_access_token
93
- }
97
+ headers: Object.assign({ api_access_token }, _headers)
94
98
  });
95
99
  };
96
100
  const contactReg = {
@@ -128,7 +132,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
128
132
  const getContactConversation = (number) => __awaiter(void 0, void 0, void 0, function* () {
129
133
  try {
130
134
  const { data } = yield cwReq(`contacts/${contactReg[number]}/conversations`, 'get');
131
- return data.payload[0];
135
+ return data.payload.sort((a, b) => a.id - b.id)[0];
132
136
  }
133
137
  catch (error) {
134
138
  return;
@@ -140,7 +144,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
140
144
  contact_id,
141
145
  "inbox_id": resolvedInbox
142
146
  });
143
- return data.payload;
147
+ return data;
144
148
  }
145
149
  catch (error) {
146
150
  return;
@@ -173,6 +177,25 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
173
177
  return;
174
178
  }
175
179
  });
180
+ const sendAttachmentMessage = (content, contactId, message) => __awaiter(void 0, void 0, void 0, function* () {
181
+ // decrypt message
182
+ const file = yield client.decryptMedia(message);
183
+ let formData = new form_data_1.default();
184
+ formData.append('attachments[]', Buffer.from(file.split(',')[1], 'base64'), {
185
+ knownLength: 1,
186
+ filename: `${message.t}.${mime_types_1.default.extension(message.mimetype)}`,
187
+ contentType: (file.match(/[^:\s*]\w+\/[\w-+\d.]+(?=[;| ])/) || ["application/octet-stream"])[0]
188
+ });
189
+ formData.append('content', content);
190
+ formData.append('message_type', 'incoming');
191
+ try {
192
+ const { data } = yield cwReq(`conversations/${convoReg[contactId]}/messages`, 'post', formData, formData.getHeaders());
193
+ return data;
194
+ }
195
+ catch (error) {
196
+ return;
197
+ }
198
+ });
176
199
  // const inboxId = s.match(/conversations\/\d*/g) && s.match(/conversations\/\d*/g)[0].replace('conversations/','')
177
200
  /**
178
201
  * Update the chatwoot contact and conversation registries
@@ -209,6 +232,7 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
209
232
  * Does the conversation exist in
210
233
  */
211
234
  let text = message.body;
235
+ let hasAttachments = false;
212
236
  switch (message.type) {
213
237
  case 'location':
214
238
  text = `${message.lat},${message.lng}`;
@@ -221,14 +245,22 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
221
245
  case 'audio':
222
246
  case 'ptt':
223
247
  case 'video':
224
- if (message.cloudUrl)
248
+ if (message.cloudUrl) {
225
249
  text = `FILE:\t${message.cloudUrl}\n\nMESSAGE:\t${message.text}`;
250
+ }
251
+ else {
252
+ text = message.text;
253
+ hasAttachments = true;
254
+ }
226
255
  break;
227
256
  default:
228
257
  text = message.body || "__UNHANDLED__";
229
258
  break;
230
259
  }
231
- yield sendConversationMessage(text, message.from, message);
260
+ if (hasAttachments)
261
+ yield sendAttachmentMessage(text, message.from, message);
262
+ else
263
+ yield sendConversationMessage(text, message.from, message);
232
264
  }));
233
265
  });
234
266
  exports.setupChatwootOutgoingMessageHandler = setupChatwootOutgoingMessageHandler;
@@ -71,7 +71,10 @@ const setupAuthenticationLayer = (cliConfig) => {
71
71
  return next();
72
72
  }
73
73
  const apiKey = req.get('key') || req.get('api_key');
74
- if (!apiKey || apiKey !== cliConfig.key) {
74
+ if (req.path.includes('chatwoot') && req.query['api_key'] && req.query['api_key'] == cliConfig.key) {
75
+ next();
76
+ }
77
+ else if (!apiKey || apiKey !== cliConfig.key) {
75
78
  res.status(401).json({ error: 'unauthorised' });
76
79
  }
77
80
  else {
@@ -9,4 +9,4 @@ export declare function injectApi(page: Page): Promise<Page>;
9
9
  /**
10
10
  * @internal
11
11
  */
12
- export declare const kill: (p: Page, b?: Browser, exit?: boolean) => Promise<void>;
12
+ export declare const kill: (p: Page, b?: Browser, exit?: boolean, pid?: number) => Promise<void>;
@@ -290,6 +290,8 @@ function initBrowser(sessionId, config = {}) {
290
290
  if (!_savedPath) {
291
291
  const chromeLauncher = yield Promise.resolve().then(() => __importStar(require('chrome-launcher')));
292
292
  config.executablePath = chromeLauncher.Launcher.getInstallations()[0];
293
+ if (!config.executablePath)
294
+ delete config.executablePath;
293
295
  yield storage.setItem('executablePath', config.executablePath);
294
296
  }
295
297
  else
@@ -324,6 +326,9 @@ function initBrowser(sessionId, config = {}) {
324
326
  }
325
327
  if (config === null || config === void 0 ? void 0 : config.corsFix)
326
328
  args.push('--disable-web-security');
329
+ if (config["userDataDir"] && !fs.existsSync(config["userDataDir"])) {
330
+ fs.mkdirSync(config["userDataDir"], { recursive: true });
331
+ }
327
332
  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 }));
328
333
  //devtools
329
334
  if (config === null || config === void 0 ? void 0 : config.devtools) {
@@ -366,27 +371,27 @@ death_1.default(() => __awaiter(void 0, void 0, void 0, function* () {
366
371
  /**
367
372
  * @internal
368
373
  */
369
- const kill = (p, b, exit) => __awaiter(void 0, void 0, void 0, function* () {
374
+ const kill = (p, b, exit, pid) => __awaiter(void 0, void 0, void 0, function* () {
370
375
  const killBrowser = (browser) => __awaiter(void 0, void 0, void 0, function* () {
371
376
  if (!browser)
372
377
  return;
373
- const pid = (browser === null || browser === void 0 ? void 0 : browser.process()) ? browser === null || browser === void 0 ? void 0 : browser.process().pid : null;
378
+ pid = (browser === null || browser === void 0 ? void 0 : browser.process()) ? browser === null || browser === void 0 ? void 0 : browser.process().pid : null;
374
379
  if (!pid)
375
380
  return;
376
381
  if (!(p === null || p === void 0 ? void 0 : p.isClosed()))
377
382
  yield (p === null || p === void 0 ? void 0 : p.close());
378
383
  if (browser)
379
384
  yield (browser === null || browser === void 0 ? void 0 : browser.close().catch(() => { }));
380
- if (pid)
381
- tree_kill_1.default(pid, 'SIGKILL');
382
385
  });
383
386
  if (p) {
384
- const browser = p === null || p === void 0 ? void 0 : p.browser();
387
+ const browser = (p === null || p === void 0 ? void 0 : p.browser) && typeof (p === null || p === void 0 ? void 0 : p.browser) === 'function' && (p === null || p === void 0 ? void 0 : p.browser());
385
388
  yield killBrowser(browser);
386
389
  }
387
390
  else if (b) {
388
391
  yield killBrowser(b);
389
392
  }
393
+ if (pid)
394
+ tree_kill_1.default(pid, 'SIGKILL');
390
395
  if (exit)
391
396
  process.exit();
392
397
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.27.1",
3
+ "version": "4.27.5",
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",
@@ -67,6 +67,7 @@
67
67
  "@types/cross-spawn": "^6.0.2",
68
68
  "@types/death": "^1.1.1",
69
69
  "@types/express": "^4.17.11",
70
+ "@types/form-data": "^2.5.0",
70
71
  "@types/fs-extra": "^9.0.11",
71
72
  "@types/line-reader": "0.0.34",
72
73
  "@types/localtunnel": "^2.0.1",
@@ -119,6 +120,7 @@
119
120
  "express": "^4.17.1",
120
121
  "express-robots-txt": "^1.0.0",
121
122
  "find-up": "^5.0.0",
123
+ "form-data": "^4.0.0",
122
124
  "fs-extra": "^10.0.0",
123
125
  "get-port": "^5.1.1",
124
126
  "hasha": "^5.2.0",