@12-apps/messaging 0.0.0-stage → 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 (57) hide show
  1. package/ADOPTING.md +119 -0
  2. package/README.md +157 -2
  3. package/dist/channel-Cs_vLGl_.d.ts +293 -0
  4. package/dist/chunk-72TDAQUW.js +136 -0
  5. package/dist/chunk-72TDAQUW.js.map +1 -0
  6. package/dist/chunk-7QVYU63E.js +7 -0
  7. package/dist/chunk-7QVYU63E.js.map +1 -0
  8. package/dist/chunk-R3DKM2IV.js +42 -0
  9. package/dist/chunk-R3DKM2IV.js.map +1 -0
  10. package/dist/chunk-Y7MG2UDQ.js +36 -0
  11. package/dist/chunk-Y7MG2UDQ.js.map +1 -0
  12. package/dist/engine-DTnp2zUL.d.ts +431 -0
  13. package/dist/index.d.ts +97 -0
  14. package/dist/index.js +999 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/manifest/index.d.ts +36 -0
  17. package/dist/manifest/index.js +14 -0
  18. package/dist/manifest/index.js.map +1 -0
  19. package/dist/manifest/server.d.ts +21 -0
  20. package/dist/manifest/server.js +17 -0
  21. package/dist/manifest/server.js.map +1 -0
  22. package/dist/server/index.d.ts +78 -0
  23. package/dist/server/index.js +15 -0
  24. package/dist/server/index.js.map +1 -0
  25. package/dist/whatsapp/index.d.ts +180 -0
  26. package/dist/whatsapp/index.js +385 -0
  27. package/dist/whatsapp/index.js.map +1 -0
  28. package/package.json +90 -4
  29. package/prisma/messaging.prisma +140 -0
  30. package/prisma/migrations/20261114120000_messaging_connector/migration.sql +110 -0
  31. package/src/core/channel.ts +106 -0
  32. package/src/core/connections.ts +67 -0
  33. package/src/core/context.ts +28 -0
  34. package/src/core/engine.ts +64 -0
  35. package/src/core/errors.ts +53 -0
  36. package/src/core/hash.ts +6 -0
  37. package/src/core/inbound.ts +240 -0
  38. package/src/core/inbox.ts +139 -0
  39. package/src/core/memory-store.ts +273 -0
  40. package/src/core/ports.ts +129 -0
  41. package/src/core/prisma-store.ts +354 -0
  42. package/src/core/retention.ts +27 -0
  43. package/src/core/sender.ts +106 -0
  44. package/src/core/store.ts +217 -0
  45. package/src/core/types.ts +189 -0
  46. package/src/index.ts +54 -0
  47. package/src/manifest/index.ts +32 -0
  48. package/src/manifest/server.ts +18 -0
  49. package/src/server/index.ts +22 -0
  50. package/src/server/jobs.ts +99 -0
  51. package/src/server/webhook.ts +92 -0
  52. package/src/whatsapp/graph.ts +136 -0
  53. package/src/whatsapp/index.ts +64 -0
  54. package/src/whatsapp/opt-out.ts +26 -0
  55. package/src/whatsapp/parse.ts +200 -0
  56. package/src/whatsapp/signature.ts +52 -0
  57. package/src/whatsapp/signup.ts +115 -0
@@ -0,0 +1,115 @@
1
+ import { GraphError, graphRequest, type GraphConfig } from "./graph";
2
+
3
+ /**
4
+ * Embedded Signup, server half. The browser half (Meta's SDK popup) hands the
5
+ * host a short-lived `code`; these three calls turn it into a connection:
6
+ *
7
+ * 1. {@link exchangeSignupCode} — the code for a business token;
8
+ * 2. {@link readSignupAccounts} — which WhatsApp Business Accounts the token
9
+ * was granted, and each one's phone numbers (id + display number);
10
+ * 3. {@link subscribeAppToWaba} — subscribe the app to the chosen account's
11
+ * webhooks, so its messages reach the connector.
12
+ *
13
+ * Then the host calls `engine.connect(...)` with the phone number id, the
14
+ * display number and the token, which the engine seals. None of these
15
+ * functions logs or returns anything but what is named.
16
+ */
17
+
18
+ type Json = Record<string, unknown>;
19
+
20
+ /** The app's own identity on Meta. The secret is the host's, read where it keeps secrets. */
21
+ export interface MetaAppCredentials {
22
+ appId: string;
23
+ appSecret: string;
24
+ }
25
+
26
+ /** Exchange Embedded Signup's code for a business integration token. */
27
+ export async function exchangeSignupCode(
28
+ graph: GraphConfig,
29
+ input: MetaAppCredentials & { code: string; redirectUri?: string },
30
+ ): Promise<{ accessToken: string }> {
31
+ const json = await graphRequest(graph, {
32
+ method: "GET",
33
+ path: "oauth/access_token",
34
+ query: {
35
+ client_id: input.appId,
36
+ client_secret: input.appSecret,
37
+ code: input.code,
38
+ ...(input.redirectUri ? { redirect_uri: input.redirectUri } : {}),
39
+ },
40
+ });
41
+ if (typeof json.access_token !== "string" || json.access_token.length === 0) {
42
+ throw new GraphError(200, "NO_ACCESS_TOKEN", "Graph answered the code exchange without a token");
43
+ }
44
+ return { accessToken: json.access_token };
45
+ }
46
+
47
+ /** One phone number of a WhatsApp Business Account. */
48
+ export interface SignupPhoneNumber {
49
+ /** The phone_number_id: the connection's external account id. */
50
+ id: string;
51
+ /** What a person reads (`+55 11 98765-4321`): the connection's display address. */
52
+ displayPhoneNumber: string;
53
+ verifiedName: string | null;
54
+ }
55
+
56
+ export interface SignupAccount {
57
+ /** The WABA id: the connection's external parent id. */
58
+ wabaId: string;
59
+ phoneNumbers: SignupPhoneNumber[];
60
+ }
61
+
62
+ /**
63
+ * The WhatsApp Business Accounts the token was granted (read from the token's
64
+ * own granular scopes, with the app's token), and each one's phone numbers.
65
+ */
66
+ export async function readSignupAccounts(
67
+ graph: GraphConfig,
68
+ input: MetaAppCredentials & { accessToken: string },
69
+ ): Promise<SignupAccount[]> {
70
+ const debug = await graphRequest(graph, {
71
+ method: "GET",
72
+ path: "debug_token",
73
+ query: { input_token: input.accessToken },
74
+ token: `${input.appId}|${input.appSecret}`,
75
+ });
76
+ const data = typeof debug.data === "object" && debug.data !== null ? (debug.data as Json) : {};
77
+ const scopes = Array.isArray(data.granular_scopes) ? (data.granular_scopes as Json[]) : [];
78
+ const wabaIds = new Set(
79
+ scopes
80
+ .filter((scope) => scope.scope === "whatsapp_business_management" && Array.isArray(scope.target_ids))
81
+ .flatMap((scope) => (scope.target_ids as unknown[]).filter((id): id is string => typeof id === "string")),
82
+ );
83
+ const accounts: SignupAccount[] = [];
84
+ for (const wabaId of wabaIds) {
85
+ accounts.push({ wabaId, phoneNumbers: await readPhoneNumbers(graph, wabaId, input.accessToken) });
86
+ }
87
+ return accounts;
88
+ }
89
+
90
+ async function readPhoneNumbers(graph: GraphConfig, wabaId: string, accessToken: string): Promise<SignupPhoneNumber[]> {
91
+ const json = await graphRequest(graph, {
92
+ method: "GET",
93
+ path: `${encodeURIComponent(wabaId)}/phone_numbers`,
94
+ query: { fields: "id,display_phone_number,verified_name" },
95
+ token: accessToken,
96
+ });
97
+ const rows = Array.isArray(json.data) ? (json.data as Json[]) : [];
98
+ return rows
99
+ .filter((row) => typeof row.id === "string" && typeof row.display_phone_number === "string")
100
+ .map((row) => ({
101
+ id: row.id as string,
102
+ displayPhoneNumber: row.display_phone_number as string,
103
+ verifiedName: typeof row.verified_name === "string" ? row.verified_name : null,
104
+ }));
105
+ }
106
+
107
+ /** Subscribe the app to a WABA's webhooks. Resolves when Meta answers `success: true`. */
108
+ export async function subscribeAppToWaba(graph: GraphConfig, input: { wabaId: string; accessToken: string }): Promise<void> {
109
+ const json = await graphRequest(graph, {
110
+ method: "POST",
111
+ path: `${encodeURIComponent(input.wabaId)}/subscribed_apps`,
112
+ token: input.accessToken,
113
+ });
114
+ if (json.success !== true) throw new GraphError(200, "NOT_SUBSCRIBED", "Graph did not confirm the subscription");
115
+ }