@wsapichat/cli 0.1.0
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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +233 -0
- package/dist/index.js +1358 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1358 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { existsSync, readFileSync, mkdirSync, writeFileSync } from 'fs';
|
|
4
|
+
import { homedir } from 'os';
|
|
5
|
+
import { join, dirname } from 'path';
|
|
6
|
+
|
|
7
|
+
// src/errors.ts
|
|
8
|
+
var CliError = class extends Error {
|
|
9
|
+
exitCode;
|
|
10
|
+
constructor(message, exitCode = 1) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.exitCode = exitCode;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var ApiError = class extends CliError {
|
|
16
|
+
status;
|
|
17
|
+
detail;
|
|
18
|
+
body;
|
|
19
|
+
constructor(status, detail, body) {
|
|
20
|
+
super(`API error ${status}: ${detail}`, 1);
|
|
21
|
+
this.status = status;
|
|
22
|
+
this.detail = detail;
|
|
23
|
+
this.body = body;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/config.ts
|
|
28
|
+
var DEFAULT_BASE_URL = "https://api.wsapi.chat";
|
|
29
|
+
function configPath(override) {
|
|
30
|
+
if (override) return override;
|
|
31
|
+
const xdg = process.env.XDG_CONFIG_HOME;
|
|
32
|
+
if (xdg) return join(xdg, "wsapi", "config.json");
|
|
33
|
+
return join(homedir(), ".wsapi", "config.json");
|
|
34
|
+
}
|
|
35
|
+
function readConfig(path) {
|
|
36
|
+
const file = configPath(path);
|
|
37
|
+
if (!existsSync(file)) return {};
|
|
38
|
+
try {
|
|
39
|
+
const raw = readFileSync(file, "utf8");
|
|
40
|
+
return JSON.parse(raw);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw new CliError(`failed to read config at ${file}: ${e.message}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function writeConfig(cfg, path) {
|
|
46
|
+
const file = configPath(path);
|
|
47
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
48
|
+
writeFileSync(file, JSON.stringify(cfg, null, 2) + "\n", { mode: 384 });
|
|
49
|
+
return file;
|
|
50
|
+
}
|
|
51
|
+
function resolveConfig(opts) {
|
|
52
|
+
const file = readConfig(opts.config);
|
|
53
|
+
const profileName = opts.profile ?? process.env.WSAPI_PROFILE ?? file.defaultProfile;
|
|
54
|
+
const profile = profileName ? file.profiles?.[profileName] : void 0;
|
|
55
|
+
if (opts.profile && !profile) {
|
|
56
|
+
throw new CliError(`profile not found: ${opts.profile}`);
|
|
57
|
+
}
|
|
58
|
+
const baseUrl = opts.baseUrl ?? process.env.WSAPI_BASE_URL ?? profile?.baseUrl ?? DEFAULT_BASE_URL;
|
|
59
|
+
const apiKey = opts.apiKey ?? process.env.WSAPI_API_KEY ?? profile?.apiKey;
|
|
60
|
+
const instanceId = opts.instanceId ?? process.env.WSAPI_INSTANCE_ID ?? profile?.instanceId;
|
|
61
|
+
if (!apiKey) {
|
|
62
|
+
throw new CliError("missing API key. Set WSAPI_API_KEY, pass --api-key, or run `wsapi config set-profile`.");
|
|
63
|
+
}
|
|
64
|
+
if (!instanceId) {
|
|
65
|
+
throw new CliError(
|
|
66
|
+
"missing instance ID. Set WSAPI_INSTANCE_ID, pass --instance-id, or run `wsapi config set-profile`."
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
return { baseUrl, apiKey, instanceId };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/client.ts
|
|
73
|
+
var WsApiClient = class {
|
|
74
|
+
constructor(cfg) {
|
|
75
|
+
this.cfg = cfg;
|
|
76
|
+
}
|
|
77
|
+
cfg;
|
|
78
|
+
url(path, query) {
|
|
79
|
+
const base = this.cfg.baseUrl.replace(/\/$/, "");
|
|
80
|
+
const cleanPath = path.startsWith("/") ? path : `/${path}`;
|
|
81
|
+
const url = new URL(base + cleanPath);
|
|
82
|
+
if (query) {
|
|
83
|
+
for (const [k, v] of Object.entries(query)) {
|
|
84
|
+
if (v === void 0) continue;
|
|
85
|
+
url.searchParams.set(k, String(v));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return url.toString();
|
|
89
|
+
}
|
|
90
|
+
headers(extra) {
|
|
91
|
+
return {
|
|
92
|
+
"X-Api-Key": this.cfg.apiKey,
|
|
93
|
+
"X-Instance-Id": this.cfg.instanceId,
|
|
94
|
+
Accept: "application/json",
|
|
95
|
+
...extra
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
async request(path, opts = {}) {
|
|
99
|
+
const headers = this.headers(opts.accept ? { Accept: opts.accept } : void 0);
|
|
100
|
+
const init = {
|
|
101
|
+
method: opts.method ?? "GET",
|
|
102
|
+
headers
|
|
103
|
+
};
|
|
104
|
+
if (opts.body !== void 0) {
|
|
105
|
+
headers["Content-Type"] = "application/json";
|
|
106
|
+
init.body = JSON.stringify(opts.body);
|
|
107
|
+
}
|
|
108
|
+
const res = await fetch(this.url(path, opts.query), init);
|
|
109
|
+
return this.parse(res);
|
|
110
|
+
}
|
|
111
|
+
async requestBinary(path, opts = {}) {
|
|
112
|
+
const headers = this.headers({ Accept: opts.accept ?? "*/*" });
|
|
113
|
+
const init = {
|
|
114
|
+
method: opts.method ?? "GET",
|
|
115
|
+
headers
|
|
116
|
+
};
|
|
117
|
+
if (opts.body !== void 0) {
|
|
118
|
+
headers["Content-Type"] = "application/json";
|
|
119
|
+
init.body = JSON.stringify(opts.body);
|
|
120
|
+
}
|
|
121
|
+
const res = await fetch(this.url(path, opts.query), init);
|
|
122
|
+
if (!res.ok) {
|
|
123
|
+
await this.throwError(res);
|
|
124
|
+
}
|
|
125
|
+
const buf = new Uint8Array(await res.arrayBuffer());
|
|
126
|
+
return {
|
|
127
|
+
data: buf,
|
|
128
|
+
contentType: res.headers.get("content-type"),
|
|
129
|
+
filename: parseFilename(res.headers.get("content-disposition"))
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async parse(res) {
|
|
133
|
+
if (!res.ok) {
|
|
134
|
+
await this.throwError(res);
|
|
135
|
+
}
|
|
136
|
+
if (res.status === 204) return void 0;
|
|
137
|
+
const ct = res.headers.get("content-type") ?? "";
|
|
138
|
+
if (ct.includes("application/json")) {
|
|
139
|
+
return await res.json();
|
|
140
|
+
}
|
|
141
|
+
const text = await res.text();
|
|
142
|
+
return text;
|
|
143
|
+
}
|
|
144
|
+
async throwError(res) {
|
|
145
|
+
let body;
|
|
146
|
+
let detail = res.statusText || `HTTP ${res.status}`;
|
|
147
|
+
try {
|
|
148
|
+
const ct = res.headers.get("content-type") ?? "";
|
|
149
|
+
if (ct.includes("application/json")) {
|
|
150
|
+
body = await res.json();
|
|
151
|
+
const b = body;
|
|
152
|
+
if (b && typeof b.detail === "string") detail = b.detail;
|
|
153
|
+
} else {
|
|
154
|
+
const text = await res.text();
|
|
155
|
+
if (text) {
|
|
156
|
+
body = text;
|
|
157
|
+
detail = text.slice(0, 500);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
}
|
|
162
|
+
throw new ApiError(res.status, detail, body);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function parseFilename(disposition) {
|
|
166
|
+
if (!disposition) return null;
|
|
167
|
+
const m = /filename\*?=(?:UTF-8'')?"?([^";]+)"?/i.exec(disposition);
|
|
168
|
+
return m ? decodeURIComponent(m[1]) : null;
|
|
169
|
+
}
|
|
170
|
+
function printJson(value) {
|
|
171
|
+
if (value === void 0) return;
|
|
172
|
+
process.stdout.write(JSON.stringify(value, null, 2) + "\n");
|
|
173
|
+
}
|
|
174
|
+
function writeBinary(buf, output) {
|
|
175
|
+
if (output) {
|
|
176
|
+
writeFileSync(output, buf);
|
|
177
|
+
process.stdout.write(`wrote ${buf.byteLength} bytes to ${output}
|
|
178
|
+
`);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
process.stdout.write(buf);
|
|
182
|
+
}
|
|
183
|
+
async function readFileToBase64(path) {
|
|
184
|
+
const { readFile } = await import('fs/promises');
|
|
185
|
+
const buf = await readFile(path);
|
|
186
|
+
return buf.toString("base64");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/runner.ts
|
|
190
|
+
function getGlobalOptions(cmd) {
|
|
191
|
+
let root = cmd;
|
|
192
|
+
while (root.parent) root = root.parent;
|
|
193
|
+
return root.opts();
|
|
194
|
+
}
|
|
195
|
+
function makeClient(cmd) {
|
|
196
|
+
const opts = getGlobalOptions(cmd);
|
|
197
|
+
const cfg = resolveConfig(opts);
|
|
198
|
+
return new WsApiClient(cfg);
|
|
199
|
+
}
|
|
200
|
+
function action(handler) {
|
|
201
|
+
return async (...args) => {
|
|
202
|
+
const cmd = args[args.length - 1];
|
|
203
|
+
try {
|
|
204
|
+
const client = makeClient(cmd);
|
|
205
|
+
const result = await handler(client, cmd);
|
|
206
|
+
if (result !== void 0) {
|
|
207
|
+
printJson(result);
|
|
208
|
+
}
|
|
209
|
+
} catch (e) {
|
|
210
|
+
handleError(e);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function handleError(e) {
|
|
215
|
+
if (e instanceof ApiError) {
|
|
216
|
+
process.stderr.write(JSON.stringify({ error: e.detail, status: e.status, body: e.body }, null, 2) + "\n");
|
|
217
|
+
process.exit(1);
|
|
218
|
+
}
|
|
219
|
+
if (e instanceof CliError) {
|
|
220
|
+
process.stderr.write(`error: ${e.message}
|
|
221
|
+
`);
|
|
222
|
+
process.exit(e.exitCode);
|
|
223
|
+
}
|
|
224
|
+
if (e instanceof Error) {
|
|
225
|
+
process.stderr.write(`error: ${e.message}
|
|
226
|
+
`);
|
|
227
|
+
process.exit(1);
|
|
228
|
+
}
|
|
229
|
+
process.stderr.write(`error: ${String(e)}
|
|
230
|
+
`);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
}
|
|
233
|
+
function parseJsonArg(value) {
|
|
234
|
+
if (!value) return {};
|
|
235
|
+
let raw = value;
|
|
236
|
+
if (value.startsWith("@")) {
|
|
237
|
+
raw = readFileSync(value.slice(1), "utf8");
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
const parsed = JSON.parse(raw);
|
|
241
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
242
|
+
throw new CliError("--data must be a JSON object");
|
|
243
|
+
}
|
|
244
|
+
return parsed;
|
|
245
|
+
} catch (e) {
|
|
246
|
+
if (e instanceof CliError) throw e;
|
|
247
|
+
throw new CliError(`failed to parse --data as JSON: ${e.message}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function buildBody(fromFlags, data) {
|
|
251
|
+
const fromData = parseJsonArg(data);
|
|
252
|
+
const compact = {};
|
|
253
|
+
for (const [k, v] of Object.entries(fromFlags)) {
|
|
254
|
+
if (v === void 0) continue;
|
|
255
|
+
compact[k] = v;
|
|
256
|
+
}
|
|
257
|
+
return { ...compact, ...fromData };
|
|
258
|
+
}
|
|
259
|
+
function commaList(value) {
|
|
260
|
+
return value.split(",").map((s) => s.trim()).filter(Boolean);
|
|
261
|
+
}
|
|
262
|
+
function intArg(value) {
|
|
263
|
+
const n = Number.parseInt(value, 10);
|
|
264
|
+
if (Number.isNaN(n)) throw new CliError(`expected integer, got ${value}`);
|
|
265
|
+
return n;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/commands/config.ts
|
|
269
|
+
function rootOpts(cmd) {
|
|
270
|
+
let r = cmd;
|
|
271
|
+
while (r.parent) r = r.parent;
|
|
272
|
+
return r.opts();
|
|
273
|
+
}
|
|
274
|
+
function registerConfigCommand(program2) {
|
|
275
|
+
const config = program2.command("config").description("Manage CLI configuration profiles");
|
|
276
|
+
config.command("path").description("Print the path to the active config file").action(function() {
|
|
277
|
+
try {
|
|
278
|
+
const opts = rootOpts(this);
|
|
279
|
+
process.stdout.write(configPath(opts.config) + "\n");
|
|
280
|
+
} catch (e) {
|
|
281
|
+
handleError(e);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
config.command("show").description("Print the resolved config file contents").action(function() {
|
|
285
|
+
try {
|
|
286
|
+
const opts = rootOpts(this);
|
|
287
|
+
const cfg = readConfig(opts.config);
|
|
288
|
+
printJson(cfg);
|
|
289
|
+
} catch (e) {
|
|
290
|
+
handleError(e);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
config.command("list").description("List configured profile names").action(function() {
|
|
294
|
+
try {
|
|
295
|
+
const opts = rootOpts(this);
|
|
296
|
+
const cfg = readConfig(opts.config);
|
|
297
|
+
const names = Object.keys(cfg.profiles ?? {});
|
|
298
|
+
printJson({ defaultProfile: cfg.defaultProfile, profiles: names });
|
|
299
|
+
} catch (e) {
|
|
300
|
+
handleError(e);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
config.command("set-profile <name>").description("Create or update a named profile").option("--api-key <key>", "API key for this profile").option("--instance-id <id>", "instance ID for this profile").option("--base-url <url>", "base URL for this profile").option("--default", "mark this profile as the default").action(function(name, options) {
|
|
304
|
+
try {
|
|
305
|
+
const opts = rootOpts(this);
|
|
306
|
+
const cfg = readConfig(opts.config);
|
|
307
|
+
cfg.profiles ??= {};
|
|
308
|
+
const existing = cfg.profiles[name] ?? {};
|
|
309
|
+
cfg.profiles[name] = {
|
|
310
|
+
...existing,
|
|
311
|
+
...options.apiKey ? { apiKey: options.apiKey } : {},
|
|
312
|
+
...options.instanceId ? { instanceId: options.instanceId } : {},
|
|
313
|
+
...options.baseUrl ? { baseUrl: options.baseUrl } : {}
|
|
314
|
+
};
|
|
315
|
+
if (options.default) cfg.defaultProfile = name;
|
|
316
|
+
const path = writeConfig(cfg, opts.config);
|
|
317
|
+
process.stdout.write(`profile "${name}" saved to ${path}
|
|
318
|
+
`);
|
|
319
|
+
} catch (e) {
|
|
320
|
+
handleError(e);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
config.command("remove <name>").alias("rm").description("Delete a named profile").action(function(name) {
|
|
324
|
+
try {
|
|
325
|
+
const opts = rootOpts(this);
|
|
326
|
+
const cfg = readConfig(opts.config);
|
|
327
|
+
if (!cfg.profiles?.[name]) throw new CliError(`profile not found: ${name}`);
|
|
328
|
+
delete cfg.profiles[name];
|
|
329
|
+
if (cfg.defaultProfile === name) delete cfg.defaultProfile;
|
|
330
|
+
const path = writeConfig(cfg, opts.config);
|
|
331
|
+
process.stdout.write(`profile "${name}" removed from ${path}
|
|
332
|
+
`);
|
|
333
|
+
} catch (e) {
|
|
334
|
+
handleError(e);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
config.command("use <name>").description("Set the default profile").action(function(name) {
|
|
338
|
+
try {
|
|
339
|
+
const opts = rootOpts(this);
|
|
340
|
+
const cfg = readConfig(opts.config);
|
|
341
|
+
if (!cfg.profiles?.[name]) throw new CliError(`profile not found: ${name}`);
|
|
342
|
+
cfg.defaultProfile = name;
|
|
343
|
+
const path = writeConfig(cfg, opts.config);
|
|
344
|
+
process.stdout.write(`default profile set to "${name}" in ${path}
|
|
345
|
+
`);
|
|
346
|
+
} catch (e) {
|
|
347
|
+
handleError(e);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/commands/session.ts
|
|
353
|
+
function registerSessionCommands(program2) {
|
|
354
|
+
const session = program2.command("session").description("Manage WhatsApp session (login, logout, status)");
|
|
355
|
+
session.command("status").description("Get session status (isConnected, isLoggedIn, deviceId)").action(action((c) => c.request("/session/status")));
|
|
356
|
+
session.command("qr").description("Fetch the pairing QR code").option("-o, --output <file>", "write the PNG to a file (default: stdout)").option("--text", "fetch the QR as a raw text string instead of a PNG").action(async function(options) {
|
|
357
|
+
try {
|
|
358
|
+
const c = makeClient(this);
|
|
359
|
+
if (options.text) {
|
|
360
|
+
const result = await c.request("/session/qr/text");
|
|
361
|
+
printJson(result);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const { data } = await c.requestBinary("/session/qr", { accept: "image/png" });
|
|
365
|
+
writeBinary(data, options.output);
|
|
366
|
+
} catch (e) {
|
|
367
|
+
handleError(e);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
session.command("pair-code <phone>").description("Get a pair code for a phone number").action(
|
|
371
|
+
action((c, cmd) => {
|
|
372
|
+
const [phone] = cmd.args;
|
|
373
|
+
return c.request(`/session/pair-code/${encodeURIComponent(phone)}`);
|
|
374
|
+
})
|
|
375
|
+
);
|
|
376
|
+
session.command("logout").description("Logout from WhatsApp").action(
|
|
377
|
+
action(async (c) => {
|
|
378
|
+
await c.request("/session/logout", { method: "POST" });
|
|
379
|
+
return { status: "ok" };
|
|
380
|
+
})
|
|
381
|
+
);
|
|
382
|
+
session.command("flush-history").description("Asynchronously flush cached history sync messages").action(action((c) => c.request("/session/flush-history", { method: "POST" })));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// src/commands/messages.ts
|
|
386
|
+
function registerMessagesCommands(program2) {
|
|
387
|
+
const m = program2.command("messages").description("Send and manage WhatsApp messages");
|
|
388
|
+
m.command("text").description("Send a text message").requiredOption("--to <jid>", "recipient JID (user or group)").requiredOption("--text <text>", "message text").option("--mentions <jids>", "comma-separated mentioned JIDs", commaList).option("--reply-to <id>", "reply to message ID").option("--reply-to-sender-id <jid>", "sender JID of the message being replied to").option("--forwarded", "mark as forwarded").option("--ephemeral <off|24h|7d|90d>", "disappearing message timer").option("--data <json>", "extra body fields as JSON (overrides flags); prefix with @ to load from file").action(
|
|
389
|
+
action((c, cmd) => {
|
|
390
|
+
const o = cmd.opts();
|
|
391
|
+
return c.request("/messages/text", {
|
|
392
|
+
method: "POST",
|
|
393
|
+
body: buildBody(
|
|
394
|
+
{
|
|
395
|
+
to: o.to,
|
|
396
|
+
text: o.text,
|
|
397
|
+
mentions: o.mentions,
|
|
398
|
+
replyTo: o.replyTo,
|
|
399
|
+
replyToSenderId: o.replyToSenderId,
|
|
400
|
+
isForwarded: o.forwarded || void 0,
|
|
401
|
+
ephemeralExpiration: o.ephemeral
|
|
402
|
+
},
|
|
403
|
+
o.data
|
|
404
|
+
)
|
|
405
|
+
});
|
|
406
|
+
})
|
|
407
|
+
);
|
|
408
|
+
for (const kind of ["image", "video", "audio", "voice"]) {
|
|
409
|
+
m.command(kind).description(`Send a ${kind} message`).requiredOption("--to <jid>", "recipient JID").option("--url <url>", "URL of the media (mutually exclusive with --data-file)").option("--data-file <path>", "local file to upload as base64 (mutually exclusive with --url)").option("--mime-type <type>", "MIME type (auto-detected if omitted)").option("--caption <text>", "media caption").option("--mentions <jids>", "comma-separated mentioned JIDs", commaList).option("--reply-to <id>", "reply to message ID").option("--reply-to-sender-id <jid>", "sender JID of the message being replied to").option("--forwarded", "mark as forwarded").option("--view-once", "send as view-once").option("--ephemeral <off|24h|7d|90d>", "disappearing timer").option("--data <json>", "extra body fields as JSON").action(
|
|
410
|
+
action(async (c, cmd) => {
|
|
411
|
+
const o = cmd.opts();
|
|
412
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : void 0;
|
|
413
|
+
return c.request(`/messages/${kind}`, {
|
|
414
|
+
method: "POST",
|
|
415
|
+
body: buildBody(
|
|
416
|
+
{
|
|
417
|
+
to: o.to,
|
|
418
|
+
url: o.url,
|
|
419
|
+
data,
|
|
420
|
+
mimeType: o.mimeType,
|
|
421
|
+
caption: o.caption,
|
|
422
|
+
mentions: o.mentions,
|
|
423
|
+
replyTo: o.replyTo,
|
|
424
|
+
replyToSenderId: o.replyToSenderId,
|
|
425
|
+
isForwarded: o.forwarded || void 0,
|
|
426
|
+
viewOnce: o.viewOnce || void 0,
|
|
427
|
+
ephemeralExpiration: o.ephemeral
|
|
428
|
+
},
|
|
429
|
+
o.data
|
|
430
|
+
)
|
|
431
|
+
});
|
|
432
|
+
})
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
m.command("document").description("Send a document message").requiredOption("--to <jid>", "recipient JID").requiredOption("--filename <name>", "filename shown to recipient").option("--url <url>", "URL of the document").option("--data-file <path>", "local file to upload as base64").option("--mime-type <type>", "MIME type").option("--caption <text>", "caption").option("--mentions <jids>", "comma-separated JIDs", commaList).option("--reply-to <id>", "reply to message ID").option("--reply-to-sender-id <jid>", "sender JID of replied message").option("--forwarded", "mark as forwarded").option("--ephemeral <off|24h|7d|90d>", "disappearing timer").option("--data <json>", "extra body fields as JSON").action(
|
|
436
|
+
action(async (c, cmd) => {
|
|
437
|
+
const o = cmd.opts();
|
|
438
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : void 0;
|
|
439
|
+
return c.request("/messages/document", {
|
|
440
|
+
method: "POST",
|
|
441
|
+
body: buildBody(
|
|
442
|
+
{
|
|
443
|
+
to: o.to,
|
|
444
|
+
filename: o.filename,
|
|
445
|
+
url: o.url,
|
|
446
|
+
data,
|
|
447
|
+
mimeType: o.mimeType,
|
|
448
|
+
caption: o.caption,
|
|
449
|
+
mentions: o.mentions,
|
|
450
|
+
replyTo: o.replyTo,
|
|
451
|
+
replyToSenderId: o.replyToSenderId,
|
|
452
|
+
isForwarded: o.forwarded || void 0,
|
|
453
|
+
ephemeralExpiration: o.ephemeral
|
|
454
|
+
},
|
|
455
|
+
o.data
|
|
456
|
+
)
|
|
457
|
+
});
|
|
458
|
+
})
|
|
459
|
+
);
|
|
460
|
+
m.command("sticker").description("Send a sticker message (WebP)").requiredOption("--to <jid>", "recipient JID").option("--url <url>", "URL of the sticker").option("--data-file <path>", "local sticker file (WebP)").option("--animated", "mark as animated").option("--mentions <jids>", "comma-separated JIDs", commaList).option("--reply-to <id>", "reply to message ID").option("--reply-to-sender-id <jid>", "sender JID of replied message").option("--forwarded", "mark as forwarded").option("--ephemeral <off|24h|7d|90d>", "disappearing timer").option("--data <json>", "extra body fields as JSON").action(
|
|
461
|
+
action(async (c, cmd) => {
|
|
462
|
+
const o = cmd.opts();
|
|
463
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : void 0;
|
|
464
|
+
return c.request("/messages/sticker", {
|
|
465
|
+
method: "POST",
|
|
466
|
+
body: buildBody(
|
|
467
|
+
{
|
|
468
|
+
to: o.to,
|
|
469
|
+
url: o.url,
|
|
470
|
+
data,
|
|
471
|
+
isAnimated: o.animated || void 0,
|
|
472
|
+
mentions: o.mentions,
|
|
473
|
+
replyTo: o.replyTo,
|
|
474
|
+
replyToSenderId: o.replyToSenderId,
|
|
475
|
+
isForwarded: o.forwarded || void 0,
|
|
476
|
+
ephemeralExpiration: o.ephemeral
|
|
477
|
+
},
|
|
478
|
+
o.data
|
|
479
|
+
)
|
|
480
|
+
});
|
|
481
|
+
})
|
|
482
|
+
);
|
|
483
|
+
m.command("contact").description("Send a contact card").requiredOption("--to <jid>", "recipient JID").option("--display-name <name>", "contact display name (auto-generates a vCard)").option("--vcard <vcard>", "raw vCard string").option("--reply-to <id>", "reply to message ID").option("--reply-to-sender-id <jid>", "sender JID of replied message").option("--data <json>", "extra body fields as JSON").action(
|
|
484
|
+
action((c, cmd) => {
|
|
485
|
+
const o = cmd.opts();
|
|
486
|
+
return c.request("/messages/contact", {
|
|
487
|
+
method: "POST",
|
|
488
|
+
body: buildBody(
|
|
489
|
+
{
|
|
490
|
+
to: o.to,
|
|
491
|
+
displayName: o.displayName,
|
|
492
|
+
vcard: o.vcard,
|
|
493
|
+
replyTo: o.replyTo,
|
|
494
|
+
replyToSenderId: o.replyToSenderId
|
|
495
|
+
},
|
|
496
|
+
o.data
|
|
497
|
+
)
|
|
498
|
+
});
|
|
499
|
+
})
|
|
500
|
+
);
|
|
501
|
+
m.command("location").description("Send a location message").requiredOption("--to <jid>", "recipient JID").option("--latitude <n>", "latitude", parseFloat).option("--longitude <n>", "longitude", parseFloat).option("--name <name>", "location name").option("--address <address>", "location address").option("--url <url>", "location URL").option("--data <json>", "extra body fields as JSON").action(
|
|
502
|
+
action((c, cmd) => {
|
|
503
|
+
const o = cmd.opts();
|
|
504
|
+
return c.request("/messages/location", {
|
|
505
|
+
method: "POST",
|
|
506
|
+
body: buildBody(
|
|
507
|
+
{
|
|
508
|
+
to: o.to,
|
|
509
|
+
latitude: o.latitude,
|
|
510
|
+
longitude: o.longitude,
|
|
511
|
+
name: o.name,
|
|
512
|
+
address: o.address,
|
|
513
|
+
url: o.url
|
|
514
|
+
},
|
|
515
|
+
o.data
|
|
516
|
+
)
|
|
517
|
+
});
|
|
518
|
+
})
|
|
519
|
+
);
|
|
520
|
+
m.command("link").description("Send a link preview message").requiredOption("--to <jid>", "recipient JID").requiredOption("--text <text>", "message text").requiredOption("--url <url>", "link URL").option("--title <title>", "preview title").option("--description <desc>", "preview description").option("--jpeg-thumbnail <base64>", "base64 JPEG thumbnail").option("--data <json>", "extra body fields as JSON").action(
|
|
521
|
+
action((c, cmd) => {
|
|
522
|
+
const o = cmd.opts();
|
|
523
|
+
return c.request("/messages/link", {
|
|
524
|
+
method: "POST",
|
|
525
|
+
body: buildBody(
|
|
526
|
+
{
|
|
527
|
+
to: o.to,
|
|
528
|
+
text: o.text,
|
|
529
|
+
url: o.url,
|
|
530
|
+
title: o.title,
|
|
531
|
+
description: o.description,
|
|
532
|
+
jpegThumbnail: o.jpegThumbnail
|
|
533
|
+
},
|
|
534
|
+
o.data
|
|
535
|
+
)
|
|
536
|
+
});
|
|
537
|
+
})
|
|
538
|
+
);
|
|
539
|
+
m.command("react <messageId>").description("React to a message (empty reaction removes it)").requiredOption("--to <jid>", "chat JID").requiredOption("--reaction <emoji>", "emoji reaction (empty string to remove)").option("--sender-id <jid>", "sender JID of the original message (group chats)").option("--data <json>", "extra body fields as JSON").action(
|
|
540
|
+
action((c, cmd) => {
|
|
541
|
+
const [messageId] = cmd.args;
|
|
542
|
+
const o = cmd.opts();
|
|
543
|
+
return c.request(`/messages/${encodeURIComponent(messageId)}/reaction`, {
|
|
544
|
+
method: "POST",
|
|
545
|
+
body: buildBody(
|
|
546
|
+
{
|
|
547
|
+
to: o.to,
|
|
548
|
+
senderId: o.senderId,
|
|
549
|
+
reaction: o.reaction
|
|
550
|
+
},
|
|
551
|
+
o.data
|
|
552
|
+
)
|
|
553
|
+
});
|
|
554
|
+
})
|
|
555
|
+
);
|
|
556
|
+
m.command("edit <messageId>").description("Edit a sent message").requiredOption("--to <jid>", "chat JID").requiredOption("--text <text>", "new message text").option("--mentions <jids>", "comma-separated JIDs", commaList).option("--ephemeral <off|24h|7d|90d>", "disappearing timer").option("--data <json>", "extra body fields as JSON").action(
|
|
557
|
+
action((c, cmd) => {
|
|
558
|
+
const [messageId] = cmd.args;
|
|
559
|
+
const o = cmd.opts();
|
|
560
|
+
return c.request(`/messages/${encodeURIComponent(messageId)}/edit`, {
|
|
561
|
+
method: "POST",
|
|
562
|
+
body: buildBody(
|
|
563
|
+
{
|
|
564
|
+
to: o.to,
|
|
565
|
+
text: o.text,
|
|
566
|
+
mentions: o.mentions,
|
|
567
|
+
ephemeralExpiration: o.ephemeral
|
|
568
|
+
},
|
|
569
|
+
o.data
|
|
570
|
+
)
|
|
571
|
+
});
|
|
572
|
+
})
|
|
573
|
+
);
|
|
574
|
+
m.command("read <messageId>").description("Mark a message as read/delivered/played").requiredOption("--chat-id <jid>", "chat JID").requiredOption("--sender-id <jid>", "sender JID").requiredOption("--receipt-type <type>", "delivered | sender | read | played").option("--data <json>", "extra body fields as JSON").action(
|
|
575
|
+
action(async (c, cmd) => {
|
|
576
|
+
const [messageId] = cmd.args;
|
|
577
|
+
const o = cmd.opts();
|
|
578
|
+
await c.request(`/messages/${encodeURIComponent(messageId)}/read`, {
|
|
579
|
+
method: "POST",
|
|
580
|
+
body: buildBody(
|
|
581
|
+
{
|
|
582
|
+
chatId: o.chatId,
|
|
583
|
+
senderId: o.senderId,
|
|
584
|
+
receiptType: o.receiptType
|
|
585
|
+
},
|
|
586
|
+
o.data
|
|
587
|
+
)
|
|
588
|
+
});
|
|
589
|
+
return { status: "ok" };
|
|
590
|
+
})
|
|
591
|
+
);
|
|
592
|
+
m.command("star <messageId>").description("Star or unstar a message").requiredOption("--chat-id <jid>", "chat JID").requiredOption("--sender-id <jid>", "sender JID").option("--unstar", "unstar instead of starring").option("--data <json>", "extra body fields as JSON").action(
|
|
593
|
+
action(async (c, cmd) => {
|
|
594
|
+
const [messageId] = cmd.args;
|
|
595
|
+
const o = cmd.opts();
|
|
596
|
+
await c.request(`/messages/${encodeURIComponent(messageId)}/star`, {
|
|
597
|
+
method: "POST",
|
|
598
|
+
body: buildBody(
|
|
599
|
+
{
|
|
600
|
+
chatId: o.chatId,
|
|
601
|
+
senderId: o.senderId,
|
|
602
|
+
starred: !o.unstar
|
|
603
|
+
},
|
|
604
|
+
o.data
|
|
605
|
+
)
|
|
606
|
+
});
|
|
607
|
+
return { status: "ok" };
|
|
608
|
+
})
|
|
609
|
+
);
|
|
610
|
+
m.command("pin <messageId>").description("Pin or unpin a message").requiredOption("--chat-id <jid>", "chat JID").requiredOption("--sender-id <jid>", "sender JID").option("--unpin", "unpin instead of pinning").option("--expiration <duration>", "pin expiration duration (e.g. 24h, 7d, 30d)").option("--data <json>", "extra body fields as JSON").action(
|
|
611
|
+
action(async (c, cmd) => {
|
|
612
|
+
const [messageId] = cmd.args;
|
|
613
|
+
const o = cmd.opts();
|
|
614
|
+
await c.request(`/messages/${encodeURIComponent(messageId)}/pin`, {
|
|
615
|
+
method: "POST",
|
|
616
|
+
body: buildBody(
|
|
617
|
+
{
|
|
618
|
+
chatId: o.chatId,
|
|
619
|
+
senderId: o.senderId,
|
|
620
|
+
pinned: !o.unpin,
|
|
621
|
+
pinExpiration: o.expiration
|
|
622
|
+
},
|
|
623
|
+
o.data
|
|
624
|
+
)
|
|
625
|
+
});
|
|
626
|
+
return { status: "ok" };
|
|
627
|
+
})
|
|
628
|
+
);
|
|
629
|
+
m.command("delete <messageId>").description("Delete a message for everyone").requiredOption("--chat-id <jid>", "chat JID").requiredOption("--sender-id <jid>", "sender JID").option("--data <json>", "extra body fields as JSON").action(
|
|
630
|
+
action(async (c, cmd) => {
|
|
631
|
+
const [messageId] = cmd.args;
|
|
632
|
+
const o = cmd.opts();
|
|
633
|
+
await c.request(`/messages/${encodeURIComponent(messageId)}/delete`, {
|
|
634
|
+
method: "POST",
|
|
635
|
+
body: buildBody({ chatId: o.chatId, senderId: o.senderId }, o.data)
|
|
636
|
+
});
|
|
637
|
+
return { status: "ok" };
|
|
638
|
+
})
|
|
639
|
+
);
|
|
640
|
+
m.command("delete-for-me <messageId>").description("Delete a message only from your chat").requiredOption("--chat-id <jid>", "chat JID").option("--sender-id <jid>", "sender JID").option("--from-me", "the message was sent by me").option("--timestamp <iso8601>", "message timestamp (ISO-8601)").option("--data <json>", "extra body fields as JSON").action(
|
|
641
|
+
action(async (c, cmd) => {
|
|
642
|
+
const [messageId] = cmd.args;
|
|
643
|
+
const o = cmd.opts();
|
|
644
|
+
await c.request(`/messages/${encodeURIComponent(messageId)}/delete-for-me`, {
|
|
645
|
+
method: "POST",
|
|
646
|
+
body: buildBody(
|
|
647
|
+
{
|
|
648
|
+
chatId: o.chatId,
|
|
649
|
+
senderId: o.senderId,
|
|
650
|
+
isFromMe: o.fromMe || void 0,
|
|
651
|
+
timestamp: o.timestamp
|
|
652
|
+
},
|
|
653
|
+
o.data
|
|
654
|
+
)
|
|
655
|
+
});
|
|
656
|
+
return { status: "ok" };
|
|
657
|
+
})
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// src/commands/groups.ts
|
|
662
|
+
function registerGroupsCommands(program2) {
|
|
663
|
+
const g = program2.command("groups").description("Group management");
|
|
664
|
+
g.command("list").description("List joined groups").action(action((c) => c.request("/groups")));
|
|
665
|
+
g.command("get <id>").description("Get group info by JID").action(
|
|
666
|
+
action((c, cmd) => {
|
|
667
|
+
const [id] = cmd.args;
|
|
668
|
+
return c.request(`/groups/${encodeURIComponent(id)}`);
|
|
669
|
+
})
|
|
670
|
+
);
|
|
671
|
+
g.command("create").description("Create a new group").requiredOption("--name <name>", "group name").requiredOption("--participants <jids>", "comma-separated participant JIDs", commaList).option("--data <json>", "extra body fields as JSON").action(
|
|
672
|
+
action((c, cmd) => {
|
|
673
|
+
const o = cmd.opts();
|
|
674
|
+
return c.request("/groups", {
|
|
675
|
+
method: "POST",
|
|
676
|
+
body: buildBody({ name: o.name, participants: o.participants }, o.data)
|
|
677
|
+
});
|
|
678
|
+
})
|
|
679
|
+
);
|
|
680
|
+
g.command("set-name <id>").description("Set group name").requiredOption("--name <name>", "new group name").action(
|
|
681
|
+
action(async (c, cmd) => {
|
|
682
|
+
const [id] = cmd.args;
|
|
683
|
+
await c.request(`/groups/${encodeURIComponent(id)}/name`, {
|
|
684
|
+
method: "PUT",
|
|
685
|
+
body: { name: cmd.opts().name }
|
|
686
|
+
});
|
|
687
|
+
return { status: "ok" };
|
|
688
|
+
})
|
|
689
|
+
);
|
|
690
|
+
g.command("set-description <id>").description("Set group description").option("--description <text>", "description (omit for empty)").action(
|
|
691
|
+
action(async (c, cmd) => {
|
|
692
|
+
const [id] = cmd.args;
|
|
693
|
+
await c.request(`/groups/${encodeURIComponent(id)}/description`, {
|
|
694
|
+
method: "PUT",
|
|
695
|
+
body: { description: cmd.opts().description ?? "" }
|
|
696
|
+
});
|
|
697
|
+
return { status: "ok" };
|
|
698
|
+
})
|
|
699
|
+
);
|
|
700
|
+
g.command("set-picture <id>").description("Set group picture (JPEG)").option("--data-file <path>", "JPEG file to upload (base64 encoded)").option("--data-base64 <base64>", "base64-encoded JPEG image").action(
|
|
701
|
+
action(async (c, cmd) => {
|
|
702
|
+
const [id] = cmd.args;
|
|
703
|
+
const o = cmd.opts();
|
|
704
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : o.dataBase64;
|
|
705
|
+
return c.request(`/groups/${encodeURIComponent(id)}/picture`, {
|
|
706
|
+
method: "POST",
|
|
707
|
+
body: { data }
|
|
708
|
+
});
|
|
709
|
+
})
|
|
710
|
+
);
|
|
711
|
+
g.command("leave <id>").description("Leave a group").action(
|
|
712
|
+
action(async (c, cmd) => {
|
|
713
|
+
const [id] = cmd.args;
|
|
714
|
+
await c.request(`/groups/${encodeURIComponent(id)}/leave`, { method: "POST" });
|
|
715
|
+
return { status: "ok" };
|
|
716
|
+
})
|
|
717
|
+
);
|
|
718
|
+
g.command("participants <id>").description("Get group participants").action(
|
|
719
|
+
action((c, cmd) => {
|
|
720
|
+
const [id] = cmd.args;
|
|
721
|
+
return c.request(`/groups/${encodeURIComponent(id)}/participants`);
|
|
722
|
+
})
|
|
723
|
+
);
|
|
724
|
+
g.command("update-participants <id>").description("Add, remove, promote, or demote participants").requiredOption("--action <action>", "add | remove | promote | demote").requiredOption("--participants <jids>", "comma-separated JIDs", commaList).action(
|
|
725
|
+
action(async (c, cmd) => {
|
|
726
|
+
const [id] = cmd.args;
|
|
727
|
+
const o = cmd.opts();
|
|
728
|
+
await c.request(`/groups/${encodeURIComponent(id)}/participants`, {
|
|
729
|
+
method: "PUT",
|
|
730
|
+
body: { participants: o.participants, action: o.action }
|
|
731
|
+
});
|
|
732
|
+
return { status: "ok" };
|
|
733
|
+
})
|
|
734
|
+
);
|
|
735
|
+
g.command("invite-link <id>").description("Get the current group invite link").action(
|
|
736
|
+
action((c, cmd) => {
|
|
737
|
+
const [id] = cmd.args;
|
|
738
|
+
return c.request(`/groups/${encodeURIComponent(id)}/invite-link`);
|
|
739
|
+
})
|
|
740
|
+
);
|
|
741
|
+
g.command("reset-invite <id>").description("Reset and rotate the group invite link").action(
|
|
742
|
+
action((c, cmd) => {
|
|
743
|
+
const [id] = cmd.args;
|
|
744
|
+
return c.request(`/groups/${encodeURIComponent(id)}/invite-link/reset`, { method: "POST" });
|
|
745
|
+
})
|
|
746
|
+
);
|
|
747
|
+
for (const setting of [
|
|
748
|
+
{ cmd: "set-announce", path: "announce", help: "Toggle announce mode (only admins can send)" },
|
|
749
|
+
{ cmd: "set-locked", path: "locked", help: "Toggle locked mode (only admins can edit info)" },
|
|
750
|
+
{ cmd: "set-join-approval", path: "join-approval", help: "Toggle join approval requirement" }
|
|
751
|
+
]) {
|
|
752
|
+
g.command(`${setting.cmd} <id>`).description(setting.help).requiredOption("--enabled <bool>", "true | false").action(
|
|
753
|
+
action(async (c, cmd) => {
|
|
754
|
+
const [id] = cmd.args;
|
|
755
|
+
const enabled = cmd.opts().enabled === "true" || cmd.opts().enabled === true;
|
|
756
|
+
await c.request(`/groups/${encodeURIComponent(id)}/settings/${setting.path}`, {
|
|
757
|
+
method: "PUT",
|
|
758
|
+
body: { enabled }
|
|
759
|
+
});
|
|
760
|
+
return { status: "ok" };
|
|
761
|
+
})
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
g.command("set-member-add-mode <id>").description("Control whether only admins can add members").requiredOption("--only-admin-add <bool>", "true | false").action(
|
|
765
|
+
action(async (c, cmd) => {
|
|
766
|
+
const [id] = cmd.args;
|
|
767
|
+
const v = cmd.opts().onlyAdminAdd;
|
|
768
|
+
const onlyAdminAdd = v === "true" || v === true;
|
|
769
|
+
await c.request(`/groups/${encodeURIComponent(id)}/settings/member-add-mode`, {
|
|
770
|
+
method: "PUT",
|
|
771
|
+
body: { onlyAdminAdd }
|
|
772
|
+
});
|
|
773
|
+
return { status: "ok" };
|
|
774
|
+
})
|
|
775
|
+
);
|
|
776
|
+
g.command("join-link").description("Join a group via an invite link code").requiredOption("--code <code>", "invite link code (the part after https://chat.whatsapp.com/)").action(
|
|
777
|
+
action((c, cmd) => {
|
|
778
|
+
return c.request("/groups/join/link", {
|
|
779
|
+
method: "POST",
|
|
780
|
+
body: { code: cmd.opts().code }
|
|
781
|
+
});
|
|
782
|
+
})
|
|
783
|
+
);
|
|
784
|
+
g.command("join-invite").description("Accept a group invite received in a direct message").requiredOption("--group-id <jid>", "group JID").requiredOption("--inviter-id <jid>", "inviter JID").requiredOption("--code <code>", "invite code").option("--expiration <unix>", "expiration timestamp (int64)").action(
|
|
785
|
+
action(async (c, cmd) => {
|
|
786
|
+
const o = cmd.opts();
|
|
787
|
+
await c.request("/groups/join/invite", {
|
|
788
|
+
method: "POST",
|
|
789
|
+
body: {
|
|
790
|
+
groupId: o.groupId,
|
|
791
|
+
inviterId: o.inviterId,
|
|
792
|
+
code: o.code,
|
|
793
|
+
...o.expiration ? { expiration: Number.parseInt(o.expiration, 10) } : {}
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
return { status: "ok" };
|
|
797
|
+
})
|
|
798
|
+
);
|
|
799
|
+
g.command("invite-info <code>").description("Preview group info from an invite code").action(
|
|
800
|
+
action((c, cmd) => {
|
|
801
|
+
const [code] = cmd.args;
|
|
802
|
+
return c.request(`/groups/invite/${encodeURIComponent(code)}`);
|
|
803
|
+
})
|
|
804
|
+
);
|
|
805
|
+
g.command("requests <id>").description("List pending join requests").action(
|
|
806
|
+
action((c, cmd) => {
|
|
807
|
+
const [id] = cmd.args;
|
|
808
|
+
return c.request(`/groups/${encodeURIComponent(id)}/requests`);
|
|
809
|
+
})
|
|
810
|
+
);
|
|
811
|
+
g.command("update-requests <id>").description("Approve or reject pending join requests").requiredOption("--action <action>", "approve | reject").requiredOption("--participants <jids>", "comma-separated JIDs", commaList).action(
|
|
812
|
+
action(async (c, cmd) => {
|
|
813
|
+
const [id] = cmd.args;
|
|
814
|
+
const o = cmd.opts();
|
|
815
|
+
await c.request(`/groups/${encodeURIComponent(id)}/requests`, {
|
|
816
|
+
method: "PUT",
|
|
817
|
+
body: { participants: o.participants, action: o.action }
|
|
818
|
+
});
|
|
819
|
+
return { status: "ok" };
|
|
820
|
+
})
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// src/commands/communities.ts
|
|
825
|
+
function registerCommunitiesCommands(program2) {
|
|
826
|
+
const cm = program2.command("communities").description("Community management");
|
|
827
|
+
cm.command("list").description("List joined communities").action(action((c) => c.request("/communities")));
|
|
828
|
+
cm.command("get <id>").description("Get community info").action(
|
|
829
|
+
action((c, cmd) => {
|
|
830
|
+
const [id] = cmd.args;
|
|
831
|
+
return c.request(`/communities/${encodeURIComponent(id)}`);
|
|
832
|
+
})
|
|
833
|
+
);
|
|
834
|
+
cm.command("create").description("Create a new community").requiredOption("--name <name>", "community name").option("--participants <jids>", "comma-separated JIDs", commaList).option("--approval-mode <mode>", "join approval mode").option("--data <json>", "extra body fields as JSON").action(
|
|
835
|
+
action((c, cmd) => {
|
|
836
|
+
const o = cmd.opts();
|
|
837
|
+
return c.request("/communities", {
|
|
838
|
+
method: "POST",
|
|
839
|
+
body: buildBody(
|
|
840
|
+
{
|
|
841
|
+
name: o.name,
|
|
842
|
+
participants: o.participants,
|
|
843
|
+
approvalMode: o.approvalMode
|
|
844
|
+
},
|
|
845
|
+
o.data
|
|
846
|
+
)
|
|
847
|
+
});
|
|
848
|
+
})
|
|
849
|
+
);
|
|
850
|
+
cm.command("leave <id>").description("Leave a community").action(
|
|
851
|
+
action(async (c, cmd) => {
|
|
852
|
+
const [id] = cmd.args;
|
|
853
|
+
await c.request(`/communities/${encodeURIComponent(id)}/leave`, { method: "POST" });
|
|
854
|
+
return { status: "ok" };
|
|
855
|
+
})
|
|
856
|
+
);
|
|
857
|
+
cm.command("set-name <id>").description("Set community name").requiredOption("--name <name>", "new name").action(
|
|
858
|
+
action(async (c, cmd) => {
|
|
859
|
+
const [id] = cmd.args;
|
|
860
|
+
await c.request(`/communities/${encodeURIComponent(id)}/name`, {
|
|
861
|
+
method: "PUT",
|
|
862
|
+
body: { name: cmd.opts().name }
|
|
863
|
+
});
|
|
864
|
+
return { status: "ok" };
|
|
865
|
+
})
|
|
866
|
+
);
|
|
867
|
+
cm.command("set-description <id>").description("Set community description").option("--description <text>", "description").action(
|
|
868
|
+
action(async (c, cmd) => {
|
|
869
|
+
const [id] = cmd.args;
|
|
870
|
+
await c.request(`/communities/${encodeURIComponent(id)}/description`, {
|
|
871
|
+
method: "PUT",
|
|
872
|
+
body: { description: cmd.opts().description ?? "" }
|
|
873
|
+
});
|
|
874
|
+
return { status: "ok" };
|
|
875
|
+
})
|
|
876
|
+
);
|
|
877
|
+
cm.command("set-picture <id>").description("Set community picture (JPEG)").option("--data-file <path>", "JPEG file to upload").option("--data-base64 <base64>", "base64-encoded JPEG image").action(
|
|
878
|
+
action(async (c, cmd) => {
|
|
879
|
+
const [id] = cmd.args;
|
|
880
|
+
const o = cmd.opts();
|
|
881
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : o.dataBase64;
|
|
882
|
+
return c.request(`/communities/${encodeURIComponent(id)}/picture`, {
|
|
883
|
+
method: "POST",
|
|
884
|
+
body: { data }
|
|
885
|
+
});
|
|
886
|
+
})
|
|
887
|
+
);
|
|
888
|
+
cm.command("set-locked <id>").description("Set community locked mode").requiredOption("--enabled <bool>", "true | false").action(
|
|
889
|
+
action(async (c, cmd) => {
|
|
890
|
+
const [id] = cmd.args;
|
|
891
|
+
const enabled = cmd.opts().enabled === "true" || cmd.opts().enabled === true;
|
|
892
|
+
await c.request(`/communities/${encodeURIComponent(id)}/settings/locked`, {
|
|
893
|
+
method: "PUT",
|
|
894
|
+
body: { enabled }
|
|
895
|
+
});
|
|
896
|
+
return { status: "ok" };
|
|
897
|
+
})
|
|
898
|
+
);
|
|
899
|
+
cm.command("participants <id>").description("Get community participants").action(
|
|
900
|
+
action((c, cmd) => {
|
|
901
|
+
const [id] = cmd.args;
|
|
902
|
+
return c.request(`/communities/${encodeURIComponent(id)}/participants`);
|
|
903
|
+
})
|
|
904
|
+
);
|
|
905
|
+
cm.command("update-participants <id>").description("Add, remove, promote, or demote participants").requiredOption("--action <action>", "add | remove | promote | demote").requiredOption("--participants <jids>", "comma-separated JIDs", commaList).action(
|
|
906
|
+
action(async (c, cmd) => {
|
|
907
|
+
const [id] = cmd.args;
|
|
908
|
+
const o = cmd.opts();
|
|
909
|
+
await c.request(`/communities/${encodeURIComponent(id)}/participants`, {
|
|
910
|
+
method: "PUT",
|
|
911
|
+
body: { participants: o.participants, action: o.action }
|
|
912
|
+
});
|
|
913
|
+
return { status: "ok" };
|
|
914
|
+
})
|
|
915
|
+
);
|
|
916
|
+
cm.command("invite-link <id>").description("Get the community invite link").action(
|
|
917
|
+
action((c, cmd) => {
|
|
918
|
+
const [id] = cmd.args;
|
|
919
|
+
return c.request(`/communities/${encodeURIComponent(id)}/invite-link`);
|
|
920
|
+
})
|
|
921
|
+
);
|
|
922
|
+
cm.command("reset-invite <id>").description("Reset the community invite link").action(
|
|
923
|
+
action((c, cmd) => {
|
|
924
|
+
const [id] = cmd.args;
|
|
925
|
+
return c.request(`/communities/${encodeURIComponent(id)}/invite-link/reset`, { method: "POST" });
|
|
926
|
+
})
|
|
927
|
+
);
|
|
928
|
+
cm.command("subgroups <id>").description("List sub-groups linked to a community").action(
|
|
929
|
+
action((c, cmd) => {
|
|
930
|
+
const [id] = cmd.args;
|
|
931
|
+
return c.request(`/communities/${encodeURIComponent(id)}/groups`);
|
|
932
|
+
})
|
|
933
|
+
);
|
|
934
|
+
cm.command("create-subgroup <id>").description("Create a sub-group within a community").requiredOption("--name <name>", "sub-group name").option("--participants <jids>", "comma-separated JIDs", commaList).option("--data <json>", "extra body fields as JSON").action(
|
|
935
|
+
action((c, cmd) => {
|
|
936
|
+
const [id] = cmd.args;
|
|
937
|
+
const o = cmd.opts();
|
|
938
|
+
return c.request(`/communities/${encodeURIComponent(id)}/groups`, {
|
|
939
|
+
method: "POST",
|
|
940
|
+
body: buildBody({ name: o.name, participants: o.participants }, o.data)
|
|
941
|
+
});
|
|
942
|
+
})
|
|
943
|
+
);
|
|
944
|
+
cm.command("link-subgroup <id>").description("Link an existing group to a community as a sub-group").requiredOption("--group-id <jid>", "group JID to link").action(
|
|
945
|
+
action(async (c, cmd) => {
|
|
946
|
+
const [id] = cmd.args;
|
|
947
|
+
await c.request(`/communities/${encodeURIComponent(id)}/groups/link`, {
|
|
948
|
+
method: "POST",
|
|
949
|
+
body: { groupId: cmd.opts().groupId }
|
|
950
|
+
});
|
|
951
|
+
return { status: "ok" };
|
|
952
|
+
})
|
|
953
|
+
);
|
|
954
|
+
cm.command("unlink-subgroup <id> <groupId>").description("Unlink a sub-group from a community").action(
|
|
955
|
+
action(async (c, cmd) => {
|
|
956
|
+
const [id, groupId] = cmd.args;
|
|
957
|
+
await c.request(`/communities/${encodeURIComponent(id)}/groups/${encodeURIComponent(groupId)}`, {
|
|
958
|
+
method: "DELETE"
|
|
959
|
+
});
|
|
960
|
+
return { status: "ok" };
|
|
961
|
+
})
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// src/commands/contacts.ts
|
|
966
|
+
function registerContactsCommands(program2) {
|
|
967
|
+
const ct = program2.command("contacts").description("Contact lookup and management");
|
|
968
|
+
ct.command("list").description("List all contacts").action(action((c) => c.request("/contacts")));
|
|
969
|
+
ct.command("get <id>").description("Get a specific contact by JID").action(
|
|
970
|
+
action((c, cmd) => {
|
|
971
|
+
const [id] = cmd.args;
|
|
972
|
+
return c.request(`/contacts/${encodeURIComponent(id)}`);
|
|
973
|
+
})
|
|
974
|
+
);
|
|
975
|
+
ct.command("create").description("Create or update a contact").requiredOption("--id <id>", "phone number or JID of the contact").requiredOption("--full-name <name>", "full name of the contact").option("--first-name <name>", "first name").option("--data <json>", "extra body fields as JSON").action(
|
|
976
|
+
action(async (c, cmd) => {
|
|
977
|
+
const o = cmd.opts();
|
|
978
|
+
await c.request("/contacts", {
|
|
979
|
+
method: "POST",
|
|
980
|
+
body: buildBody(
|
|
981
|
+
{
|
|
982
|
+
id: o.id,
|
|
983
|
+
fullName: o.fullName,
|
|
984
|
+
firstName: o.firstName
|
|
985
|
+
},
|
|
986
|
+
o.data
|
|
987
|
+
)
|
|
988
|
+
});
|
|
989
|
+
return { status: "ok" };
|
|
990
|
+
})
|
|
991
|
+
);
|
|
992
|
+
ct.command("sync").description("Trigger a full contact sync from the WhatsApp server").action(
|
|
993
|
+
action(async (c) => {
|
|
994
|
+
await c.request("/contacts/sync", { method: "POST" });
|
|
995
|
+
return { status: "ok" };
|
|
996
|
+
})
|
|
997
|
+
);
|
|
998
|
+
ct.command("blocklist").description("List blocked contacts").action(action((c) => c.request("/contacts/blocklist")));
|
|
999
|
+
ct.command("block <id>").description("Block a contact").action(
|
|
1000
|
+
action(async (c, cmd) => {
|
|
1001
|
+
const [id] = cmd.args;
|
|
1002
|
+
await c.request(`/contacts/${encodeURIComponent(id)}/block`, { method: "PUT" });
|
|
1003
|
+
return { status: "ok" };
|
|
1004
|
+
})
|
|
1005
|
+
);
|
|
1006
|
+
ct.command("unblock <id>").description("Unblock a contact").action(
|
|
1007
|
+
action(async (c, cmd) => {
|
|
1008
|
+
const [id] = cmd.args;
|
|
1009
|
+
await c.request(`/contacts/${encodeURIComponent(id)}/unblock`, { method: "PUT" });
|
|
1010
|
+
return { status: "ok" };
|
|
1011
|
+
})
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// src/commands/users.ts
|
|
1016
|
+
function registerUsersCommands(program2) {
|
|
1017
|
+
const u = program2.command("users").description("Account management, user lookup, and profile");
|
|
1018
|
+
u.command("me").description("Get own profile info").action(action((c) => c.request("/users/me/profile")));
|
|
1019
|
+
u.command("update-me").description("Update own profile (name, status, picture)").option("--name <name>", "display name").option("--status <text>", "status text").option("--picture-file <path>", "JPEG file (base64-encoded for upload)").option("--picture-base64 <base64>", "base64-encoded JPEG").option("--data <json>", "extra body fields as JSON").action(
|
|
1020
|
+
action(async (c, cmd) => {
|
|
1021
|
+
const o = cmd.opts();
|
|
1022
|
+
const picture = o.pictureFile ? await readFileToBase64(o.pictureFile) : o.pictureBase64;
|
|
1023
|
+
await c.request("/users/me/profile", {
|
|
1024
|
+
method: "PUT",
|
|
1025
|
+
body: buildBody({ name: o.name, status: o.status, picture }, o.data)
|
|
1026
|
+
});
|
|
1027
|
+
return { status: "ok" };
|
|
1028
|
+
})
|
|
1029
|
+
);
|
|
1030
|
+
u.command("set-presence").description("Set presence to available or unavailable").requiredOption("--presence <state>", "available | unavailable").action(
|
|
1031
|
+
action(async (c, cmd) => {
|
|
1032
|
+
await c.request("/users/me/presence", {
|
|
1033
|
+
method: "PUT",
|
|
1034
|
+
body: { presence: cmd.opts().presence }
|
|
1035
|
+
});
|
|
1036
|
+
return { status: "ok" };
|
|
1037
|
+
})
|
|
1038
|
+
);
|
|
1039
|
+
u.command("privacy").description("Get privacy settings").action(action((c) => c.request("/users/me/privacy")));
|
|
1040
|
+
u.command("set-privacy").description("Update a single privacy setting").requiredOption("--setting <name>", "groupadd | last | status | profile | readreceipts | online | calladd").requiredOption("--value <value>", "all | contacts | contact_blacklist | match_last_seen | known | none").action(
|
|
1041
|
+
action((c, cmd) => {
|
|
1042
|
+
const o = cmd.opts();
|
|
1043
|
+
return c.request("/users/me/privacy", {
|
|
1044
|
+
method: "PUT",
|
|
1045
|
+
body: { setting: o.setting, value: o.value }
|
|
1046
|
+
});
|
|
1047
|
+
})
|
|
1048
|
+
);
|
|
1049
|
+
u.command("check <phone>").description("Check if a phone number is on WhatsApp").action(
|
|
1050
|
+
action((c, cmd) => {
|
|
1051
|
+
const [phone] = cmd.args;
|
|
1052
|
+
return c.request(`/users/${encodeURIComponent(phone)}/check`);
|
|
1053
|
+
})
|
|
1054
|
+
);
|
|
1055
|
+
u.command("check-bulk").description("Bulk check phone numbers on WhatsApp").requiredOption("--phones <list>", "comma-separated phone numbers", commaList).action(
|
|
1056
|
+
action((c, cmd) => {
|
|
1057
|
+
return c.request("/users/check", {
|
|
1058
|
+
method: "POST",
|
|
1059
|
+
body: { phones: cmd.opts().phones }
|
|
1060
|
+
});
|
|
1061
|
+
})
|
|
1062
|
+
);
|
|
1063
|
+
u.command("profile <phone>").description("Get a user's public profile info").action(
|
|
1064
|
+
action((c, cmd) => {
|
|
1065
|
+
const [phone] = cmd.args;
|
|
1066
|
+
return c.request(`/users/${encodeURIComponent(phone)}/profile`);
|
|
1067
|
+
})
|
|
1068
|
+
);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// src/commands/media.ts
|
|
1072
|
+
function registerMediaCommands(program2) {
|
|
1073
|
+
const md = program2.command("media").description("Download media");
|
|
1074
|
+
md.command("download <id>").description("Download media by ID (from a received message webhook event)").option("-o, --output <file>", "write to file (default: stdout)").action(async function(id, options) {
|
|
1075
|
+
try {
|
|
1076
|
+
const c = makeClient(this);
|
|
1077
|
+
const { data, filename } = await c.requestBinary("/media/download", {
|
|
1078
|
+
query: { id }
|
|
1079
|
+
});
|
|
1080
|
+
const out = options.output ?? filename ?? void 0;
|
|
1081
|
+
writeBinary(data, out);
|
|
1082
|
+
} catch (e) {
|
|
1083
|
+
handleError(e);
|
|
1084
|
+
}
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
// src/commands/chats.ts
|
|
1089
|
+
function registerChatsCommands(program2) {
|
|
1090
|
+
const ch = program2.command("chats").description("Chat listing, info, and settings");
|
|
1091
|
+
ch.command("list").description("List all known chats").action(action((c) => c.request("/chats")));
|
|
1092
|
+
ch.command("get <chatId>").description("Get chat info").action(
|
|
1093
|
+
action((c, cmd) => {
|
|
1094
|
+
const [chatId] = cmd.args;
|
|
1095
|
+
return c.request(`/chats/${encodeURIComponent(chatId)}`);
|
|
1096
|
+
})
|
|
1097
|
+
);
|
|
1098
|
+
ch.command("delete <chatId>").description("Delete a chat").action(
|
|
1099
|
+
action(async (c, cmd) => {
|
|
1100
|
+
const [chatId] = cmd.args;
|
|
1101
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}`, { method: "DELETE" });
|
|
1102
|
+
return { status: "ok" };
|
|
1103
|
+
})
|
|
1104
|
+
);
|
|
1105
|
+
ch.command("picture <chatId>").description("Get the chat's profile picture info").action(
|
|
1106
|
+
action((c, cmd) => {
|
|
1107
|
+
const [chatId] = cmd.args;
|
|
1108
|
+
return c.request(`/chats/${encodeURIComponent(chatId)}/picture`);
|
|
1109
|
+
})
|
|
1110
|
+
);
|
|
1111
|
+
ch.command("business <chatId>").description("Get business profile for a chat").action(
|
|
1112
|
+
action((c, cmd) => {
|
|
1113
|
+
const [chatId] = cmd.args;
|
|
1114
|
+
return c.request(`/chats/${encodeURIComponent(chatId)}/business`);
|
|
1115
|
+
})
|
|
1116
|
+
);
|
|
1117
|
+
ch.command("set-presence <chatId>").description("Send chat presence (typing, paused, recording)").requiredOption("--state <state>", "typing | paused | recording").action(
|
|
1118
|
+
action(async (c, cmd) => {
|
|
1119
|
+
const [chatId] = cmd.args;
|
|
1120
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/presence`, {
|
|
1121
|
+
method: "PUT",
|
|
1122
|
+
body: { state: cmd.opts().state }
|
|
1123
|
+
});
|
|
1124
|
+
return { status: "ok" };
|
|
1125
|
+
})
|
|
1126
|
+
);
|
|
1127
|
+
ch.command("subscribe-presence <chatId>").description("Subscribe to presence updates for a chat").action(
|
|
1128
|
+
action(async (c, cmd) => {
|
|
1129
|
+
const [chatId] = cmd.args;
|
|
1130
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/presence/subscribe`, { method: "PUT" });
|
|
1131
|
+
return { status: "ok" };
|
|
1132
|
+
})
|
|
1133
|
+
);
|
|
1134
|
+
ch.command("set-ephemeral <chatId>").description("Set disappearing-messages timer").requiredOption("--expiration <off|24h|7d|90d>", "timer value").action(
|
|
1135
|
+
action(async (c, cmd) => {
|
|
1136
|
+
const [chatId] = cmd.args;
|
|
1137
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/ephemeral`, {
|
|
1138
|
+
method: "PUT",
|
|
1139
|
+
body: { expiration: cmd.opts().expiration }
|
|
1140
|
+
});
|
|
1141
|
+
return { status: "ok" };
|
|
1142
|
+
})
|
|
1143
|
+
);
|
|
1144
|
+
ch.command("mute <chatId>").description("Mute or unmute a chat").requiredOption("--duration <8h|1w|always|off>", "mute duration").action(
|
|
1145
|
+
action(async (c, cmd) => {
|
|
1146
|
+
const [chatId] = cmd.args;
|
|
1147
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/mute`, {
|
|
1148
|
+
method: "PUT",
|
|
1149
|
+
body: { duration: cmd.opts().duration }
|
|
1150
|
+
});
|
|
1151
|
+
return { status: "ok" };
|
|
1152
|
+
})
|
|
1153
|
+
);
|
|
1154
|
+
ch.command("pin <chatId>").description("Pin or unpin a chat").option("--unpin", "unpin instead of pinning").action(
|
|
1155
|
+
action(async (c, cmd) => {
|
|
1156
|
+
const [chatId] = cmd.args;
|
|
1157
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/pin`, {
|
|
1158
|
+
method: "PUT",
|
|
1159
|
+
body: { pinned: !cmd.opts().unpin }
|
|
1160
|
+
});
|
|
1161
|
+
return { status: "ok" };
|
|
1162
|
+
})
|
|
1163
|
+
);
|
|
1164
|
+
ch.command("archive <chatId>").description("Archive or unarchive a chat").option("--unarchive", "unarchive instead of archiving").action(
|
|
1165
|
+
action(async (c, cmd) => {
|
|
1166
|
+
const [chatId] = cmd.args;
|
|
1167
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/archive`, {
|
|
1168
|
+
method: "PUT",
|
|
1169
|
+
body: { archived: !cmd.opts().unarchive }
|
|
1170
|
+
});
|
|
1171
|
+
return { status: "ok" };
|
|
1172
|
+
})
|
|
1173
|
+
);
|
|
1174
|
+
ch.command("read <chatId>").description("Mark a chat as read or unread").option("--unread", "mark unread instead of read").action(
|
|
1175
|
+
action(async (c, cmd) => {
|
|
1176
|
+
const [chatId] = cmd.args;
|
|
1177
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/read`, {
|
|
1178
|
+
method: "PUT",
|
|
1179
|
+
body: { read: !cmd.opts().unread }
|
|
1180
|
+
});
|
|
1181
|
+
return { status: "ok" };
|
|
1182
|
+
})
|
|
1183
|
+
);
|
|
1184
|
+
ch.command("request-history <chatId>").description("Request on-demand message history (delivered async via events)").requiredOption("--last-message-id <id>", "ID of the last known message").requiredOption("--last-message-sender-id <jid>", "sender of the last known message").option("--count <n>", "number of messages to request (1-500, default 50)", intArg).option("--data <json>", "extra body fields as JSON").action(
|
|
1185
|
+
action((c, cmd) => {
|
|
1186
|
+
const [chatId] = cmd.args;
|
|
1187
|
+
const o = cmd.opts();
|
|
1188
|
+
return c.request(`/chats/${encodeURIComponent(chatId)}/messages`, {
|
|
1189
|
+
method: "POST",
|
|
1190
|
+
body: buildBody(
|
|
1191
|
+
{
|
|
1192
|
+
lastMessageId: o.lastMessageId,
|
|
1193
|
+
lastMessageSenderId: o.lastMessageSenderId,
|
|
1194
|
+
count: o.count
|
|
1195
|
+
},
|
|
1196
|
+
o.data
|
|
1197
|
+
)
|
|
1198
|
+
});
|
|
1199
|
+
})
|
|
1200
|
+
);
|
|
1201
|
+
ch.command("clear <chatId>").description("Clear all messages from a chat").action(
|
|
1202
|
+
action(async (c, cmd) => {
|
|
1203
|
+
const [chatId] = cmd.args;
|
|
1204
|
+
await c.request(`/chats/${encodeURIComponent(chatId)}/clear`, { method: "POST" });
|
|
1205
|
+
return { status: "ok" };
|
|
1206
|
+
})
|
|
1207
|
+
);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
// src/commands/calls.ts
|
|
1211
|
+
function registerCallsCommands(program2) {
|
|
1212
|
+
const calls = program2.command("calls").description("Call management");
|
|
1213
|
+
calls.command("reject <callId>").description("Reject an incoming call").requiredOption("--caller-id <jid>", "JID of the caller").action(
|
|
1214
|
+
action(async (c, cmd) => {
|
|
1215
|
+
const [callId] = cmd.args;
|
|
1216
|
+
await c.request(`/calls/${encodeURIComponent(callId)}/reject`, {
|
|
1217
|
+
method: "POST",
|
|
1218
|
+
body: { callerId: cmd.opts().callerId }
|
|
1219
|
+
});
|
|
1220
|
+
return { status: "ok" };
|
|
1221
|
+
})
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
// src/commands/newsletters.ts
|
|
1226
|
+
function registerNewslettersCommands(program2) {
|
|
1227
|
+
const n = program2.command("newsletters").description("Newsletter / channel management");
|
|
1228
|
+
n.command("list").description("List subscribed newsletters").action(action((c) => c.request("/newsletters")));
|
|
1229
|
+
n.command("get <id>").description("Get newsletter info by JID").action(
|
|
1230
|
+
action((c, cmd) => {
|
|
1231
|
+
const [id] = cmd.args;
|
|
1232
|
+
return c.request(`/newsletters/${encodeURIComponent(id)}`);
|
|
1233
|
+
})
|
|
1234
|
+
);
|
|
1235
|
+
n.command("invite-info <code>").description("Look up a newsletter by invite code").action(
|
|
1236
|
+
action((c, cmd) => {
|
|
1237
|
+
const [code] = cmd.args;
|
|
1238
|
+
return c.request(`/newsletters/invite/${encodeURIComponent(code)}`);
|
|
1239
|
+
})
|
|
1240
|
+
);
|
|
1241
|
+
n.command("create").description("Create a newsletter").requiredOption("--name <name>", "newsletter name").option("--description <text>", "newsletter description").option("--picture-file <path>", "JPEG file for the newsletter picture").option("--picture-base64 <base64>", "base64 JPEG picture").option("--data <json>", "extra body fields as JSON").action(
|
|
1242
|
+
action(async (c, cmd) => {
|
|
1243
|
+
const o = cmd.opts();
|
|
1244
|
+
const picture = o.pictureFile ? await readFileToBase64(o.pictureFile) : o.pictureBase64;
|
|
1245
|
+
return c.request("/newsletters", {
|
|
1246
|
+
method: "POST",
|
|
1247
|
+
body: buildBody(
|
|
1248
|
+
{
|
|
1249
|
+
name: o.name,
|
|
1250
|
+
description: o.description,
|
|
1251
|
+
picture
|
|
1252
|
+
},
|
|
1253
|
+
o.data
|
|
1254
|
+
)
|
|
1255
|
+
});
|
|
1256
|
+
})
|
|
1257
|
+
);
|
|
1258
|
+
n.command("subscribe <id>").description("Subscribe to a newsletter").action(
|
|
1259
|
+
action(async (c, cmd) => {
|
|
1260
|
+
const [id] = cmd.args;
|
|
1261
|
+
await c.request(`/newsletters/${encodeURIComponent(id)}/subscription`, {
|
|
1262
|
+
method: "PUT",
|
|
1263
|
+
body: { subscribed: true }
|
|
1264
|
+
});
|
|
1265
|
+
return { status: "ok" };
|
|
1266
|
+
})
|
|
1267
|
+
);
|
|
1268
|
+
n.command("unsubscribe <id>").description("Unsubscribe from a newsletter").action(
|
|
1269
|
+
action(async (c, cmd) => {
|
|
1270
|
+
const [id] = cmd.args;
|
|
1271
|
+
await c.request(`/newsletters/${encodeURIComponent(id)}/subscription`, {
|
|
1272
|
+
method: "PUT",
|
|
1273
|
+
body: { subscribed: false }
|
|
1274
|
+
});
|
|
1275
|
+
return { status: "ok" };
|
|
1276
|
+
})
|
|
1277
|
+
);
|
|
1278
|
+
n.command("mute <id>").description("Mute a newsletter").action(
|
|
1279
|
+
action(async (c, cmd) => {
|
|
1280
|
+
const [id] = cmd.args;
|
|
1281
|
+
await c.request(`/newsletters/${encodeURIComponent(id)}/mute`, {
|
|
1282
|
+
method: "PUT",
|
|
1283
|
+
body: { mute: true }
|
|
1284
|
+
});
|
|
1285
|
+
return { status: "ok" };
|
|
1286
|
+
})
|
|
1287
|
+
);
|
|
1288
|
+
n.command("unmute <id>").description("Unmute a newsletter").action(
|
|
1289
|
+
action(async (c, cmd) => {
|
|
1290
|
+
const [id] = cmd.args;
|
|
1291
|
+
await c.request(`/newsletters/${encodeURIComponent(id)}/mute`, {
|
|
1292
|
+
method: "PUT",
|
|
1293
|
+
body: { mute: false }
|
|
1294
|
+
});
|
|
1295
|
+
return { status: "ok" };
|
|
1296
|
+
})
|
|
1297
|
+
);
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
// src/commands/status.ts
|
|
1301
|
+
function registerStatusCommands(program2) {
|
|
1302
|
+
const s = program2.command("status").description("Status / story posting");
|
|
1303
|
+
s.command("privacy").description("Get status broadcast privacy settings").action(action((c) => c.request("/status/privacy")));
|
|
1304
|
+
s.command("text").description("Post a text status").requiredOption("--text <text>", "status text").action(
|
|
1305
|
+
action((c, cmd) => {
|
|
1306
|
+
return c.request("/status/text", {
|
|
1307
|
+
method: "POST",
|
|
1308
|
+
body: { text: cmd.opts().text }
|
|
1309
|
+
});
|
|
1310
|
+
})
|
|
1311
|
+
);
|
|
1312
|
+
for (const kind of ["image", "video"]) {
|
|
1313
|
+
s.command(kind).description(`Post a${kind === "image" ? "n" : ""} ${kind} status`).option("--url <url>", "URL of the media").option("--data-file <path>", "local file to upload as base64").option("--mime-type <type>", "MIME type").option("--caption <text>", "caption").option("--data <json>", "extra body fields as JSON").action(
|
|
1314
|
+
action(async (c, cmd) => {
|
|
1315
|
+
const o = cmd.opts();
|
|
1316
|
+
const data = o.dataFile ? await readFileToBase64(o.dataFile) : void 0;
|
|
1317
|
+
return c.request(`/status/${kind}`, {
|
|
1318
|
+
method: "POST",
|
|
1319
|
+
body: buildBody(
|
|
1320
|
+
{
|
|
1321
|
+
url: o.url,
|
|
1322
|
+
data,
|
|
1323
|
+
mimeType: o.mimeType,
|
|
1324
|
+
caption: o.caption
|
|
1325
|
+
},
|
|
1326
|
+
o.data
|
|
1327
|
+
)
|
|
1328
|
+
});
|
|
1329
|
+
})
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1332
|
+
s.command("delete <messageId>").description("Delete (revoke) a previously-posted status").action(
|
|
1333
|
+
action(async (c, cmd) => {
|
|
1334
|
+
const [messageId] = cmd.args;
|
|
1335
|
+
await c.request(`/status/${encodeURIComponent(messageId)}/delete`, { method: "POST" });
|
|
1336
|
+
return { status: "ok" };
|
|
1337
|
+
})
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// src/index.ts
|
|
1342
|
+
var program = new Command();
|
|
1343
|
+
program.name("wsapi").description("Command-line client for the WSAPI WhatsApp cloud API").version("0.1.0").option("--profile <name>", "named profile from the config file").option("--api-key <key>", "WSAPI API key (overrides env and config)").option("--instance-id <id>", "WSAPI instance ID (overrides env and config)").option("--base-url <url>", "API base URL (default https://api.wsapi.chat)").option("--config <path>", "path to config file (default ~/.wsapi/config.json)");
|
|
1344
|
+
registerConfigCommand(program);
|
|
1345
|
+
registerSessionCommands(program);
|
|
1346
|
+
registerMessagesCommands(program);
|
|
1347
|
+
registerGroupsCommands(program);
|
|
1348
|
+
registerCommunitiesCommands(program);
|
|
1349
|
+
registerContactsCommands(program);
|
|
1350
|
+
registerUsersCommands(program);
|
|
1351
|
+
registerMediaCommands(program);
|
|
1352
|
+
registerChatsCommands(program);
|
|
1353
|
+
registerCallsCommands(program);
|
|
1354
|
+
registerNewslettersCommands(program);
|
|
1355
|
+
registerStatusCommands(program);
|
|
1356
|
+
program.parseAsync(process.argv).catch(handleError);
|
|
1357
|
+
//# sourceMappingURL=index.js.map
|
|
1358
|
+
//# sourceMappingURL=index.js.map
|