@getdial/cli 0.4.0 → 0.6.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/dist/cli.js +1 -3
- package/dist/commands/send.js +1 -5
- package/dist/commands/wait-for.js +58 -20
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -106,16 +106,14 @@ numbers
|
|
|
106
106
|
})));
|
|
107
107
|
program
|
|
108
108
|
.command("send")
|
|
109
|
-
.description("Send an SMS
|
|
109
|
+
.description("Send an SMS. POST /api/v1/messages.")
|
|
110
110
|
.requiredOption("--to <e164>", "destination phone number, E.164 (e.g. +14155551234)")
|
|
111
111
|
.requiredOption("--body <text>", "message body")
|
|
112
|
-
.option("--channel <sms|whatsapp>", "channel to send on", "sms")
|
|
113
112
|
.option("--from-number-id <id>", "phone_number_id to send from (defaults to onboard's number)")
|
|
114
113
|
.option("--json", "machine-readable output")
|
|
115
114
|
.action(async (opts) => process.exit(await runSend({
|
|
116
115
|
to: opts.to,
|
|
117
116
|
body: opts.body,
|
|
118
|
-
channel: opts.channel,
|
|
119
117
|
fromNumberId: opts.fromNumberId,
|
|
120
118
|
json: !!opts.json,
|
|
121
119
|
})));
|
package/dist/commands/send.js
CHANGED
|
@@ -11,11 +11,7 @@ export async function runSend(opts) {
|
|
|
11
11
|
fail(opts.json, "no_from_number", "No default phone_number_id in auth. Pass --from-number-id <id>.");
|
|
12
12
|
return 1;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
fail(opts.json, "bad_channel", `--channel must be sms or whatsapp, got "${opts.channel}".`);
|
|
16
|
-
return 1;
|
|
17
|
-
}
|
|
18
|
-
const res = await apiPost("/api/v1/messages", { to: opts.to, body: opts.body, channel: opts.channel, from_number_id: fromNumberId }, auth.api_key);
|
|
14
|
+
const res = await apiPost("/api/v1/messages", { to: opts.to, body: opts.body, channel: "sms", from_number_id: fromNumberId }, auth.api_key);
|
|
19
15
|
if (!res.ok) {
|
|
20
16
|
fail(opts.json, "send_failed", res.error, { status: res.status });
|
|
21
17
|
return 2;
|
|
@@ -1,33 +1,23 @@
|
|
|
1
|
+
import { readAuth } from "../lib/state.js";
|
|
1
2
|
import { paths } from "../lib/paths.js";
|
|
2
3
|
import { supervisorStatus } from "../lib/supervisor/index.js";
|
|
3
4
|
import { parseFieldArg, parseRegexArg } from "../lib/event-filter.js";
|
|
4
5
|
import { currentSize, findLatestMatch, tailUntilMatch } from "../lib/log-tail.js";
|
|
6
|
+
import { apiPost } from "../lib/api.js";
|
|
7
|
+
const PER_POLL_SECONDS = 30;
|
|
5
8
|
export async function runWaitFor(opts) {
|
|
6
|
-
const status = supervisorStatus();
|
|
7
|
-
if (!status.installed || !status.running) {
|
|
8
|
-
const hint = !status.installed
|
|
9
|
-
? "Run `dial listen install` (or rerun `dial onboard`)."
|
|
10
|
-
: "It's installed but not running. Reinstall with `dial listen install` to restart it.";
|
|
11
|
-
if (opts.json) {
|
|
12
|
-
console.log(JSON.stringify({
|
|
13
|
-
ok: false,
|
|
14
|
-
reason: "listen_not_running",
|
|
15
|
-
installed: status.installed,
|
|
16
|
-
running: status.running,
|
|
17
|
-
pid: status.pid,
|
|
18
|
-
unit_path: status.unitPath,
|
|
19
|
-
}));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
console.error(`listen daemon is not running (installed=${status.installed}, running=${status.running}). ${hint}`);
|
|
23
|
-
}
|
|
24
|
-
return 3;
|
|
25
|
-
}
|
|
26
9
|
const spec = {
|
|
27
10
|
eventType: opts.eventType,
|
|
28
11
|
fields: opts.fields.map(parseFieldArg),
|
|
29
12
|
regexes: opts.regexes.map(parseRegexArg),
|
|
30
13
|
};
|
|
14
|
+
const status = supervisorStatus();
|
|
15
|
+
if (status.installed && status.running) {
|
|
16
|
+
return waitFromLog(spec, opts);
|
|
17
|
+
}
|
|
18
|
+
return waitFromApi(spec, opts);
|
|
19
|
+
}
|
|
20
|
+
async function waitFromLog(spec, opts) {
|
|
31
21
|
const file = paths().listenLog;
|
|
32
22
|
const startOffset = currentSize(file);
|
|
33
23
|
const hit = await tailUntilMatch(file, spec, startOffset, opts.timeoutSeconds * 1000);
|
|
@@ -54,3 +44,51 @@ export async function runWaitFor(opts) {
|
|
|
54
44
|
}
|
|
55
45
|
return 2;
|
|
56
46
|
}
|
|
47
|
+
async function waitFromApi(spec, opts) {
|
|
48
|
+
const auth = readAuth();
|
|
49
|
+
if (!auth) {
|
|
50
|
+
fail(opts.json, "not_signed_in", "Not signed in. Run `dial signup` and `dial onboard` first.");
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
const filters = {};
|
|
54
|
+
for (const f of spec.fields)
|
|
55
|
+
filters[f.name] = f.value;
|
|
56
|
+
const regexFilters = {};
|
|
57
|
+
for (const r of spec.regexes)
|
|
58
|
+
regexFilters[r.name] = { pattern: r.regex.source, flags: r.regex.flags };
|
|
59
|
+
const deadline = Date.now() + opts.timeoutSeconds * 1000;
|
|
60
|
+
while (Date.now() < deadline) {
|
|
61
|
+
const remainingSec = Math.max(1, Math.ceil((deadline - Date.now()) / 1000));
|
|
62
|
+
const timeout = Math.min(PER_POLL_SECONDS, remainingSec);
|
|
63
|
+
const res = await apiPost("/api/v1/events/wait", {
|
|
64
|
+
event_type: spec.eventType,
|
|
65
|
+
filters: Object.keys(filters).length > 0 ? filters : undefined,
|
|
66
|
+
regex_filters: Object.keys(regexFilters).length > 0 ? regexFilters : undefined,
|
|
67
|
+
timeout,
|
|
68
|
+
}, auth.api_key);
|
|
69
|
+
if (res.ok && res.data?.event) {
|
|
70
|
+
process.stdout.write(JSON.stringify(res.data.event) + "\n");
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
if (res.ok === false && res.status === 408) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (res.ok === false) {
|
|
77
|
+
fail(opts.json, "api_fallback_failed", res.error, { status: res.status });
|
|
78
|
+
return 4;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (opts.json) {
|
|
82
|
+
console.log(JSON.stringify({ ok: false, timeout: true, source: "api", event: null }));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
console.error(`timed out after ${opts.timeoutSeconds}s; no matching ${opts.eventType} via API fallback.`);
|
|
86
|
+
}
|
|
87
|
+
return 2;
|
|
88
|
+
}
|
|
89
|
+
function fail(json, code, message, extra) {
|
|
90
|
+
if (json)
|
|
91
|
+
console.log(JSON.stringify({ ok: false, code, message, ...extra }));
|
|
92
|
+
else
|
|
93
|
+
console.error(message);
|
|
94
|
+
}
|