@zerodot/whatsapp 1.0.0 → 1.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.
Files changed (2) hide show
  1. package/dist/index.cjs +77 -12
  2. package/package.json +4 -4
package/dist/index.cjs CHANGED
@@ -133,7 +133,7 @@ async function callSubscriptions(app, call, options) {
133
133
  // src/icon.ts
134
134
  const WHATSAPP_ICON = (0, _zerodot_node_sdk.fileIcon)(require("url").pathToFileURL(__filename).href, "../whatsapp.svg");
135
135
  // src/credential.ts
136
- const TIMEOUT_MS$1 = 1e4;
136
+ const TIMEOUT_MS$2 = 1e4;
137
137
  const whatsappCredential = {
138
138
  type: WHATSAPP_CREDENTIAL,
139
139
  label: "WhatsApp API",
@@ -153,8 +153,8 @@ const whatsappCredential = {
153
153
  path: `/${encodeURIComponent(access.businessAccountId)}`,
154
154
  query: { fields: "id,name" }
155
155
  }, {
156
- signal: AbortSignal.timeout(TIMEOUT_MS$1),
157
- timeoutMs: TIMEOUT_MS$1,
156
+ signal: AbortSignal.timeout(TIMEOUT_MS$2),
157
+ timeoutMs: TIMEOUT_MS$2,
158
158
  version: DEFAULT_VERSION
159
159
  });
160
160
  }
@@ -173,8 +173,8 @@ const whatsappTriggerCredential = {
173
173
  iconUrl: WHATSAPP_ICON,
174
174
  async test(credential) {
175
175
  await appAccessToken(whatsappAppAccess(credential), {
176
- signal: AbortSignal.timeout(TIMEOUT_MS$1),
177
- timeoutMs: TIMEOUT_MS$1
176
+ signal: AbortSignal.timeout(TIMEOUT_MS$2),
177
+ timeoutMs: TIMEOUT_MS$2
178
178
  });
179
179
  }
180
180
  };
@@ -358,7 +358,8 @@ const whatsappConfig = zod.z.object({
358
358
  resource: ["media"],
359
359
  operation: ["mediaUpload"]
360
360
  }),
361
- title: "Sender Phone Number ID",
361
+ loadOptions: "getPhoneNumbers",
362
+ title: "Sender Phone Number (or ID)",
362
363
  description: "ID of the phone number of the business account. The messages come from it, and the files go to it"
363
364
  }),
364
365
  recipientPhoneNumber: zod.z.string().optional().meta({
@@ -477,7 +478,8 @@ const whatsappConfig = zod.z.object({
477
478
  }),
478
479
  template: zod.z.string().optional().meta({
479
480
  ...on({ operation: ["sendTemplate"] }),
480
- description: "The template in the format name|language, for example hello_world|en_US"
481
+ loadOptions: "getTemplates",
482
+ description: "Pick a template, or type one in the format name|language, for example hello_world|en_US"
481
483
  }),
482
484
  components: zod.z.object({ component: zod.z.array(templateComponent) }).partial().optional().meta(on({ operation: ["sendTemplate"] })),
483
485
  message: zod.z.string().optional().meta({
@@ -519,12 +521,16 @@ const whatsappConfig = zod.z.object({
519
521
  limitWaitTimeHours: zod.z.number().min(.01).max(720)
520
522
  }).partial().optional().meta({
521
523
  ...on({ operation: ["sendAndWait"] }),
524
+ ui: "collection",
522
525
  description: "limitWaitTimeHours: how long to wait for an answer. Default 168 (7 days)."
523
526
  }),
524
- additionalFields: additionalFields.default({}).meta(on({ operation: ["send"] }, {
525
- resource: ["media"],
526
- operation: ["mediaUpload"]
527
- })),
527
+ additionalFields: additionalFields.default({}).meta({
528
+ ...on({ operation: ["send"] }, {
529
+ resource: ["media"],
530
+ operation: ["mediaUpload"]
531
+ }),
532
+ ui: "collection"
533
+ }),
528
534
  timeoutMs: zod.z.number().int().min(1).max(3e5).default(3e4)
529
535
  }).superRefine((config, context) => {
530
536
  if (!OPERATIONS_OF[config.resource].includes(config.operation)) context.addIssue({
@@ -533,6 +539,61 @@ const whatsappConfig = zod.z.object({
533
539
  message: `"${config.operation}" is not an operation of "${config.resource}"`
534
540
  });
535
541
  });
542
+ // src/loaders.ts
543
+ const TIMEOUT_MS$1 = 15e3;
544
+ const byLabel = (a, b) => {
545
+ const left = a.label.toLowerCase();
546
+ const right = b.label.toLowerCase();
547
+ return left < right ? -1 : left > right ? 1 : 0;
548
+ };
549
+ /** The version of the config, or the default. The config in the editor can be partial. */
550
+ const versionOf = (config) => typeof config.apiVersion === "string" && /^v\d+\.\d+$/.test(config.apiVersion) ? config.apiVersion : DEFAULT_VERSION;
551
+ /** One page of a list of the business account, with the cursor of the next page. */
552
+ async function listOf(ctx, edge, query) {
553
+ const access = whatsappAccess(ctx.credential);
554
+ if (access.businessAccountId === "") return null;
555
+ const answer = await callWhatsApp(access, {
556
+ method: "GET",
557
+ path: `/${segment(access.businessAccountId)}/${edge}`,
558
+ query: ctx.cursor ? {
559
+ ...query,
560
+ after: ctx.cursor
561
+ } : query
562
+ }, {
563
+ signal: ctx.signal,
564
+ timeoutMs: TIMEOUT_MS$1,
565
+ version: versionOf(ctx.config)
566
+ });
567
+ const after = answer.paging?.next ? answer.paging.cursors?.after : void 0;
568
+ return {
569
+ items: answer.data ?? [],
570
+ nextCursor: after ?? null
571
+ };
572
+ }
573
+ /** The phone numbers of the business account: "number - verified name", sorted by that text. */
574
+ async function getPhoneNumbers(ctx) {
575
+ const page = await listOf(ctx, "phone_numbers", {});
576
+ if (!page) return [];
577
+ return {
578
+ options: page.items.filter((item) => typeof item.id === "string").map((item) => ({
579
+ label: `${item.display_phone_number ?? ""} - ${item.verified_name ?? ""}`,
580
+ value: item.id
581
+ })).sort(byLabel),
582
+ nextCursor: page.nextCursor
583
+ };
584
+ }
585
+ /** The templates of the business account: "name - language", with the value `name|language`. */
586
+ async function getTemplates(ctx) {
587
+ const page = await listOf(ctx, "message_templates", { fields: "name,language" });
588
+ if (!page) return [];
589
+ return {
590
+ options: page.items.filter((item) => typeof item.name === "string" && typeof item.language === "string").map(({ name, language }) => ({
591
+ label: `${name} - ${language}`,
592
+ value: `${name}|${language}`
593
+ })).sort(byLabel),
594
+ nextCursor: page.nextCursor
595
+ };
596
+ }
536
597
  // src/request.ts
537
598
  const need = (value, name, label) => {
538
599
  if (value === void 0 || value.trim() === "") throw new Error(`WhatsApp ${label} needs "${name}"`);
@@ -849,6 +910,10 @@ var node_default = (0, _zerodot_node_sdk.defineNode)({
849
910
  out: ["out"]
850
911
  },
851
912
  subtitle: (config) => typeof config.operation === "string" ? `${String(config.resource ?? "message")}: ${config.operation}` : void 0,
913
+ loadOptions: {
914
+ getPhoneNumbers,
915
+ getTemplates
916
+ },
852
917
  waitForm: waitFormOf,
853
918
  async run({ config: c, input, credential, signal, resumeUrl }) {
854
919
  const access = whatsappAccess(credential);
@@ -939,7 +1004,7 @@ const triggerConfig = zod.z.object({
939
1004
  sent: "Sent"
940
1005
  },
941
1006
  description: "WhatsApp sends a notice when the status of a message changes, for example from Sent to Delivered and from Delivered to Read. Select the statuses that start the workflow, to avoid more than one run for one message"
942
- }) }).partial().optional()
1007
+ }) }).partial().optional().meta({ ui: "collection" })
943
1008
  });
944
1009
  /** The `X-Hub-Signature-256` value of a webhook request from Meta. */
945
1010
  function whatsappSignature(secret, rawBody) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerodot/whatsapp",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Send WhatsApp messages and templates, upload and manage media with the WhatsApp Business Cloud API, wait for a reply, and start a workflow on WhatsApp webhook events.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "repository": {
@@ -21,7 +21,7 @@
21
21
  "whatsapp.svg"
22
22
  ],
23
23
  "zerodot": {
24
- "sdk": "^1.2",
24
+ "sdk": "^1.7",
25
25
  "label": "WhatsApp",
26
26
  "summary": "Send WhatsApp messages and react to webhook events"
27
27
  },
@@ -30,12 +30,12 @@
30
30
  },
31
31
  "dependencies": {},
32
32
  "peerDependencies": {
33
- "@zerodot/node-sdk": "^1.2.0",
33
+ "@zerodot/node-sdk": "^1.7.0",
34
34
  "zod": "^4.0.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "26.6.5",
38
- "@zerodot/node-sdk": "^1.2.1",
38
+ "@zerodot/node-sdk": "^1.7.0",
39
39
  "tsdown": "0.23.2",
40
40
  "tsx": "4.23.15",
41
41
  "typescript": "7.0.2",