-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdingtalk.js
349 lines (275 loc) · 8.82 KB
/
dingtalk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* dingtalk adapter
*/
const crypto = require("crypto");
const Adapter = require.main.require("hubot/src/adapter");
const { TextMessage } = require.main.require("hubot/src/message");
const User = require.main.require("hubot/src/user");
const Robot = require("dingtalk-robot-sdk")
const path = require('path')
const fs = require('fs')
const Text = Robot.Text;
const authDict = ["token", "sign"];
const dict = {
MODE: {
Both: 1,
Single: 2,
Group: 3,
}
};
const modeDict = [dict.MODE.Both, dict.MODE.Single, dict.MODE.Group];
class Dingtalk extends Adapter {
constructor(robot, options) {
super(robot);
this.token = options.token;
this.secret = options.secret;
this.authType = options.authType || authDict[0];
this.mode = options.mode ? options.mode * 1 : dict.MODE.Both;
this.blackList = options.blackList ? options.blackList.split(",") : [];
this.whiteList = options.whiteList ? options.whiteList.split(",") : [];
// 钉钉发送消息地址,20分钟有效期
// todo cache?
this.sessionWebhook = null;
this.webhook = null;
this.robot.logger.info("Constructor");
}
getWebHookUrl(accessToken) {
return new Robot({ accessToken: accessToken, secret: this.secret }).getWebHook();
}
request(data, envelope, cb) {
let webHookUrl = this.sessionWebhook;
// 只有指定room时使用webhook,outgoing返回消息采用sessionWebhook
if (envelope.room) {
const accessToken = this.robot.brain.get(envelope.room);
if (accessToken) {
webHookUrl = this.getWebHookUrl(accessToken);
} else {
this.robot.logger.error(`dingtalk send to room ${envelope.room} failed: accessToken not found`);
return;
}
}
// todo 直接调用robot.send时缺少room,可能会使用sessionWebhook导致错误发送
if (webHookUrl) {
this.robot
.http(webHookUrl)
.header("Content-Type", "application/json")
.post(JSON.stringify(data.get()))((err, resp, body) => {
if (body) {
try {
const result = JSON.parse(body);
if (result.errmsg !== 'ok') {
this.robot.logger.error("response failed:" + result.errmsg);
}
} catch (e) {
this.robot.logger.error("response failed:" + e.errmsg);
}
} else {
this.robot.logger.error("request failed:" + err);
}
cb && cb();
});
} else {
this.robot.logger.error("missing webhook");
}
}
//for room
send(envelope, ...strings) {
this.robot.logger.info("Send");
if (strings.length === 0) {
return;
}
const string = strings.shift();
const text = new Text();
text.setContent(string);
if (envelope.user && envelope.user.id) {
text.atId(envelope.user.id);
}
this.robot.logger.debug(`dingtalk send to ${envelope.room || ""} message: ${text}`);
this.request(text, envelope, () => {
this.send.apply(this, [envelope].concat(strings));
});
}
// for user
reply(envelope, ...strings) {
this.robot.logger.info("reply");
this.send.apply(
this,
[envelope].concat(strings.map(str => `${envelope.user.name}: ${str}`))
);
}
referrerCheck(request) {
if (this.authType === "token") {
const requestToken = request.get("token");
return requestToken === this.token;
} else {
const timestamp = request.get("timestamp");
const sign = request.get("sign");
const hash = crypto
.createHmac("sha256", this.secret)
.update(timestamp + "\n" + this.secret)
.digest("base64");
return hash === sign;
}
}
isRobotSupportMode(messageData) {
const { conversationType } = messageData;
// 判断消息模式
if (this.mode === 1) {
return conversationType === "1" || conversationType === "2"
} else if (this.mode === 2) {
return conversationType === "1"
} else if (this.mode === 3) {
return conversationType === "2"
}
return false;
}
isMessageChannelDisabled(messageData) {
const { conversationId, } = messageData;
// 判断会话权限
// 优先使用黑名单,为空时再使用白名单
if (this.blackList && this.blackList.length) {
if (this.blackList.indexOf(conversationId) >= 0) {
this.robot.logger.info(
`${conversationId} is blocked`
);
return true;
}
} else if (this.whiteList && this.whiteList.length) {
if (this.whiteList.indexOf(conversationId) >= 0) {
return false;
}
this.robot.logger.info(
`${conversationId} is blocked`
);
return true;
}
return false;
}
listen() {
this.robot.router.get("/hubot/dingtalk/message/", (request, response) => {
response.send("<h1>Hubot Dingtalk</h1><p>请使用POST方式请求!</p>");
})
this.robot.router.post("/hubot/dingtalk/message/", (request, response) => {
let data = {};
if (request.body.payload) {
JSON.parse((data = request.body.payload));
} else {
data = request.body;
}
if (this.referrerCheck(request)) {
this.robot.logger.info(
`dingtalk receive data conversationId: ${data.conversationId}`
);
this.robot.logger.debug(
`${JSON.stringify(data)}`
);
const isRobotSupportMode = this.isRobotSupportMode(data);
if (!isRobotSupportMode) {
const bannedText = new Text();
bannedText.setContent(`机器人不支持${data.conversationType === "1" ? '单聊' : '群聊'}!`);
if (data.senderId && data.conversationType === "2") {
bannedText.atId(data.senderId);
}
response.send(
JSON.stringify(bannedText.get())
);
return;
}
const isMessageChannelDisabled = this.isMessageChannelDisabled(data);
if (isMessageChannelDisabled) {
const bannedText = new Text();
bannedText.setContent("机器人尚未对该会话启用!");
if (data.senderId && data.conversationType === "2") {
bannedText.atId(data.senderId);
}
response.send(
JSON.stringify(bannedText.get())
);
return;
}
this.sessionWebhook = data.sessionWebhook;
this.receiveMessageFromUrl(
data.text.content,
data.createAt,
data.senderId,
data.senderNick
);
response.send(
JSON.stringify({
msgtype: "empty"
})
);
} else {
response.send("who are you!!!");
}
});
}
// 针对钉钉talk特性,给message添加机器人名称才能出发hubot的respond
addPrefixMessage(message) {
if (!message.match(this.robot.respondPattern(""))) {
return `${this.robot.name}: ${message}`;
}
return message;
}
read(base) {
const configFilePath = path.resolve(base || ".", "conf", "dingtalk-room.json");
if (fs.existsSync(configFilePath)) {
try {
const data = fs.readFileSync(configFilePath);
return JSON.parse(data);
} catch (e) {
this.robot.logger.error("Read file: " + configFilePath + " failed");
this.robot.logger.error(e.message);
}
} else {
this.robot.logger.info("Definition file not found:" + configFilePath);
}
return [];
}
receiveMessageFromUrl(msg, msgId, senderId, username) {
this.robot.logger.info(msg);
const user = new User(senderId, { name: username });
this.receive(new TextMessage(user, this.addPrefixMessage(msg), msgId));
}
run() {
this.robot.logger.info("Run");
if (authDict.indexOf(this.authType) === -1) {
this.robot.logger.error("Allowed auth type is token or sign!");
return false;
}
if (modeDict.indexOf(this.mode) === -1) {
this.robot.logger.error("Valid mode is one of 1,2,3!");
return false;
}
if (!this.token && !this.secret) {
this.robot.logger.error("No token or secret is provided to dingtalk!");
} else {
this.listen();
}
// start incoming webhook
const conf = this.read();
conf.forEach((config) => {
if (config.room && config.env && process.env[config.env]) {
this.robot.brain.set(config.room, process.env[config.env])
}
})
this.emit("connected");
}
}
// 钉钉自定义机器人签名
const token = process.env.HUBOT_DINGTALK_TOKEN;
const secret = process.env.HUBOT_DINGTALK_SECRET;
const authType = process.env.HUBOT_DINGTALK_AUTH_TYPE;
const mode = process.env.HUBOT_DINGTALK_MODE;
const blackList = process.env.HUBOT_DINGTALK_BLACKLIST;
const whiteList = process.env.HUBOT_DINGTALK_WHITELIST;
exports.use = robot => {
return new Dingtalk(robot, {
token,
secret,
authType,
mode,
blackList,
whiteList
})
};