whatsapp-cloud 0.0.4 → 0.0.6

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 (51) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/agent_docs/INCOMING_MESSAGES_BRAINSTORM.md +500 -0
  3. package/cloud-api-docs/webhooks/endpoint.md +112 -0
  4. package/cloud-api-docs/webhooks/overview.md +154 -0
  5. package/docs/DEVELOPMENT.md +154 -0
  6. package/docs/webhooks.md +76 -0
  7. package/package.json +6 -2
  8. package/src/client/HttpClient.ts +43 -6
  9. package/src/client/WhatsAppClient.ts +6 -0
  10. package/src/examples/main.ts +9 -0
  11. package/src/examples/template.ts +134 -0
  12. package/src/index.ts +7 -0
  13. package/src/schemas/client.ts +2 -2
  14. package/src/schemas/index.ts +2 -0
  15. package/src/schemas/templates/component.ts +145 -0
  16. package/src/schemas/templates/index.ts +4 -0
  17. package/src/schemas/templates/request.ts +78 -0
  18. package/src/schemas/templates/response.ts +64 -0
  19. package/src/schemas/webhooks/incoming-message.ts +38 -0
  20. package/src/schemas/webhooks/index.ts +3 -0
  21. package/src/schemas/webhooks/payload.ts +56 -0
  22. package/src/services/accounts/AccountsClient.ts +6 -14
  23. package/src/services/accounts/AccountsService.ts +19 -21
  24. package/src/services/accounts/methods/list-phone-numbers.ts +1 -2
  25. package/src/services/business/BusinessClient.ts +1 -9
  26. package/src/services/business/BusinessService.ts +19 -21
  27. package/src/services/business/methods/list-accounts.ts +1 -2
  28. package/src/services/messages/MessagesClient.ts +2 -6
  29. package/src/services/messages/MessagesService.ts +42 -22
  30. package/src/services/templates/TemplatesClient.ts +35 -0
  31. package/src/services/templates/TemplatesService.ts +117 -0
  32. package/src/services/templates/index.ts +3 -0
  33. package/src/services/templates/methods/create.ts +27 -0
  34. package/src/services/templates/methods/delete.ts +38 -0
  35. package/src/services/templates/methods/get.ts +23 -0
  36. package/src/services/templates/methods/list.ts +36 -0
  37. package/src/services/templates/methods/update.ts +35 -0
  38. package/src/services/webhooks/WebhooksService.ts +214 -0
  39. package/src/services/webhooks/index.ts +3 -0
  40. package/src/services/webhooks/utils/extract-messages.ts +25 -0
  41. package/src/services/webhooks/utils/extract-statuses.ts +25 -0
  42. package/src/services/webhooks/utils/verify.ts +29 -0
  43. package/src/types/index.ts +2 -0
  44. package/src/types/templates/component.ts +33 -0
  45. package/src/types/templates/index.ts +4 -0
  46. package/src/types/templates/request.ts +28 -0
  47. package/src/types/templates/response.ts +34 -0
  48. package/src/types/webhooks/incoming-message.ts +16 -0
  49. package/src/types/webhooks/index.ts +3 -0
  50. package/src/types/webhooks/payload.ts +8 -0
  51. package/tsconfig.json +2 -3
@@ -0,0 +1,25 @@
1
+ import type { WebhookPayload } from "../../../types/webhooks";
2
+
3
+ /**
4
+ * Extract status updates from webhook payload
5
+ *
6
+ * Flattens the nested structure: entry[].changes[].value.statuses[]
7
+ * Returns a flat array of status updates
8
+ *
9
+ * @param payload - Webhook payload from Meta
10
+ * @returns Flat array of status updates
11
+ */
12
+ export function extractStatuses(payload: WebhookPayload): unknown[] {
13
+ const statuses: unknown[] = [];
14
+
15
+ for (const entry of payload.entry) {
16
+ for (const change of entry.changes) {
17
+ if (change.field === "messages" && change.value.statuses) {
18
+ statuses.push(...change.value.statuses);
19
+ }
20
+ }
21
+ }
22
+
23
+ return statuses;
24
+ }
25
+
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Verify webhook GET request from Meta
3
+ *
4
+ * Meta sends GET requests to verify webhook endpoints:
5
+ * GET /webhook?hub.mode=subscribe&hub.challenge=<CHALLENGE>&hub.verify_token=<TOKEN>
6
+ *
7
+ * @param query - Query parameters from GET request
8
+ * @param verifyToken - Your verification token (stored on your server)
9
+ * @returns Challenge string if valid, null if invalid
10
+ */
11
+ export function verifyWebhook(
12
+ query: {
13
+ "hub.mode"?: string;
14
+ "hub.verify_token"?: string;
15
+ "hub.challenge"?: string;
16
+ },
17
+ verifyToken: string
18
+ ): string | null {
19
+ const mode = query["hub.mode"];
20
+ const token = query["hub.verify_token"];
21
+ const challenge = query["hub.challenge"];
22
+
23
+ // Verify mode is "subscribe" and token matches
24
+ if (mode === "subscribe" && token === verifyToken && challenge) {
25
+ return challenge;
26
+ }
27
+
28
+ return null;
29
+ }
@@ -2,4 +2,6 @@ export type * from "./client";
2
2
  export type * from "./messages/index";
3
3
  export type * from "./accounts/index";
4
4
  export type * from "./business/index";
5
+ export type * from "./templates/index";
6
+ export type * from "./webhooks/index";
5
7
  export type * from "./debug";
@@ -0,0 +1,33 @@
1
+ import { z } from "zod";
2
+ import {
3
+ buttonSchema,
4
+ componentSchema,
5
+ quickReplyButtonSchema,
6
+ urlButtonSchema,
7
+ phoneNumberButtonSchema,
8
+ copyCodeButtonSchema,
9
+ flowButtonSchema,
10
+ headerComponentSchema,
11
+ bodyComponentSchema,
12
+ footerComponentSchema,
13
+ buttonsComponentSchema,
14
+ } from "../../schemas/templates/component";
15
+
16
+ /**
17
+ * Button types
18
+ */
19
+ export type QuickReplyButton = z.infer<typeof quickReplyButtonSchema>;
20
+ export type UrlButton = z.infer<typeof urlButtonSchema>;
21
+ export type PhoneNumberButton = z.infer<typeof phoneNumberButtonSchema>;
22
+ export type CopyCodeButton = z.infer<typeof copyCodeButtonSchema>;
23
+ export type FlowButton = z.infer<typeof flowButtonSchema>;
24
+ export type Button = z.infer<typeof buttonSchema>;
25
+
26
+ /**
27
+ * Component types
28
+ */
29
+ export type HeaderComponent = z.infer<typeof headerComponentSchema>;
30
+ export type BodyComponent = z.infer<typeof bodyComponentSchema>;
31
+ export type FooterComponent = z.infer<typeof footerComponentSchema>;
32
+ export type ButtonsComponent = z.infer<typeof buttonsComponentSchema>;
33
+ export type Component = z.infer<typeof componentSchema>;
@@ -0,0 +1,4 @@
1
+ export * from "./component";
2
+ export * from "./request";
3
+ export * from "./response";
4
+
@@ -0,0 +1,28 @@
1
+ import { z } from "zod";
2
+ import {
3
+ createTemplateRequestSchema,
4
+ updateTemplateRequestSchema,
5
+ listTemplatesRequestSchema,
6
+ deleteTemplateRequestSchema,
7
+ } from "../../schemas/templates/request";
8
+
9
+ /**
10
+ * Type for creating a template
11
+ */
12
+ export type CreateTemplateRequest = z.infer<typeof createTemplateRequestSchema>;
13
+
14
+ /**
15
+ * Type for updating a template
16
+ */
17
+ export type UpdateTemplateRequest = z.infer<typeof updateTemplateRequestSchema>;
18
+
19
+ /**
20
+ * Type for listing templates
21
+ */
22
+ export type ListTemplatesRequest = z.infer<typeof listTemplatesRequestSchema>;
23
+
24
+ /**
25
+ * Type for deleting a template
26
+ */
27
+ export type DeleteTemplateRequest = z.infer<typeof deleteTemplateRequestSchema>;
28
+
@@ -0,0 +1,34 @@
1
+ import { z } from "zod";
2
+ import {
3
+ templateResponseSchema,
4
+ createTemplateResponseSchema,
5
+ listTemplatesResponseSchema,
6
+ updateTemplateResponseSchema,
7
+ deleteTemplateResponseSchema,
8
+ } from "../../schemas/templates/response";
9
+
10
+ /**
11
+ * Type for a single template
12
+ */
13
+ export type TemplateResponse = z.infer<typeof templateResponseSchema>;
14
+
15
+ /**
16
+ * Type for create template response
17
+ */
18
+ export type CreateTemplateResponse = z.infer<typeof createTemplateResponseSchema>;
19
+
20
+ /**
21
+ * Type for list templates response
22
+ */
23
+ export type ListTemplatesResponse = z.infer<typeof listTemplatesResponseSchema>;
24
+
25
+ /**
26
+ * Type for update template response
27
+ */
28
+ export type UpdateTemplateResponse = z.infer<typeof updateTemplateResponseSchema>;
29
+
30
+ /**
31
+ * Type for delete template response
32
+ */
33
+ export type DeleteTemplateResponse = z.infer<typeof deleteTemplateResponseSchema>;
34
+
@@ -0,0 +1,16 @@
1
+ import { z } from "zod";
2
+ import {
3
+ incomingTextMessageSchema,
4
+ incomingMessageSchema,
5
+ } from "../../schemas/webhooks/incoming-message";
6
+
7
+ /**
8
+ * Type for incoming text message
9
+ */
10
+ export type IncomingTextMessage = z.infer<typeof incomingTextMessageSchema>;
11
+
12
+ /**
13
+ * Union type for all incoming message types
14
+ */
15
+ export type IncomingMessage = z.infer<typeof incomingMessageSchema>;
16
+
@@ -0,0 +1,3 @@
1
+ export * from "./incoming-message";
2
+ export * from "./payload";
3
+
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+ import { webhookPayloadSchema } from "../../schemas/webhooks/payload";
3
+
4
+ /**
5
+ * Type for webhook payload
6
+ */
7
+ export type WebhookPayload = z.infer<typeof webhookPayloadSchema>;
8
+
package/tsconfig.json CHANGED
@@ -12,10 +12,9 @@
12
12
  "module": "esnext",
13
13
  "moduleResolution": "bundler",
14
14
  "target": "esnext",
15
- "types": [],
16
15
  // For nodejs:
17
- // "lib": ["esnext"],
18
- // "types": ["node"],
16
+ "lib": ["esnext"],
17
+ "types": ["node"],
19
18
  // and npm install -D @types/node
20
19
 
21
20
  // Other Outputs