onebots 0.0.22 → 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 -22
- 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
|
@@ -44,7 +44,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
44
44
|
this.startHttp({
|
|
45
45
|
access_token: this.config.access_token,
|
|
46
46
|
event_enabled: true,
|
|
47
|
-
event_buffer_size:
|
|
47
|
+
event_buffer_size: 10,
|
|
48
48
|
...config
|
|
49
49
|
});
|
|
50
50
|
}
|
|
@@ -98,7 +98,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
98
98
|
this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`), (ctx) => this._httpRequestHandler(ctx, config));
|
|
99
99
|
this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
|
|
100
100
|
this.on('dispatch', (payload) => {
|
|
101
|
-
if (!['message', 'notice', 'request', '
|
|
101
|
+
if (!['message', 'notice', 'request', 'meta'].includes(payload.type))
|
|
102
102
|
return;
|
|
103
103
|
if (config.event_enabled) {
|
|
104
104
|
this.history.push(payload);
|
|
@@ -108,28 +108,37 @@ class V12 extends events_1.EventEmitter {
|
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
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;
|
|
111
125
|
this.on('dispatch', (unserialized) => {
|
|
112
|
-
const serialized = JSON.stringify(unserialized);
|
|
113
|
-
const options = {
|
|
114
|
-
method: "POST",
|
|
115
|
-
timeout: this.config.request_timeout * 1000,
|
|
116
|
-
headers: {
|
|
117
|
-
"Content-Type": "application/json",
|
|
118
|
-
"Content-Length": Buffer.byteLength(serialized),
|
|
119
|
-
"X-Self-ID": String(this.client.uin),
|
|
120
|
-
"User-Agent": "OneBot",
|
|
121
|
-
},
|
|
122
|
-
};
|
|
123
|
-
const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
|
|
124
126
|
try {
|
|
125
|
-
|
|
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) => {
|
|
126
135
|
if (res.statusCode !== 200)
|
|
127
|
-
return this.logger.warn(`
|
|
136
|
+
return this.logger.warn(`Webhook(${config.url})上报事件收到非200响应:` + res.statusCode);
|
|
128
137
|
let data = "";
|
|
129
138
|
res.setEncoding("utf-8");
|
|
130
139
|
res.on("data", (chunk) => data += chunk);
|
|
131
140
|
res.on("end", () => {
|
|
132
|
-
this.logger.debug(`收到
|
|
141
|
+
this.logger.debug(`收到Webhook响应 ${res.statusCode} :` + data);
|
|
133
142
|
if (!data)
|
|
134
143
|
return;
|
|
135
144
|
try {
|
|
@@ -140,15 +149,63 @@ class V12 extends events_1.EventEmitter {
|
|
|
140
149
|
}
|
|
141
150
|
});
|
|
142
151
|
}).on("error", (err) => {
|
|
143
|
-
this.logger.error(`
|
|
152
|
+
this.logger.error(`Webhook(${config.url})上报事件失败:` + err.message);
|
|
144
153
|
}).end(serialized, () => {
|
|
145
|
-
this.logger.debug(`
|
|
154
|
+
this.logger.debug(`Webhook(${config.url})上报事件成功: ` + serialized);
|
|
146
155
|
});
|
|
147
156
|
}
|
|
148
157
|
catch (e) {
|
|
149
|
-
this.logger.error(`
|
|
158
|
+
this.logger.error(`Webhook(${config.url})上报失败:` + e.message);
|
|
150
159
|
}
|
|
151
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
|
+
}
|
|
152
209
|
}
|
|
153
210
|
startWs(config) {
|
|
154
211
|
this.wss = this.oneBot.app.router.ws(this.path, this.oneBot.app.httpServer);
|
|
@@ -170,7 +227,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
170
227
|
if (token)
|
|
171
228
|
req.headers["authorization"] = `Bearer ${token}`;
|
|
172
229
|
if (!req.headers["authorization"] || req.headers["authorization"] !== `Bearer ${config.access_token}`)
|
|
173
|
-
return ws.close(
|
|
230
|
+
return ws.close(401, "wrong access token");
|
|
174
231
|
}
|
|
175
232
|
this._webSocketHandler(ws);
|
|
176
233
|
});
|
|
@@ -416,7 +473,7 @@ class V12 extends events_1.EventEmitter {
|
|
|
416
473
|
const headers = {
|
|
417
474
|
"X-Self-ID": String(this.client.uin),
|
|
418
475
|
"X-Client-Role": "Universal",
|
|
419
|
-
"User-Agent": "OneBot/12 (qq)
|
|
476
|
+
"User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
|
|
420
477
|
"Sec-WebSocket-Protocol": "12.onebots.v0.0.15"
|
|
421
478
|
};
|
|
422
479
|
if (config.access_token)
|