@nextclaw/channel-extension-whatsapp 0.1.1-beta.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/LICENSE +21 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +12 -0
- package/dist/main.js.map +1 -0
- package/dist/services/whatsapp-channel.service.js +102 -0
- package/dist/services/whatsapp-channel.service.js.map +1 -0
- package/nextclaw.extension.json +31 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NextClaw contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { WhatsAppChannel } from "./services/whatsapp-channel.service.js";
|
|
2
|
+
import { startBusChannelExtension, warnNcpEventError } from "@nextclaw/extension-sdk";
|
|
3
|
+
//#region src/main.ts
|
|
4
|
+
await startBusChannelExtension({
|
|
5
|
+
channelId: "whatsapp",
|
|
6
|
+
createChannel: ({ config, bus }) => new WhatsAppChannel(config, bus),
|
|
7
|
+
onChannelStartError: warnNcpEventError("whatsapp")
|
|
8
|
+
});
|
|
9
|
+
//#endregion
|
|
10
|
+
export {};
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","names":[],"sources":["../src/main.ts"],"sourcesContent":["import type { Config, MessageBus } from \"@nextclaw/core\";\nimport { startBusChannelExtension, warnNcpEventError } from \"@nextclaw/extension-sdk\";\nimport { WhatsAppChannel } from \"./services/whatsapp-channel.service.js\";\n\nawait startBusChannelExtension<Config[\"channels\"][\"whatsapp\"], MessageBus>({\n channelId: \"whatsapp\",\n createChannel: ({ config, bus }) => new WhatsAppChannel(config, bus),\n onChannelStartError: warnNcpEventError(\"whatsapp\"),\n});\n"],"mappings":";;;AAIA,MAAM,yBAAqE;CACzE,WAAW;CACX,gBAAgB,EAAE,QAAQ,UAAU,IAAI,gBAAgB,QAAQ,IAAI;CACpE,qBAAqB,kBAAkB,WAAW;CACnD,CAAC"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { BaseChannel } from "@nextclaw/core";
|
|
2
|
+
import WebSocket from "ws";
|
|
3
|
+
//#region src/services/whatsapp-channel.service.ts
|
|
4
|
+
var WhatsAppChannel = class extends BaseChannel {
|
|
5
|
+
name = "whatsapp";
|
|
6
|
+
ws = null;
|
|
7
|
+
connected = false;
|
|
8
|
+
constructor(config, bus) {
|
|
9
|
+
super(config, bus);
|
|
10
|
+
}
|
|
11
|
+
start = async () => {
|
|
12
|
+
this.running = true;
|
|
13
|
+
const bridgeUrl = this.config.bridgeUrl;
|
|
14
|
+
while (this.running) try {
|
|
15
|
+
await new Promise((resolve, reject) => {
|
|
16
|
+
const ws = new WebSocket(bridgeUrl);
|
|
17
|
+
this.ws = ws;
|
|
18
|
+
ws.on("open", () => {
|
|
19
|
+
this.connected = true;
|
|
20
|
+
});
|
|
21
|
+
ws.on("message", (data) => {
|
|
22
|
+
const payload = data.toString();
|
|
23
|
+
this.handleBridgeMessage(payload);
|
|
24
|
+
});
|
|
25
|
+
ws.on("close", () => {
|
|
26
|
+
this.connected = false;
|
|
27
|
+
this.ws = null;
|
|
28
|
+
resolve();
|
|
29
|
+
});
|
|
30
|
+
ws.on("error", (_err) => {
|
|
31
|
+
this.connected = false;
|
|
32
|
+
this.ws = null;
|
|
33
|
+
reject(_err);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
} catch {
|
|
37
|
+
if (!this.running) break;
|
|
38
|
+
await sleep(5e3);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
stop = async () => {
|
|
42
|
+
this.running = false;
|
|
43
|
+
this.connected = false;
|
|
44
|
+
if (this.ws) {
|
|
45
|
+
this.ws.close();
|
|
46
|
+
this.ws = null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
send = async (msg) => {
|
|
50
|
+
if (!this.ws || !this.connected) return;
|
|
51
|
+
const payload = {
|
|
52
|
+
type: "send",
|
|
53
|
+
to: msg.chatId,
|
|
54
|
+
text: msg.content
|
|
55
|
+
};
|
|
56
|
+
this.ws.send(JSON.stringify(payload));
|
|
57
|
+
};
|
|
58
|
+
handleBridgeMessage = async (raw) => {
|
|
59
|
+
let data;
|
|
60
|
+
try {
|
|
61
|
+
data = JSON.parse(raw);
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const msgType = data.type;
|
|
66
|
+
if (msgType === "message") {
|
|
67
|
+
const pn = data.pn ?? "";
|
|
68
|
+
const sender = data.sender ?? "";
|
|
69
|
+
let content = data.content ?? "";
|
|
70
|
+
const userId = pn || sender;
|
|
71
|
+
const senderId = userId.includes("@") ? userId.split("@")[0] : userId;
|
|
72
|
+
if (content === "[Voice Message]") content = "[Voice Message: Transcription not available for WhatsApp yet]";
|
|
73
|
+
await this.handleMessage({
|
|
74
|
+
senderId,
|
|
75
|
+
chatId: sender || userId,
|
|
76
|
+
content,
|
|
77
|
+
attachments: [],
|
|
78
|
+
metadata: {
|
|
79
|
+
message_id: data.id,
|
|
80
|
+
timestamp: data.timestamp,
|
|
81
|
+
is_group: Boolean(data.isGroup)
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (msgType === "status") {
|
|
87
|
+
const status = data.status;
|
|
88
|
+
if (status === "connected") this.connected = true;
|
|
89
|
+
else if (status === "disconnected") this.connected = false;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (msgType === "qr") return;
|
|
93
|
+
if (msgType === "error") return;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
function sleep(ms) {
|
|
97
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
export { WhatsAppChannel };
|
|
101
|
+
|
|
102
|
+
//# sourceMappingURL=whatsapp-channel.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whatsapp-channel.service.js","names":[],"sources":["../../src/services/whatsapp-channel.service.ts"],"sourcesContent":["import { BaseChannel, type Config, type MessageBus, type OutboundMessage } from \"@nextclaw/core\";\nimport WebSocket from \"ws\";\nexport class WhatsAppChannel extends BaseChannel<Config[\"channels\"][\"whatsapp\"]> {\n name = \"whatsapp\";\n private ws: WebSocket | null = null;\n private connected = false;\n constructor(config: Config[\"channels\"][\"whatsapp\"], bus: MessageBus) {\n super(config, bus);\n }\n start = async (): Promise<void> => {\n this.running = true;\n const bridgeUrl = this.config.bridgeUrl;\n while (this.running) {\n try {\n await new Promise<void>((resolve, reject) => {\n const ws = new WebSocket(bridgeUrl);\n this.ws = ws;\n ws.on(\"open\", () => {\n this.connected = true;\n });\n ws.on(\"message\", (data: WebSocket.RawData) => {\n const payload = data.toString();\n void this.handleBridgeMessage(payload);\n });\n ws.on(\"close\", () => {\n this.connected = false;\n this.ws = null;\n resolve();\n });\n ws.on(\"error\", (_err: Error) => {\n this.connected = false;\n this.ws = null;\n reject(_err);\n });\n });\n }\n catch {\n if (!this.running) {\n break;\n }\n await sleep(5000);\n }\n }\n };\n stop = async (): Promise<void> => {\n this.running = false;\n this.connected = false;\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n }\n };\n send = async (msg: OutboundMessage): Promise<void> => {\n if (!this.ws || !this.connected) {\n return;\n }\n const payload = {\n type: \"send\",\n to: msg.chatId,\n text: msg.content\n };\n this.ws.send(JSON.stringify(payload));\n };\n private handleBridgeMessage = async (raw: string): Promise<void> => {\n let data: Record<string, unknown>;\n try {\n data = JSON.parse(raw) as Record<string, unknown>;\n }\n catch {\n return;\n }\n const msgType = data.type as string | undefined;\n if (msgType === \"message\") {\n const pn = (data.pn as string | undefined) ?? \"\";\n const sender = (data.sender as string | undefined) ?? \"\";\n let content = (data.content as string | undefined) ?? \"\";\n const userId = pn || sender;\n const senderId = userId.includes(\"@\") ? userId.split(\"@\")[0] : userId;\n if (content === \"[Voice Message]\") {\n content = \"[Voice Message: Transcription not available for WhatsApp yet]\";\n }\n await this.handleMessage({\n senderId,\n chatId: sender || userId,\n content,\n attachments: [],\n metadata: {\n message_id: data.id,\n timestamp: data.timestamp,\n is_group: Boolean(data.isGroup)\n }\n });\n return;\n }\n if (msgType === \"status\") {\n const status = data.status as string | undefined;\n if (status === \"connected\") {\n this.connected = true;\n }\n else if (status === \"disconnected\") {\n this.connected = false;\n }\n return;\n }\n if (msgType === \"qr\") {\n return;\n }\n if (msgType === \"error\") {\n return;\n }\n };\n}\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n"],"mappings":";;;AAEA,IAAa,kBAAb,cAAqC,YAA4C;CAC7E,OAAO;CACP,KAA+B;CAC/B,YAAoB;CACpB,YAAY,QAAwC,KAAiB;AACjE,QAAM,QAAQ,IAAI;;CAEtB,QAAQ,YAA2B;AAC/B,OAAK,UAAU;EACf,MAAM,YAAY,KAAK,OAAO;AAC9B,SAAO,KAAK,QACR,KAAI;AACA,SAAM,IAAI,SAAe,SAAS,WAAW;IACzC,MAAM,KAAK,IAAI,UAAU,UAAU;AACnC,SAAK,KAAK;AACV,OAAG,GAAG,cAAc;AAChB,UAAK,YAAY;MACnB;AACF,OAAG,GAAG,YAAY,SAA4B;KAC1C,MAAM,UAAU,KAAK,UAAU;AAC1B,UAAK,oBAAoB,QAAQ;MACxC;AACF,OAAG,GAAG,eAAe;AACjB,UAAK,YAAY;AACjB,UAAK,KAAK;AACV,cAAS;MACX;AACF,OAAG,GAAG,UAAU,SAAgB;AAC5B,UAAK,YAAY;AACjB,UAAK,KAAK;AACV,YAAO,KAAK;MACd;KACJ;UAEA;AACF,OAAI,CAAC,KAAK,QACN;AAEJ,SAAM,MAAM,IAAK;;;CAI7B,OAAO,YAA2B;AAC9B,OAAK,UAAU;AACf,OAAK,YAAY;AACjB,MAAI,KAAK,IAAI;AACT,QAAK,GAAG,OAAO;AACf,QAAK,KAAK;;;CAGlB,OAAO,OAAO,QAAwC;AAClD,MAAI,CAAC,KAAK,MAAM,CAAC,KAAK,UAClB;EAEJ,MAAM,UAAU;GACZ,MAAM;GACN,IAAI,IAAI;GACR,MAAM,IAAI;GACb;AACD,OAAK,GAAG,KAAK,KAAK,UAAU,QAAQ,CAAC;;CAEzC,sBAA8B,OAAO,QAA+B;EAChE,IAAI;AACJ,MAAI;AACA,UAAO,KAAK,MAAM,IAAI;UAEpB;AACF;;EAEJ,MAAM,UAAU,KAAK;AACrB,MAAI,YAAY,WAAW;GACvB,MAAM,KAAM,KAAK,MAA6B;GAC9C,MAAM,SAAU,KAAK,UAAiC;GACtD,IAAI,UAAW,KAAK,WAAkC;GACtD,MAAM,SAAS,MAAM;GACrB,MAAM,WAAW,OAAO,SAAS,IAAI,GAAG,OAAO,MAAM,IAAI,CAAC,KAAK;AAC/D,OAAI,YAAY,kBACZ,WAAU;AAEd,SAAM,KAAK,cAAc;IACrB;IACA,QAAQ,UAAU;IAClB;IACA,aAAa,EAAE;IACf,UAAU;KACN,YAAY,KAAK;KACjB,WAAW,KAAK;KAChB,UAAU,QAAQ,KAAK,QAAQ;KAClC;IACJ,CAAC;AACF;;AAEJ,MAAI,YAAY,UAAU;GACtB,MAAM,SAAS,KAAK;AACpB,OAAI,WAAW,YACX,MAAK,YAAY;YAEZ,WAAW,eAChB,MAAK,YAAY;AAErB;;AAEJ,MAAI,YAAY,KACZ;AAEJ,MAAI,YAAY,QACZ;;;AAIZ,SAAS,MAAM,IAA2B;AACtC,QAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "nextclaw-channel-extension-whatsapp",
|
|
3
|
+
"name": "NextClaw WhatsApp Channel Extension",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"server": {
|
|
6
|
+
"type": "stdio",
|
|
7
|
+
"command": "node",
|
|
8
|
+
"args": ["dist/main.js"]
|
|
9
|
+
},
|
|
10
|
+
"contributes": {
|
|
11
|
+
"channels": [
|
|
12
|
+
{
|
|
13
|
+
"id": "whatsapp",
|
|
14
|
+
"name": "WhatsApp",
|
|
15
|
+
"description": "WhatsApp bridge channel",
|
|
16
|
+
"configSchema": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"additionalProperties": true,
|
|
19
|
+
"properties": {
|
|
20
|
+
"enabled": { "type": "boolean" },
|
|
21
|
+
"bridgeUrl": { "type": "string" },
|
|
22
|
+
"allowFrom": {
|
|
23
|
+
"type": "array",
|
|
24
|
+
"items": { "type": "string" }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nextclaw/channel-extension-whatsapp",
|
|
3
|
+
"version": "0.1.1-beta.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "NextClaw WhatsApp channel extension process.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"development": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"nextclaw.extension.json"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"ws": "^8.18.0",
|
|
21
|
+
"@nextclaw/core": "0.12.25-beta.0",
|
|
22
|
+
"@nextclaw/extension-sdk": "0.1.12-beta.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^20.17.6",
|
|
26
|
+
"@types/ws": "^8.5.14",
|
|
27
|
+
"tsx": "^4.19.2",
|
|
28
|
+
"typescript": "^5.6.3"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsdown src/index.ts src/main.ts --dts.sourcemap --clean --target es2022 --no-fixedExtension --unbundle",
|
|
32
|
+
"lint": "eslint src --max-warnings=0",
|
|
33
|
+
"tsc": "tsc -p tsconfig.json"
|
|
34
|
+
}
|
|
35
|
+
}
|