@nextclaw/channel-extension-dingtalk 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/dingtalk-channel.service.js +113 -0
- package/dist/services/dingtalk-channel.service.js.map +1 -0
- package/nextclaw.extension.json +32 -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 { DingTalkChannel } from "./services/dingtalk-channel.service.js";
|
|
2
|
+
import { startBusChannelExtension, warnNcpEventError } from "@nextclaw/extension-sdk";
|
|
3
|
+
//#region src/main.ts
|
|
4
|
+
await startBusChannelExtension({
|
|
5
|
+
channelId: "dingtalk",
|
|
6
|
+
createChannel: ({ config, bus }) => new DingTalkChannel(config, bus),
|
|
7
|
+
onChannelStartError: warnNcpEventError("dingtalk")
|
|
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 { DingTalkChannel } from \"./services/dingtalk-channel.service.js\";\n\nawait startBusChannelExtension<Config[\"channels\"][\"dingtalk\"], MessageBus>({\n channelId: \"dingtalk\",\n createChannel: ({ config, bus }) => new DingTalkChannel(config, bus),\n onChannelStartError: warnNcpEventError(\"dingtalk\"),\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,113 @@
|
|
|
1
|
+
import { APP_TITLE, BaseChannel } from "@nextclaw/core";
|
|
2
|
+
import { DWClient, EventAck, TOPIC_ROBOT } from "dingtalk-stream";
|
|
3
|
+
import { fetch } from "undici";
|
|
4
|
+
//#region src/services/dingtalk-channel.service.ts
|
|
5
|
+
var DingTalkChannel = class extends BaseChannel {
|
|
6
|
+
name = "dingtalk";
|
|
7
|
+
client = null;
|
|
8
|
+
accessToken = null;
|
|
9
|
+
tokenExpiry = 0;
|
|
10
|
+
constructor(config, bus) {
|
|
11
|
+
super(config, bus);
|
|
12
|
+
}
|
|
13
|
+
start = async () => {
|
|
14
|
+
this.running = true;
|
|
15
|
+
if (!this.config.clientId || !this.config.clientSecret) throw new Error("DingTalk clientId/clientSecret not configured");
|
|
16
|
+
this.client = new DWClient({
|
|
17
|
+
clientId: this.config.clientId,
|
|
18
|
+
clientSecret: this.config.clientSecret,
|
|
19
|
+
debug: false
|
|
20
|
+
});
|
|
21
|
+
this.client.registerCallbackListener(TOPIC_ROBOT, async (res) => {
|
|
22
|
+
await this.handleRobotMessage(res);
|
|
23
|
+
});
|
|
24
|
+
this.client.registerAllEventListener(() => ({ status: EventAck.SUCCESS }));
|
|
25
|
+
await this.client.connect();
|
|
26
|
+
};
|
|
27
|
+
stop = async () => {
|
|
28
|
+
this.running = false;
|
|
29
|
+
if (this.client) {
|
|
30
|
+
this.client.disconnect();
|
|
31
|
+
this.client = null;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
send = async (msg) => {
|
|
35
|
+
const token = await this.getAccessToken();
|
|
36
|
+
if (!token) return;
|
|
37
|
+
const url = "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend";
|
|
38
|
+
const payload = {
|
|
39
|
+
robotCode: this.config.clientId,
|
|
40
|
+
userIds: [msg.chatId],
|
|
41
|
+
msgKey: "sampleMarkdown",
|
|
42
|
+
msgParam: JSON.stringify({
|
|
43
|
+
text: msg.content,
|
|
44
|
+
title: `${APP_TITLE} Reply`
|
|
45
|
+
})
|
|
46
|
+
};
|
|
47
|
+
const response = await fetch(url, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: {
|
|
50
|
+
"content-type": "application/json",
|
|
51
|
+
"x-acs-dingtalk-access-token": token
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify(payload)
|
|
54
|
+
});
|
|
55
|
+
if (!response.ok) throw new Error(`DingTalk send failed: ${response.status}`);
|
|
56
|
+
};
|
|
57
|
+
handleRobotMessage = async (res) => {
|
|
58
|
+
if (!res?.data) return;
|
|
59
|
+
let parsed;
|
|
60
|
+
try {
|
|
61
|
+
parsed = JSON.parse(res.data);
|
|
62
|
+
} catch {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const text = parsed.text?.content?.trim() ?? "";
|
|
66
|
+
if (!text) {
|
|
67
|
+
this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const senderId = parsed.senderStaffId || parsed.senderId || "";
|
|
71
|
+
const senderName = parsed.senderNick || "";
|
|
72
|
+
if (!senderId) {
|
|
73
|
+
this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
await this.handleMessage({
|
|
77
|
+
senderId,
|
|
78
|
+
chatId: senderId,
|
|
79
|
+
content: text,
|
|
80
|
+
attachments: [],
|
|
81
|
+
metadata: {
|
|
82
|
+
sender_name: senderName,
|
|
83
|
+
platform: "dingtalk"
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });
|
|
87
|
+
};
|
|
88
|
+
getAccessToken = async () => {
|
|
89
|
+
if (this.accessToken && Date.now() < this.tokenExpiry) return this.accessToken;
|
|
90
|
+
const url = "https://api.dingtalk.com/v1.0/oauth2/accessToken";
|
|
91
|
+
const payload = {
|
|
92
|
+
appKey: this.config.clientId,
|
|
93
|
+
appSecret: this.config.clientSecret
|
|
94
|
+
};
|
|
95
|
+
const response = await fetch(url, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers: { "content-type": "application/json" },
|
|
98
|
+
body: JSON.stringify(payload)
|
|
99
|
+
});
|
|
100
|
+
if (!response.ok) return null;
|
|
101
|
+
const data = await response.json();
|
|
102
|
+
const token = data.accessToken;
|
|
103
|
+
const expiresIn = Number(data.expireIn ?? 7200);
|
|
104
|
+
if (!token) return null;
|
|
105
|
+
this.accessToken = token;
|
|
106
|
+
this.tokenExpiry = Date.now() + (expiresIn - 60) * 1e3;
|
|
107
|
+
return token;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
//#endregion
|
|
111
|
+
export { DingTalkChannel };
|
|
112
|
+
|
|
113
|
+
//# sourceMappingURL=dingtalk-channel.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dingtalk-channel.service.js","names":[],"sources":["../../src/services/dingtalk-channel.service.ts"],"sourcesContent":["import { APP_TITLE, BaseChannel, type Config, type MessageBus, type OutboundMessage } from \"@nextclaw/core\";\nimport { DWClient, EventAck, TOPIC_ROBOT, type DWClientDownStream } from \"dingtalk-stream\";\nimport { fetch } from \"undici\";\nexport class DingTalkChannel extends BaseChannel<Config[\"channels\"][\"dingtalk\"]> {\n name = \"dingtalk\";\n private client: DWClient | null = null;\n private accessToken: string | null = null;\n private tokenExpiry = 0;\n constructor(config: Config[\"channels\"][\"dingtalk\"], bus: MessageBus) {\n super(config, bus);\n }\n start = async (): Promise<void> => {\n this.running = true;\n if (!this.config.clientId || !this.config.clientSecret) {\n throw new Error(\"DingTalk clientId/clientSecret not configured\");\n }\n this.client = new DWClient({\n clientId: this.config.clientId,\n clientSecret: this.config.clientSecret,\n debug: false\n });\n this.client.registerCallbackListener(TOPIC_ROBOT, async (res: DWClientDownStream) => {\n await this.handleRobotMessage(res);\n });\n this.client.registerAllEventListener(() => ({ status: EventAck.SUCCESS }));\n await this.client.connect();\n };\n stop = async (): Promise<void> => {\n this.running = false;\n if (this.client) {\n this.client.disconnect();\n this.client = null;\n }\n };\n send = async (msg: OutboundMessage): Promise<void> => {\n const token = await this.getAccessToken();\n if (!token) {\n return;\n }\n const url = \"https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend\";\n const payload = {\n robotCode: this.config.clientId,\n userIds: [msg.chatId],\n msgKey: \"sampleMarkdown\",\n msgParam: JSON.stringify({\n text: msg.content,\n title: `${APP_TITLE} Reply`\n })\n };\n const response = await fetch(url, {\n method: \"POST\",\n headers: {\n \"content-type\": \"application/json\",\n \"x-acs-dingtalk-access-token\": token\n },\n body: JSON.stringify(payload)\n });\n if (!response.ok) {\n throw new Error(`DingTalk send failed: ${response.status}`);\n }\n };\n private handleRobotMessage = async (res: DWClientDownStream): Promise<void> => {\n if (!res?.data) {\n return;\n }\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(res.data) as Record<string, unknown>;\n }\n catch {\n return;\n }\n const text = (parsed.text as {\n content?: string;\n } | undefined)?.content?.trim() ?? \"\";\n if (!text) {\n this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });\n return;\n }\n const senderId = (parsed.senderStaffId as string | undefined) || (parsed.senderId as string | undefined) || \"\";\n const senderName = (parsed.senderNick as string | undefined) || \"\";\n if (!senderId) {\n this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });\n return;\n }\n await this.handleMessage({\n senderId,\n chatId: senderId,\n content: text,\n attachments: [],\n metadata: {\n sender_name: senderName,\n platform: \"dingtalk\"\n }\n });\n this.client?.socketCallBackResponse(res.headers.messageId, { ok: true });\n };\n private getAccessToken = async (): Promise<string | null> => {\n if (this.accessToken && Date.now() < this.tokenExpiry) {\n return this.accessToken;\n }\n const url = \"https://api.dingtalk.com/v1.0/oauth2/accessToken\";\n const payload = {\n appKey: this.config.clientId,\n appSecret: this.config.clientSecret\n };\n const response = await fetch(url, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n body: JSON.stringify(payload)\n });\n if (!response.ok) {\n return null;\n }\n const data = (await response.json()) as Record<string, unknown>;\n const token = data.accessToken as string | undefined;\n const expiresIn = Number(data.expireIn ?? 7200);\n if (!token) {\n return null;\n }\n this.accessToken = token;\n this.tokenExpiry = Date.now() + (expiresIn - 60) * 1000;\n return token;\n };\n}\n\n"],"mappings":";;;;AAGA,IAAa,kBAAb,cAAqC,YAA4C;CAC7E,OAAO;CACP,SAAkC;CAClC,cAAqC;CACrC,cAAsB;CACtB,YAAY,QAAwC,KAAiB;AACjE,QAAM,QAAQ,IAAI;;CAEtB,QAAQ,YAA2B;AAC/B,OAAK,UAAU;AACf,MAAI,CAAC,KAAK,OAAO,YAAY,CAAC,KAAK,OAAO,aACtC,OAAM,IAAI,MAAM,gDAAgD;AAEpE,OAAK,SAAS,IAAI,SAAS;GACvB,UAAU,KAAK,OAAO;GACtB,cAAc,KAAK,OAAO;GAC1B,OAAO;GACV,CAAC;AACF,OAAK,OAAO,yBAAyB,aAAa,OAAO,QAA4B;AACjF,SAAM,KAAK,mBAAmB,IAAI;IACpC;AACF,OAAK,OAAO,gCAAgC,EAAE,QAAQ,SAAS,SAAS,EAAE;AAC1E,QAAM,KAAK,OAAO,SAAS;;CAE/B,OAAO,YAA2B;AAC9B,OAAK,UAAU;AACf,MAAI,KAAK,QAAQ;AACb,QAAK,OAAO,YAAY;AACxB,QAAK,SAAS;;;CAGtB,OAAO,OAAO,QAAwC;EAClD,MAAM,QAAQ,MAAM,KAAK,gBAAgB;AACzC,MAAI,CAAC,MACD;EAEJ,MAAM,MAAM;EACZ,MAAM,UAAU;GACZ,WAAW,KAAK,OAAO;GACvB,SAAS,CAAC,IAAI,OAAO;GACrB,QAAQ;GACR,UAAU,KAAK,UAAU;IACrB,MAAM,IAAI;IACV,OAAO,GAAG,UAAU;IACvB,CAAC;GACL;EACD,MAAM,WAAW,MAAM,MAAM,KAAK;GAC9B,QAAQ;GACR,SAAS;IACL,gBAAgB;IAChB,+BAA+B;IAClC;GACD,MAAM,KAAK,UAAU,QAAQ;GAChC,CAAC;AACF,MAAI,CAAC,SAAS,GACV,OAAM,IAAI,MAAM,yBAAyB,SAAS,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0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "nextclaw-channel-extension-dingtalk",
|
|
3
|
+
"name": "NextClaw DingTalk 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": "dingtalk",
|
|
14
|
+
"name": "DingTalk",
|
|
15
|
+
"description": "DingTalk robot channel",
|
|
16
|
+
"configSchema": {
|
|
17
|
+
"type": "object",
|
|
18
|
+
"additionalProperties": true,
|
|
19
|
+
"properties": {
|
|
20
|
+
"enabled": { "type": "boolean" },
|
|
21
|
+
"clientId": { "type": "string" },
|
|
22
|
+
"clientSecret": { "type": "string" },
|
|
23
|
+
"allowFrom": {
|
|
24
|
+
"type": "array",
|
|
25
|
+
"items": { "type": "string" }
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nextclaw/channel-extension-dingtalk",
|
|
3
|
+
"version": "0.1.1-beta.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "NextClaw DingTalk 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
|
+
"dingtalk-stream": "^2.1.4",
|
|
21
|
+
"undici": "^6.21.0",
|
|
22
|
+
"@nextclaw/core": "0.12.25-beta.0",
|
|
23
|
+
"@nextclaw/extension-sdk": "0.1.12-beta.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.17.6",
|
|
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
|
+
}
|