-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
Copy pathsetup.ts
508 lines (470 loc) · 16.1 KB
/
setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import { camelize, isBase64 } from './../utils/tools';
import commandLineUsage from 'command-line-usage';
import meow, { AnyFlags } from 'meow';
import { Merge, JsonObject } from 'type-fest';
import { HELP_HEADER } from "./logo";
import { readJsonSync, writeFile } from 'fs-extra'
import * as changeCase from "change-case";
import { tryOpenFileAsObject } from './file-utils';
import { ConfigObject } from '../api/model/config';
import uuidAPIKey from 'uuid-apikey';
import { ev, Spin } from '../controllers/events';
import isUrl from 'is-url-superb';
import * as path from 'path';
import { setupLogging } from '../logging/logging';
let checkUrl = url => typeof url === 'string' ? isUrl(url) : false;
const configWithCases = readJsonSync(path.join(__dirname,'../../bin/config-schema.json'));
const optionList:
Merge<Merge<{
name?: string,
description?: string,
typeLabel?: string,
}, meow.AnyFlag>, {
type?: typeof Boolean | typeof Number | typeof String
}>[] = [{
name: 'no-api',
default: false,
alias: 'n',
type: Boolean,
description: "Don't expose the api. This may be useful if you just want to set the webhooks."
},{
name: 'bot-press-url',
alias: 'b',
type: String,
typeLabel: '{blue {underline http://localhost:3000/api/v1/bots/cool-bot}}',
description: "The Botpress URL that ends with your bot id."
},{
name: 'twilio-webhook',
alias: 't',
type: String,
typeLabel: '{blue {underline http://localhost:5555/incoming}}',
description: "Send twillio payloads to this URL. EASY API will also parse and processes twillio response message payloads."
},{
name: 'chatwoot-url',
type: String,
typeLabel: '{blue {underline http://localhost:3000/api/v1/accounts/3/inboxes/1}}',
description: "The URL of the specific Chatwoot inbox you set up for this session"
},{
name: 'chatwoot-api-access-token',
type: String,
typeLabel: '{blue {underline mEEwUGEEML2ZThMm252rLg1M}}',
description: "The access token of the specific Chatwoot inbox you set up for this session"
},
{
name: 'port',
alias: 'p',
default: 8002,
type: Number,
typeLabel: '{blue {underline 8080}}',
description: "Set the port for the api. Default to 8002."
},
{
name: 'api-host',
type: String,
typeLabel: '{yellow {underline localhost}}',
description: "The easy API may be sitting behind a reverse proxy. In this case set --api-host in order to make sure the api docs and api explorer are working properly. You will need to include the protocol as well."
},
{
name: 'host',
alias: 'h',
default: 'localhost',
type: String,
typeLabel: '{red {underline localhost}}',
description: "Set the hostname for the api documantation and statistics. Overrides --api-host. Default: localhost."
},
{
name: 'webhook',
alias: 'w',
type: String,
typeLabel: '{yellow {underline https://webhook.site/....}}',
description: "Webhook to use for the listeners."
},
{
name: 'ev',
alias: 'e',
type: String,
typeLabel: '{green {underline https://webhook.site/....}}',
description: "Send launch events to this URL."
},
{
name: 'ef',
type: String,
//@ts-ignore
default: ["qr", "STARTUP"],
isMultiple: true,
typeLabel: '{blueBright {underline qr,STARTUP}}',
description: "Filters which namespaces trigger the webhook set in -e/--ev."
},
{
name: 'allow-session-data-wh',
alias: 'x',
default: false,
type: Boolean,
description: "By default, if you set -e flag, the session data is not transferred to the webhook as it is extremely sensitive data. In order to bypass this security measure, use this flag."
},
{
name: 'key',
alias: 'k',
type: String,
typeLabel: '{redBright {underline apikey}}',
description: "Specify an api key to use as a check for all requests. If you add -k by itself, a key will be autogenerated for you."
},
{
name: 'config',
alias: 'c',
type: String,
typeLabel: '{yellowBright {underline ./config.json}}',
description: "The relative json file that contains the config. By default the system will look for config.json which will override any config variables set. Default: './config.json'."
},
{
name: 'session',
alias: 's',
type: String,
typeLabel: '{magentaBright {underline BASE64}}',
description: "A base64 string representing the session data."
},
{
name: 'keep-alive',
alias: 'a',
type: Boolean,
description: "If true, the system will force the session to refocus in this process. This will prevent you from opening a session elsewhere."
},
{
name: 'use-session-id-in-path',
alias: 'i',
type: Boolean,
description: "If true, all API paths will include the session id. default to false and the default session Id is 'session'."
},
{
name: 'generate-api-docs',
alias: 'd',
type: Boolean,
default: true,
description: "Generate postman collection and expose api docs to open in browser."
},
{
name: 'session-data-only',
alias: 'o',
type: Boolean,
description: "Kill the process when the session data is saved.",
default: false
},
{
name: 'skip-save-postman-collection',
type: Boolean,
description: "Don't save the postman collection.",
default: false
},
{
name: 'headful',
type: Boolean,
description: "Show the browser window on your machine.",
default: false
},
{
name: 'headful',
type: Boolean,
description: "Pre authenticate documentation site [High security risk]."
},
{
name: 'stats',
type: Boolean,
description: "Exposes API swagger-statistics.",
default: false
},
{
name: 'pre-auth-docs',
type: Boolean,
description: "Grab config options from the environment variables.",
default: false
},
{
name: 'no-kill-on-logout',
type: Boolean,
description: "Keeps the process alive when host account logs out of session. default is false",
default: false
},
{
name: 'debug',
type: Boolean,
description: "Print out the CLI flag values and the WA_* env vars. default is false",
default: false
},
{
name: 'cors',
type: Boolean,
description: "Enable all cors requests",
default: false
},
{
name: 'socket',
type: Boolean,
description: "Expose a socket.io middleware on the server.",
default: false
},
{
name: 'license-key',
alias: 'l',
type: String,
typeLabel: '{yellowBright {underline B2BJ4JFB-2UN2J3ND-2J5I.....}}',
description: "The license key you want to use for this server. License keys are used to unlock features. Learn more here https://github.com/open-wa/wa-automate-nodejs#license-key"
},
{
name: 'ready-webhook',
type: String,
typeLabel: '{yellow {underline https://webhook.site/....}}',
description: "Webhook that fires when the EASY API is completely ready"
},
{
name: 'on-call',
type: String,
typeLabel: '{yellow {underline "Please do not call this number"}}',
description: "A default message to send to any number that is trying to call the host account"
},
{
name: 'auto-reject',
type: Boolean,
description: "Automatically reject incoming phone and video calls to the host account."
},
{
name: 'emit-unread',
type: Boolean,
description: "Emit all unread messages via onMessage webhooks on launch.",
default: false
},
{
name: 'skip-url-check',
type: Boolean,
description: "Don't validate webhook URLs. Enables use of non-FQDNs."
},
{
name: 'tunnel',
type: Boolean,
description: "Expose a tunnel to your EASY API session - this is for testing and it is unsecured."
},
{
name: 'help',
description: 'Print this usage guide.'
}
]
export const optionKeys = optionList.map(({ name }) => camelize(name));
export const optionKeysWithDefalts = [...optionList.filter(o=>o.hasOwnProperty('default')).map(({ name }) => camelize(name)), 'popup'];
export const PrimitiveConverter = {
Number : 1,
Boolean : true,
String : "hello"
}
export const cliOptionNames = optionList.reduce((acc, c) => {
if (!c.type) return acc
acc[camelize(c.name)] = typeof PrimitiveConverter[c.type.name]
return acc
}, {})
export const meowFlags: () => AnyFlags = () => {
const extraFlags = {}
configWithCases.map(({ type, key }) => {
if (key === "popup") type = "number";
if (key === "viewport") type = "string";
if (key === "stickerServerEndpoint") type = "string";
extraFlags[key] = {
type
}
});
const res = {};
optionList.map(option => {
res[camelize(option.name)] = {
...option,
//@ts-ignore
type: option.type?.name?.toLowerCase()
}
});
return {
...res,
...extraFlags
};
}
export const helptext = commandLineUsage([{
content: HELP_HEADER,
raw: true,
},
{
header: '',
optionList
},
{
header: "Session config flags",
optionList: [
...configWithCases.map(c => {
let type;
if (c.type === 'boolean') type = Boolean;
if (c.type === 'string') type = String;
if (c.type === '"number"' || c.type === 'number') type = Number;
return {
name: c.p,
type,
description: c.description
}
})
]
},
{
content: `Please check here for more information on some of the above mentioned parameters: {underline https://docs.openwa.dev/interfaces/api_model_config.configobject}`
},
{
content: 'Project home: {underline https://github.com/open-wa/wa-automate-nodejs}'
}
])
export const envArgs: () => JsonObject = () => {
const env = {};
Object.entries(process.env).filter(([k, ]) => k.includes('WA')).map(([k, v]) => env[changeCase.camelCase(k.replace('WA_', ''))] = (v == 'false' || v == 'FALSE') ? false : (v == 'true' || v == 'TRUE') ? true : Number(v) || v);
return env
}
export const configFile: (config : string) => JsonObject = (config : string) => {
let confFile = {};
const conf = config || process.env.WA_CLI_CONFIG
if (conf) {
if (isBase64(conf as string)) {
confFile = JSON.parse(Buffer.from(conf as string, 'base64').toString('ascii'))
} else {
confFile = tryOpenFileAsObject(conf as string || `cli.config.json`);
if (!confFile) console.error(`Unable to read config file json: ${conf}`)
}
} else {
confFile = tryOpenFileAsObject(`cli.config.json`);
}
return confFile;
}
export const cli: () => {
createConfig: ConfigObject,
cliConfig: Merge<ConfigObject, {
[k: string]: any
}>,
PORT: number,
spinner: Spin
} = () => {
const _cli = meow(helptext, {
flags: {
...meowFlags(),
popup: {
type: 'boolean',
default: false
}
},
booleanDefault: undefined
});
/**
* Config order should follow airmanship rules. Least maneuverable to most maneuverable.
*
* 1. ENV VARS
* 2. Config file
* 3. CLI flags
*/
const nonCliConfigs = {
/**
* Environmental Variables
*/
...envArgs(),
/**
* The configuration file OR the --config base64 encoded config object
*/
...(configFile(_cli.flags.config as string) || {}),
}
optionList.filter(option=>option.default)
const cliConfig: any = {
sessionId: "session",
/**
* Prioirity goes from bottom up
*/
...nonCliConfigs,
/**
* CLI flags
*/
..._cli.flags,
/**
* Grab the configs for the cli defaults
*/
...optionKeysWithDefalts.reduce((p,c)=> nonCliConfigs.hasOwnProperty(c) ? {
...p,
[c]:nonCliConfigs[c]
} : p,{})
};
//firstly set up logger
if(cliConfig?.logging){
if(Array.isArray(cliConfig?.logging))
cliConfig.logging = setupLogging(cliConfig?.logging, `easy-api-${cliConfig?.sessionId || 'session'}`)
}
const PORT = Number(cliConfig.port || process.env.PORT || 8080);
const spinner = new Spin(cliConfig.sessionId, 'STARTUP', cliConfig?.disableSpins);
const createConfig: ConfigObject = {
...cliConfig
};
if (cliConfig?.session) {
createConfig.sessionData = cliConfig.session;
}
if (cliConfig?.allowSessionDataWh) {
cliConfig.allowSessionDataWebhook = cliConfig.allowSessionDataWh;
}
/**
* Build create() specific conig
*/
if ((cliConfig?.licenseKey || cliConfig?.l)) {
createConfig.licenseKey = cliConfig.licenseKey || cliConfig.l
}
if (cliConfig?.popup) {
createConfig.popup = PORT
}
if (!(cliConfig?.key == null) && cliConfig?.key == "") {
cliConfig.key = uuidAPIKey.create().apiKey;
}
if (cliConfig.viewport && cliConfig.viewport.split && cliConfig.viewport.split('x').length && cliConfig.viewport.split('x').length == 2 && cliConfig.viewport.split('x').map(Number).map(n => !!n ? n : null).filter(n => n).length == 2) {
const [width, height] = cliConfig.viewport.split('x').map(Number).map(n => !!n ? n : null).filter(n => n);
createConfig.viewport = { width, height }
}
if (cliConfig.resizable) {
createConfig.defaultViewport = null // <= set this to have viewport emulation off
}
if (cliConfig.sessionDataOnly) {
ev.on(`sessionData.**`, async (sessionData, sessionId) => {
writeFile(`${sessionId}.data.json`, JSON.stringify(sessionData), (err) => {
if (err) { spinner.fail(err.message); return; }
else
spinner.succeed(`Session data saved: ${sessionId}.data.json\nClosing.`);
process.exit();
});
})
}
if(cliConfig.skipUrlCheck) checkUrl = () => true;
if (cliConfig.webhook || cliConfig.webhook == '') {
if (checkUrl(cliConfig.webhook) || Array.isArray(cliConfig.webhook)) {
spinner.succeed('webhooks set already')
} else {
if (cliConfig.webhook == '') cliConfig.webhook = 'webhooks.json';
cliConfig.webhook = tryOpenFileAsObject(cliConfig.webhook, true);
if (!checkUrl(cliConfig.webhook)) {
cliConfig.webhook = undefined
}
}
}
if (cliConfig.twilioWebhook || cliConfig.twilioWebhook == '') {
if (cliConfig.twilioWebhook == '' && cliConfig.webhook) cliConfig.twilioWebhook = cliConfig.webhook;
if (!checkUrl(cliConfig.twilioWebhook)) {
cliConfig.twilioWebhook = undefined
}
if(cliConfig.twilioWebhook && (!createConfig.cloudUploadOptions || createConfig.messagePreprocessor!=='UPLOAD_CLOUD')) {
spinner.info('twilioWebhook set but messagePreprocessor not set to UPLOAD_CLOUD or cloudUploadOptions is missing')
}
}
if (cliConfig.apiHost) {
cliConfig.apiHost = cliConfig.apiHost.replace(/\/$/, '')
}
if (cliConfig.debug) {
spinner.succeed(`DEBUG - flags: ${JSON.stringify(cliConfig)}`)
const WA_ENV = {};
Object.keys(process.env).map(k => {
if (k.startsWith('WA_')) WA_ENV[k] = process.env[k];
})
spinner.succeed(`DEBUG - env vars: ${JSON.stringify(WA_ENV)}`)
}
return {
createConfig, cliConfig, PORT, spinner
}
}