onebots 0.0.21 → 0.0.23
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/README.md +1 -2
- package/lib/service/V12/config.d.ts +0 -8
- package/lib/service/V12/index.d.ts +6 -0
- package/lib/service/V12/index.js +79 -38
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -5,10 +5,9 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/onebots)
|
|
6
6
|
[](https://onebot.dev/)
|
|
7
7
|
[](https://12.onebot.dev/)
|
|
8
|
-
[](https://nodejs.org)
|
|
9
9
|
[](https://jq.qq.com/?_wv=1027&k=B22VGXov)
|
|
10
10
|
|
|
11
|
-
[](https://star-history.com/#lc-cn/onebots&Date)
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
[Type Docs](https://lc-cn.github.io/onebots)
|
|
@@ -6,12 +6,4 @@ export declare namespace Config {
|
|
|
6
6
|
event_enabled?: boolean;
|
|
7
7
|
event_buffer_size?: number;
|
|
8
8
|
}
|
|
9
|
-
type WebhookConfig = string | ({
|
|
10
|
-
url: string;
|
|
11
|
-
timeout?: number;
|
|
12
|
-
} & AuthInfo);
|
|
13
|
-
type WsReverseConfig = string | ({
|
|
14
|
-
url: string;
|
|
15
|
-
reconnect_interval?: number;
|
|
16
|
-
} & AuthInfo);
|
|
17
9
|
}
|
|
@@ -26,6 +26,7 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
|
|
|
26
26
|
start(path?: string): void;
|
|
27
27
|
private startHttp;
|
|
28
28
|
private startWebhook;
|
|
29
|
+
private runActions;
|
|
29
30
|
private startWs;
|
|
30
31
|
private startWsReverse;
|
|
31
32
|
stop(force?: boolean): Promise<void>;
|
|
@@ -113,6 +114,11 @@ export declare namespace V12 {
|
|
|
113
114
|
}
|
|
114
115
|
interface WebhookConfig extends Config.AuthInfo {
|
|
115
116
|
url: string;
|
|
117
|
+
timeout?: number;
|
|
118
|
+
get_latest_actions?: boolean | string | {
|
|
119
|
+
path?: string;
|
|
120
|
+
interval: number;
|
|
121
|
+
};
|
|
116
122
|
}
|
|
117
123
|
interface WsConfig extends Config.AuthInfo {
|
|
118
124
|
}
|
package/lib/service/V12/index.js
CHANGED
|
@@ -36,22 +36,6 @@ class V12 extends events_1.EventEmitter {
|
|
|
36
36
|
return this.db.get('eventBuffer');
|
|
37
37
|
}
|
|
38
38
|
start(path) {
|
|
39
|
-
this.client.on('system.online', async () => {
|
|
40
|
-
this.action.sendPrivateMsg.apply(this, [1659488338, [
|
|
41
|
-
{
|
|
42
|
-
type: 'reply',
|
|
43
|
-
data: {
|
|
44
|
-
message_id: 'YunIUgAAD0N494J8Y7u1awE='
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
type: 'text',
|
|
49
|
-
data: {
|
|
50
|
-
text: '测试文本'
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
]]);
|
|
54
|
-
});
|
|
55
39
|
this.path = `/${this.client.uin}`;
|
|
56
40
|
if (path)
|
|
57
41
|
this.path += path;
|
|
@@ -60,7 +44,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
60
44
|
this.startHttp({
|
|
61
45
|
access_token: this.config.access_token,
|
|
62
46
|
event_enabled: true,
|
|
63
|
-
event_buffer_size:
|
|
47
|
+
event_buffer_size: 10,
|
|
64
48
|
...config
|
|
65
49
|
});
|
|
66
50
|
}
|
|
@@ -114,7 +98,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
114
98
|
this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`), (ctx) => this._httpRequestHandler(ctx, config));
|
|
115
99
|
this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
116
100
|
this.on('dispatch', (payload) => {
|
|
117
|
-
if (!['message', 'notice', 'request', '
|
|
101
|
+
if (!['message', 'notice', 'request', 'meta'].includes(payload.type))
|
|
118
102
|
return;
|
|
119
103
|
if (config.event_enabled) {
|
|
120
104
|
this.history.push(payload);
|
|
@@ -124,28 +108,37 @@ class V12 extends events_1.EventEmitter {
|
|
|
124
108
|
});
|
|
125
109
|
}
|
|
126
110
|
startWebhook(config) {
|
|
111
|
+
const options = {
|
|
112
|
+
method: "POST",
|
|
113
|
+
timeout: config.timeout || this.config.request_timeout,
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
|
|
117
|
+
"X-OneBot-Version": 12,
|
|
118
|
+
"X-Impl": "oicq_onebot",
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
if (config.access_token) {
|
|
122
|
+
options.headers['Authorization'] = `Bearer ${config.access_token}`;
|
|
123
|
+
}
|
|
124
|
+
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
127
125
|
this.on('dispatch', (unserialized) => {
|
|
128
|
-
const serialized = JSON.stringify(unserialized);
|
|
129
|
-
const options = {
|
|
130
|
-
method: "POST",
|
|
131
|
-
timeout: this.config.request_timeout * 1000,
|
|
132
|
-
headers: {
|
|
133
|
-
"Content-Type": "application/json",
|
|
134
|
-
"Content-Length": Buffer.byteLength(serialized),
|
|
135
|
-
"X-Self-ID": String(this.client.uin),
|
|
136
|
-
"User-Agent": "OneBot",
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
140
126
|
try {
|
|
141
|
-
|
|
127
|
+
const serialized = JSON.stringify(unserialized);
|
|
128
|
+
protocol.request(config.url, {
|
|
129
|
+
...options,
|
|
130
|
+
headers: {
|
|
131
|
+
...options.headers,
|
|
132
|
+
"Content-Length": Buffer.byteLength(serialized),
|
|
133
|
+
}
|
|
134
|
+
}, (res) => {
|
|
142
135
|
if (res.statusCode !== 200)
|
|
143
|
-
return this.logger.warn(`
|
|
136
|
+
return this.logger.warn(`Webhook(${config.url})上报事件收到非200响应:` + res.statusCode);
|
|
144
137
|
let data = "";
|
|
145
138
|
res.setEncoding("utf-8");
|
|
146
139
|
res.on("data", (chunk) => data += chunk);
|
|
147
140
|
res.on("end", () => {
|
|
148
|
-
this.logger.debug(`收到
|
|
141
|
+
this.logger.debug(`收到Webhook响应 ${res.statusCode} :` + data);
|
|
149
142
|
if (!data)
|
|
150
143
|
return;
|
|
151
144
|
try {
|
|
@@ -156,15 +149,63 @@ class V12 extends events_1.EventEmitter {
|
|
|
156
149
|
}
|
|
157
150
|
});
|
|
158
151
|
}).on("error", (err) => {
|
|
159
|
-
this.logger.error(`
|
|
152
|
+
this.logger.error(`Webhook(${config.url})上报事件失败:` + err.message);
|
|
160
153
|
}).end(serialized, () => {
|
|
161
|
-
this.logger.debug(`
|
|
154
|
+
this.logger.debug(`Webhook(${config.url})上报事件成功: ` + serialized);
|
|
162
155
|
});
|
|
163
156
|
}
|
|
164
157
|
catch (e) {
|
|
165
|
-
this.logger.error(`
|
|
158
|
+
this.logger.error(`Webhook(${config.url})上报失败:` + e.message);
|
|
166
159
|
}
|
|
167
160
|
});
|
|
161
|
+
if (config.get_latest_actions) {
|
|
162
|
+
const interval = (config.get_latest_actions && typeof config.get_latest_actions === 'object') ?
|
|
163
|
+
config.get_latest_actions.interval * 1000 :
|
|
164
|
+
1000 * 60;
|
|
165
|
+
setInterval(() => {
|
|
166
|
+
try {
|
|
167
|
+
const actionPath = typeof config.get_latest_actions === 'string' ?
|
|
168
|
+
config.get_latest_actions :
|
|
169
|
+
typeof config.get_latest_actions === 'boolean' ?
|
|
170
|
+
'get_latest_actions' :
|
|
171
|
+
config.get_latest_actions.path || '/get_latest_actions';
|
|
172
|
+
protocol.request(`${config.url}${actionPath}`, {
|
|
173
|
+
...options,
|
|
174
|
+
method: 'GET',
|
|
175
|
+
headers: {
|
|
176
|
+
...options.headers,
|
|
177
|
+
}
|
|
178
|
+
}, (res) => {
|
|
179
|
+
if (res.statusCode !== 200)
|
|
180
|
+
return this.logger.warn(`Webhook(${config.url})获取动作队列收到非200响应:` + res.statusCode);
|
|
181
|
+
let data = "";
|
|
182
|
+
res.setEncoding("utf-8");
|
|
183
|
+
res.on("data", (chunk) => data += chunk);
|
|
184
|
+
res.on("end", () => {
|
|
185
|
+
this.logger.info(`获取动作队列响应 ${res.statusCode} :` + data);
|
|
186
|
+
if (!data)
|
|
187
|
+
return;
|
|
188
|
+
try {
|
|
189
|
+
this.runActions(JSON.parse(data));
|
|
190
|
+
}
|
|
191
|
+
catch (e) {
|
|
192
|
+
this.logger.error(`执行动作报错:` + e.message);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}).on("error", (err) => {
|
|
196
|
+
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + err.message);
|
|
197
|
+
}).end();
|
|
198
|
+
}
|
|
199
|
+
catch (e) {
|
|
200
|
+
this.logger.error(`Webhook(${config.url})获取动作队列失败:` + e.message);
|
|
201
|
+
}
|
|
202
|
+
}, interval);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
runActions(actions) {
|
|
206
|
+
for (const action of actions) {
|
|
207
|
+
this.apply(action);
|
|
208
|
+
}
|
|
168
209
|
}
|
|
169
210
|
startWs(config) {
|
|
170
211
|
this.wss = this.oneBot.app.router.ws(this.path, this.oneBot.app.httpServer);
|
|
@@ -186,7 +227,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
186
227
|
if (token)
|
|
187
228
|
req.headers["authorization"] = `Bearer ${token}`;
|
|
188
229
|
if (!req.headers["authorization"] || req.headers["authorization"] !== `Bearer ${config.access_token}`)
|
|
189
|
-
return ws.close(
|
|
230
|
+
return ws.close(401, "wrong access token");
|
|
190
231
|
}
|
|
191
232
|
this._webSocketHandler(ws);
|
|
192
233
|
});
|
|
@@ -432,7 +473,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
432
473
|
const headers = {
|
|
433
474
|
"X-Self-ID": String(this.client.uin),
|
|
434
475
|
"X-Client-Role": "Universal",
|
|
435
|
-
"User-Agent": "OneBot/12 (qq)
|
|
476
|
+
"User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
|
|
436
477
|
"Sec-WebSocket-Protocol": "12.onebots.v0.0.15"
|
|
437
478
|
};
|
|
438
479
|
if (config.access_token)
|