@vatio-ai/cli 0.127.0 → 0.129.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/lib/api-client.mjs +8 -1
- package/lib/commands/webhooks.mjs +76 -3
- package/lib/help.mjs +5 -0
- package/package.json +1 -1
package/lib/api-client.mjs
CHANGED
|
@@ -272,9 +272,16 @@ export class ApiClient extends HttpClient {
|
|
|
272
272
|
return this.get("/templates", { environment });
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
createTemplate({ name, language, category, body, examples = [], footer = null
|
|
275
|
+
createTemplate({ name, language, category, body = null, examples = [], footer = null, buttons = [], components = null,
|
|
276
|
+
parameterFormat = null }) {
|
|
277
|
+
if (components) {
|
|
278
|
+
const payload = { name, language, category, components };
|
|
279
|
+
if (parameterFormat) payload.parameter_format = parameterFormat;
|
|
280
|
+
return this.post("/templates", payload);
|
|
281
|
+
}
|
|
276
282
|
const payload = { name, language, category, body, examples };
|
|
277
283
|
if (String(footer ?? "").trim() !== "") payload.footer = String(footer).trim();
|
|
284
|
+
if (buttons.length > 0) payload.buttons = buttons;
|
|
278
285
|
return this.post("/templates", payload);
|
|
279
286
|
}
|
|
280
287
|
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
// reports what happened. Sending itself is the backend's job, with a server
|
|
5
5
|
// key, not the CLI's.
|
|
6
6
|
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
|
|
7
9
|
import { deployClient } from "./deploy.mjs";
|
|
8
10
|
import { fail, paint, sleep, takeEnv, takeFlag, takeValue } from "../support.mjs";
|
|
9
11
|
|
|
@@ -18,7 +20,10 @@ function takeAll(args, name) {
|
|
|
18
20
|
|
|
19
21
|
const LIST_USAGE = "vatio whatsapp templates list [--env live] [--json]";
|
|
20
22
|
const CREATE_USAGE = "vatio whatsapp templates create NAME --language CODE --body TEXT [--example VALUE]... " +
|
|
21
|
-
"[--category utility|marketing] [--footer TEXT] [--
|
|
23
|
+
"[--category utility|marketing] [--footer TEXT] [--button-url 'TEXT|URL']... [--button-example VALUE]... " +
|
|
24
|
+
"[--button-phone 'TEXT|PHONE'] [--button-quick-reply TEXT]... [--json]\n" +
|
|
25
|
+
" vatio whatsapp templates create [NAME] --components FILE [--language CODE] " +
|
|
26
|
+
"[--category utility|marketing|authentication] [--parameter-format positional|named] [--json]";
|
|
22
27
|
const RM_USAGE = "vatio whatsapp templates rm NAME [--language CODE] [--json]";
|
|
23
28
|
const SYNC_USAGE = "vatio whatsapp templates sync [--json]";
|
|
24
29
|
|
|
@@ -69,6 +74,7 @@ export async function templates(config, args) {
|
|
|
69
74
|
const count = template.parameter_count ?? "?";
|
|
70
75
|
console.log(` ${template.name} (${template.language}) ${template.category} ${status} ${count} param${count === 1 ? "" : "s"}`);
|
|
71
76
|
if (template.body) console.log(` ${template.body.replace(/\n/g, "\n ")}`);
|
|
77
|
+
if (template.buttons?.length) console.log(` buttons: ${template.buttons.map(describeButton).join(" · ")}`);
|
|
72
78
|
if (template.status === "REJECTED" && template.rejected_reason) console.log(` ${paint("red", "rejected:")} ${template.rejected_reason}`);
|
|
73
79
|
if (template.status === "APPROVED") console.log(` "template": ${JSON.stringify(template.example)}`);
|
|
74
80
|
}
|
|
@@ -81,23 +87,56 @@ export async function templates(config, args) {
|
|
|
81
87
|
// decides where, so there is no environment to choose.
|
|
82
88
|
async function createTemplate(config, args, json) {
|
|
83
89
|
takeValue(args, "--env");
|
|
90
|
+
const file = takeValue(args, "--components");
|
|
91
|
+
if (file !== null) return createFromComponents(config, args, json, file);
|
|
92
|
+
|
|
84
93
|
const language = takeValue(args, "--language");
|
|
85
94
|
const body = takeValue(args, "--body");
|
|
86
95
|
const category = takeValue(args, "--category") ?? "utility";
|
|
87
96
|
const footer = takeValue(args, "--footer");
|
|
88
97
|
const examples = takeAll(args, "--example");
|
|
98
|
+
const buttons = takeButtons(args);
|
|
89
99
|
const name = args.shift();
|
|
90
100
|
if (!name || !language || !body || args.length > 0) fail(`Usage: ${CREATE_USAGE}`);
|
|
91
101
|
|
|
92
102
|
const workspace = config.resolveWorkspaceRequired();
|
|
93
103
|
const client = deployClient(config, workspace);
|
|
94
|
-
const template = await client.createTemplate({ name, language, category, body, examples, footer });
|
|
104
|
+
const template = await client.createTemplate({ name, language, category, body, examples, footer, buttons });
|
|
105
|
+
printCreated(template, json, template.parameter_count ?? examples.length);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Meta's own structure, as its Message Templates API takes it: a JSON file
|
|
109
|
+
// (or - for stdin) with the components list, or the whole request body
|
|
110
|
+
// ({ name, language, category, parameter_format, components }), whose fields
|
|
111
|
+
// the flags override.
|
|
112
|
+
async function createFromComponents(config, args, json, file) {
|
|
113
|
+
let parsed;
|
|
114
|
+
try {
|
|
115
|
+
parsed = JSON.parse(readFileSync(file === "-" ? 0 : file, "utf8"));
|
|
116
|
+
} catch (error) {
|
|
117
|
+
fail(`--components: could not read ${file === "-" ? "stdin" : file} as JSON (${error.message})`);
|
|
118
|
+
}
|
|
119
|
+
const request = Array.isArray(parsed) ? { components: parsed } : (parsed ?? {});
|
|
120
|
+
const language = takeValue(args, "--language") ?? request.language;
|
|
121
|
+
const category = takeValue(args, "--category") ?? request.category ?? "utility";
|
|
122
|
+
const parameterFormat = takeValue(args, "--parameter-format") ?? request.parameter_format;
|
|
123
|
+
const name = args.shift() ?? request.name;
|
|
124
|
+
if (!name || !language || !Array.isArray(request.components) || args.length > 0) fail(`Usage: ${CREATE_USAGE}`);
|
|
125
|
+
|
|
126
|
+
const client = deployClient(config, config.resolveWorkspaceRequired());
|
|
127
|
+
const template = await client.createTemplate({
|
|
128
|
+
name, language, category: String(category).toLowerCase(), components: request.components,
|
|
129
|
+
parameterFormat: parameterFormat ? String(parameterFormat).toLowerCase() : null
|
|
130
|
+
});
|
|
131
|
+
printCreated(template, json, template.parameter_count ?? 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function printCreated(template, json, count) {
|
|
95
135
|
if (json) {
|
|
96
136
|
console.log(JSON.stringify(template, null, 2));
|
|
97
137
|
return;
|
|
98
138
|
}
|
|
99
139
|
const own = template.whatsapp_connected === true;
|
|
100
|
-
const count = template.parameter_count ?? examples.length;
|
|
101
140
|
console.log(`Submitted ${template.name} (${template.language}) for Meta's review on ` +
|
|
102
141
|
`${own ? "your own number" : "Vatio's test number"}: ${template.category}, ` +
|
|
103
142
|
`${count} param${count === 1 ? "" : "s"}, ${paint("cyan", template.status)}.`);
|
|
@@ -114,6 +153,40 @@ async function createTemplate(config, args, json) {
|
|
|
114
153
|
}
|
|
115
154
|
}
|
|
116
155
|
|
|
156
|
+
// In the order WhatsApp draws them: links, the phone, then quick replies,
|
|
157
|
+
// which Meta wants together. A link ending in {{1}} takes its value per
|
|
158
|
+
// message, and its --button-example for Meta's review, in order.
|
|
159
|
+
function takeButtons(args) {
|
|
160
|
+
const pair = (flag, value, second) => {
|
|
161
|
+
const at = value.indexOf("|");
|
|
162
|
+
if (at <= 0 || at === value.length - 1) fail(`${flag} takes "TEXT|${second}", like "Ver pedido|https://example.com/orders/{{1}}"`);
|
|
163
|
+
return [value.slice(0, at).trim(), value.slice(at + 1).trim()];
|
|
164
|
+
};
|
|
165
|
+
const urls = takeAll(args, "--button-url").map((value) => {
|
|
166
|
+
const [text, url] = pair("--button-url", value, "URL");
|
|
167
|
+
return { type: "url", text, url };
|
|
168
|
+
});
|
|
169
|
+
const phones = takeAll(args, "--button-phone").map((value) => {
|
|
170
|
+
const [text, phone] = pair("--button-phone", value, "PHONE");
|
|
171
|
+
return { type: "phone_number", text, phone_number: phone };
|
|
172
|
+
});
|
|
173
|
+
const replies = takeAll(args, "--button-quick-reply").map((text) => ({ type: "quick_reply", text }));
|
|
174
|
+
|
|
175
|
+
const examples = takeAll(args, "--button-example");
|
|
176
|
+
const dynamic = urls.filter((button) => button.url.endsWith("{{1}}"));
|
|
177
|
+
if (examples.length !== dynamic.length) {
|
|
178
|
+
fail(`--button-example: ${examples.length} given, ${dynamic.length} needed, one per --button-url ` +
|
|
179
|
+
"ending in {{1}}, in order.");
|
|
180
|
+
}
|
|
181
|
+
dynamic.forEach((button, index) => { button.example = examples[index]; });
|
|
182
|
+
return [...urls, ...phones, ...replies];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function describeButton(button) {
|
|
186
|
+
const target = button.url ?? button.phone_number;
|
|
187
|
+
return target ? `${button.text} (${target})` : button.text;
|
|
188
|
+
}
|
|
189
|
+
|
|
117
190
|
// Where create put it: the workspace's own number, or Vatio's test number
|
|
118
191
|
// before it has one. Every language under the name unless one is given.
|
|
119
192
|
async function deleteTemplate(config, args, json) {
|
package/lib/help.mjs
CHANGED
|
@@ -223,6 +223,11 @@ the two you are in. Commands default as listed.
|
|
|
223
223
|
The number's templates and the "template" to send
|
|
224
224
|
whatsapp templates create NAME --language CODE --body TEXT [--example VALUE]...
|
|
225
225
|
[--category utility|marketing] [--footer TEXT]
|
|
226
|
+
[--button-url 'TEXT|URL']... [--button-example VALUE]...
|
|
227
|
+
[--button-phone 'TEXT|PHONE'] [--button-quick-reply TEXT]...
|
|
228
|
+
whatsapp templates create [NAME] --components FILE [--language CODE]
|
|
229
|
+
[--category utility|marketing|authentication]
|
|
230
|
+
[--parameter-format positional|named]
|
|
226
231
|
Submit one for Meta's review: on your number, or
|
|
227
232
|
on Vatio's test number until you connect one
|
|
228
233
|
whatsapp templates rm NAME [--language CODE] | sync
|