@theokit/gateway-whatsapp 0.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.
- package/CHANGELOG.md +31 -0
- package/README.md +7 -0
- package/dist/bridge/whatsapp-web-bridge.mjs +140 -0
- package/dist/index.cjs +809 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +428 -0
- package/dist/index.d.ts +428 -0
- package/dist/index.js +769 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- @theokit/sdk@1.3.0
|
|
9
|
+
- @theokit/gateway@2.0.0
|
|
10
|
+
|
|
11
|
+
## 1.0.0
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies
|
|
16
|
+
- @theokit/sdk@1.2.0
|
|
17
|
+
- @theokit/gateway@1.0.0
|
|
18
|
+
|
|
19
|
+
## [Unreleased]
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- Initial package skeleton (Roadmap v1.4 #2).
|
|
24
|
+
- `WhatsAppAdapter` extending `BasePlatformAdapter` with multi-backend support (ADRs D303-D314).
|
|
25
|
+
- `WhatsAppCloudBackend` for Meta WhatsApp Business Cloud API (D304).
|
|
26
|
+
- `WhatsAppWebBackend` for `whatsapp-web.js` subprocess bridge (D305).
|
|
27
|
+
- `verifyWebhookSignature` + `verifyWebhookSubscription` helpers (D306, D312).
|
|
28
|
+
- `splitForWhatsApp` 4096-char message splitter (D310).
|
|
29
|
+
- `mapWhatsAppCloudError` + `mapWhatsAppWebError` per-backend error mappers.
|
|
30
|
+
- Group mention filter with digit-only normalizer (D309).
|
|
31
|
+
- Status receipts via `onStatusReceipt` callback (D307).
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# `@theokit/gateway-whatsapp`
|
|
2
|
+
|
|
3
|
+
WhatsApp platform adapter for `@theokit/gateway`. Multi-backend (Meta WhatsApp Business Cloud API + `whatsapp-web.js` subprocess bridge).
|
|
4
|
+
|
|
5
|
+
Status: **v0.1.0 pre-release**. Pre-1.0 contract per ADR D314 — breaking changes allowed within 0.x.
|
|
6
|
+
|
|
7
|
+
See `examples/whatsapp-bot/` for a full reference implementation.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* whatsapp-web.js subprocess bridge.
|
|
4
|
+
*
|
|
5
|
+
* Spawned by `WhatsAppWebBackend.connect()`. Speaks JSON-lines over stdio:
|
|
6
|
+
* - stdin ← commands (`{ cmd: "send" | "shutdown", ... }`)
|
|
7
|
+
* - stdout → events (`{ event: "ready" | "message" | "send_ack" | "status" | "error", ... }`)
|
|
8
|
+
*
|
|
9
|
+
* The user MUST have `whatsapp-web.js` installed in their app (peer dep).
|
|
10
|
+
*
|
|
11
|
+
* MUST carry `whatsapp-web-bridge` literally in argv (EC-5 cmdline guard).
|
|
12
|
+
*
|
|
13
|
+
* Usage: node whatsapp-web-bridge.mjs --tag whatsapp-web-bridge --session <id>
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { createInterface } from "node:readline";
|
|
19
|
+
|
|
20
|
+
function emit(event) {
|
|
21
|
+
process.stdout.write(`${JSON.stringify(event)}\n`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function emitError(message) {
|
|
25
|
+
emit({ event: "error", message });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let Client;
|
|
29
|
+
let LocalAuth;
|
|
30
|
+
try {
|
|
31
|
+
const mod = await import("whatsapp-web.js");
|
|
32
|
+
Client = mod.Client;
|
|
33
|
+
LocalAuth = mod.LocalAuth;
|
|
34
|
+
} catch (err) {
|
|
35
|
+
emitError(
|
|
36
|
+
`whatsapp-web.js not installed in your app. Run \`pnpm add whatsapp-web.js\` to use the web backend. (${err?.message ?? err})`,
|
|
37
|
+
);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const args = process.argv.slice(2);
|
|
42
|
+
const sessionIdIdx = args.indexOf("--session");
|
|
43
|
+
const sessionId = sessionIdIdx >= 0 ? args[sessionIdIdx + 1] : "default";
|
|
44
|
+
|
|
45
|
+
const client = new Client({
|
|
46
|
+
authStrategy: new LocalAuth({ clientId: sessionId }),
|
|
47
|
+
puppeteer: { headless: true, args: ["--no-sandbox"] },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
client.on("qr", (qr) => {
|
|
51
|
+
// QR is logged to stderr so the user can scan from terminal output.
|
|
52
|
+
process.stderr.write(`\n[whatsapp-web bridge] Scan this QR with your WhatsApp app:\n${qr}\n\n`);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
client.on("ready", () => {
|
|
56
|
+
const phone = client.info?.wid?.user ?? "unknown";
|
|
57
|
+
emit({ event: "ready", botPhone: phone });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
client.on("message", (msg) => {
|
|
61
|
+
try {
|
|
62
|
+
emit({
|
|
63
|
+
event: "message",
|
|
64
|
+
msgId: msg.id?._serialized ?? `wa-${Date.now()}`,
|
|
65
|
+
from: msg.from ?? "",
|
|
66
|
+
body: msg.body ?? "",
|
|
67
|
+
isGroup: typeof msg.from === "string" && msg.from.endsWith("@g.us"),
|
|
68
|
+
chatId: msg.from ?? "",
|
|
69
|
+
contactName: msg._data?.notifyName,
|
|
70
|
+
timestamp: (msg.timestamp ?? Math.floor(Date.now() / 1000)) * 1000,
|
|
71
|
+
});
|
|
72
|
+
} catch (err) {
|
|
73
|
+
emitError(`message handler failed: ${err?.message ?? err}`);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
client.on("message_ack", (msg, ack) => {
|
|
78
|
+
// ack 1=sent, 2=delivered, 3=read, 4=played
|
|
79
|
+
const statusMap = { 1: "sent", 2: "delivered", 3: "read" };
|
|
80
|
+
const status = statusMap[ack];
|
|
81
|
+
if (status === undefined) return;
|
|
82
|
+
emit({
|
|
83
|
+
event: "status",
|
|
84
|
+
msgId: msg.id?._serialized ?? "",
|
|
85
|
+
status,
|
|
86
|
+
recipient: msg.to ?? msg.from ?? "",
|
|
87
|
+
timestamp: Date.now(),
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
client.on("auth_failure", (msg) => {
|
|
92
|
+
emitError(`AUTHENTICATION_FAILURE: ${msg}`);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
client.on("disconnected", (reason) => {
|
|
96
|
+
emitError(`DISCONNECTED: ${reason}`);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const rl = createInterface({ input: process.stdin });
|
|
100
|
+
rl.on("line", async (line) => {
|
|
101
|
+
let cmd;
|
|
102
|
+
try {
|
|
103
|
+
cmd = JSON.parse(line);
|
|
104
|
+
} catch {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (cmd?.cmd === "shutdown") {
|
|
108
|
+
try {
|
|
109
|
+
await client.destroy();
|
|
110
|
+
} catch {
|
|
111
|
+
/* ignore */
|
|
112
|
+
}
|
|
113
|
+
process.exit(0);
|
|
114
|
+
}
|
|
115
|
+
if (cmd?.cmd === "send") {
|
|
116
|
+
const msgId = cmd.msgId;
|
|
117
|
+
try {
|
|
118
|
+
const sent = await client.sendMessage(cmd.to, cmd.text);
|
|
119
|
+
emit({
|
|
120
|
+
event: "send_ack",
|
|
121
|
+
msgId,
|
|
122
|
+
success: true,
|
|
123
|
+
wamid: sent?.id?._serialized,
|
|
124
|
+
});
|
|
125
|
+
} catch (err) {
|
|
126
|
+
emit({
|
|
127
|
+
event: "send_ack",
|
|
128
|
+
msgId,
|
|
129
|
+
success: false,
|
|
130
|
+
error: err?.message ?? String(err),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Kick off the auth/login flow.
|
|
137
|
+
client.initialize().catch((err) => {
|
|
138
|
+
emitError(`initialize failed: ${err?.message ?? err}`);
|
|
139
|
+
process.exit(1);
|
|
140
|
+
});
|