forked from jasonwwl/wework_message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (65 loc) · 1.76 KB
/
index.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
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const wework = require("./build/Release/wework.node");
class WeWork {
constructor(config) {
const { corpid, secret, pk } = config;
this.pk = pk;
this.corpid = corpid;
wework.init(
corpid,
secret,
path.resolve(__dirname, "./lib/libWeWorkFinanceSdk_C.so")
);
}
getChatData(seq, limit = 1000, timeout = 60) {
const data = wework.getChatData(
this.corpid,
BigInt(seq),
BigInt(limit),
BigInt(timeout)
);
const parsedData = JSON.parse(data);
const { errcode, errmsg, chatdata } = parsedData;
if (errcode !== 0) {
throw new Error(`GetChatData Error: #${errcode}-${errmsg}`);
}
return chatdata;
}
getDecryptedChatData(seq, limit = 1000, timeout = 60) {
const chatdata = this.getChatData(seq, limit, timeout);
const message = [];
for (let msg of chatdata) {
const decodeMsg = this.decryptData(msg);
message.push({
...decodeMsg,
seq: msg.seq,
raw: msg,
});
}
return message;
}
decryptData(msg) {
const { encrypt_random_key, encrypt_chat_msg } = msg;
const randomKey = crypto
.privateDecrypt(
{
key: this.pk,
padding: crypto.constants.RSA_PKCS1_PADDING,
},
Buffer.from(encrypt_random_key, "base64")
)
.toString("utf-8");
const decryptData = wework.decryptData(randomKey, encrypt_chat_msg);
const decryptMsg = JSON.parse(decryptData);
decryptMsg.seq = msg.seq;
decryptMsg.raw = msg;
return decryptMsg;
}
getMediaData(id, savedFilepath) {
wework.getMediaData(this.corpid, id, savedFilepath);
return savedFilepath;
}
}
module.exports = WeWork;