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.
- package/CHANGELOG.md +13 -0
- package/agent_docs/INCOMING_MESSAGES_BRAINSTORM.md +500 -0
- package/cloud-api-docs/webhooks/endpoint.md +112 -0
- package/cloud-api-docs/webhooks/overview.md +154 -0
- package/docs/DEVELOPMENT.md +154 -0
- package/docs/webhooks.md +76 -0
- package/package.json +6 -2
- package/src/client/HttpClient.ts +43 -6
- package/src/client/WhatsAppClient.ts +6 -0
- package/src/examples/main.ts +9 -0
- package/src/examples/template.ts +134 -0
- package/src/index.ts +7 -0
- package/src/schemas/client.ts +2 -2
- package/src/schemas/index.ts +2 -0
- package/src/schemas/templates/component.ts +145 -0
- package/src/schemas/templates/index.ts +4 -0
- package/src/schemas/templates/request.ts +78 -0
- package/src/schemas/templates/response.ts +64 -0
- package/src/schemas/webhooks/incoming-message.ts +38 -0
- package/src/schemas/webhooks/index.ts +3 -0
- package/src/schemas/webhooks/payload.ts +56 -0
- package/src/services/accounts/AccountsClient.ts +6 -14
- package/src/services/accounts/AccountsService.ts +19 -21
- package/src/services/accounts/methods/list-phone-numbers.ts +1 -2
- package/src/services/business/BusinessClient.ts +1 -9
- package/src/services/business/BusinessService.ts +19 -21
- package/src/services/business/methods/list-accounts.ts +1 -2
- package/src/services/messages/MessagesClient.ts +2 -6
- package/src/services/messages/MessagesService.ts +42 -22
- package/src/services/templates/TemplatesClient.ts +35 -0
- package/src/services/templates/TemplatesService.ts +117 -0
- package/src/services/templates/index.ts +3 -0
- package/src/services/templates/methods/create.ts +27 -0
- package/src/services/templates/methods/delete.ts +38 -0
- package/src/services/templates/methods/get.ts +23 -0
- package/src/services/templates/methods/list.ts +36 -0
- package/src/services/templates/methods/update.ts +35 -0
- package/src/services/webhooks/WebhooksService.ts +214 -0
- package/src/services/webhooks/index.ts +3 -0
- package/src/services/webhooks/utils/extract-messages.ts +25 -0
- package/src/services/webhooks/utils/extract-statuses.ts +25 -0
- package/src/services/webhooks/utils/verify.ts +29 -0
- package/src/types/index.ts +2 -0
- package/src/types/templates/component.ts +33 -0
- package/src/types/templates/index.ts +4 -0
- package/src/types/templates/request.ts +28 -0
- package/src/types/templates/response.ts +34 -0
- package/src/types/webhooks/incoming-message.ts +16 -0
- package/src/types/webhooks/index.ts +3 -0
- package/src/types/webhooks/payload.ts +8 -0
- 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
|
+
}
|
package/src/types/index.ts
CHANGED
|
@@ -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,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
|
+
|
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
|
-
|
|
18
|
-
|
|
16
|
+
"lib": ["esnext"],
|
|
17
|
+
"types": ["node"],
|
|
19
18
|
// and npm install -D @types/node
|
|
20
19
|
|
|
21
20
|
// Other Outputs
|