-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
304 lines (274 loc) · 7.54 KB
/
Copy pathindex.js
File metadata and controls
304 lines (274 loc) · 7.54 KB
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
"use strict"
require("dotenv").config();
const qq = require("icqq");
const PUSH_TARGET = process.env.PUSH_TARGET;
const THREAD_ID = Number(process.env.THREAD_ID);
const MAX_RETRIES = Number(process.env.MAX_RETRIES);
const API_URL = process.env.API_URL || "https://push.meowbot.page/push";
const QQ_PLATFORM = process.env.QQ_PLATFORM;
const QQ_DATA_DIR = process.env.QQ_DATA_DIR;
const SUBSCRIBE = process.env.SUBSCRIBE;
if(!PUSH_TARGET) {
console.error("请设置PUSH_TARGET环境变量!");
process.exit(1);
}
// 处理订阅
let allPrivate = false;
let allGroup = false;
let allGroupAt = false;
/** @type Set<number> */
let privateSet = new Set();
/** @type Map<number, { at: boolean, sub?: Set<number> }> */
let groupMap = new Map();
const subs = SUBSCRIBE.split(' ');
// 屎山代码能跑就行,别动!
subs.forEach((v) => {
try {
if(v == 'Pall') {
allPrivate = true;
return;
}
if(v == 'Gall') {
allGroup = true;
return;
}
if(v == 'Gall@') {
allGroupAt = true;
return;
}
if(v[0] == 'P') {
const id = Number(v.substring(1, v.length));
privateSet.add(id);
}
if(v[0] == 'G') {
let at = false;
if(v.endsWith('@')) {
at = true;
v = v.substring(0, v.length - 1);
}
const i = v.lastIndexOf('/');
if(i == -1) {
const id = Number(v.substring(1, v.length));
groupMap.set(id, { at: at })
}
else {
const id = Number(v.substring(1, i));
const subs = v.substring(i + 1, v.length).split(',');
const subSet = new Set()
subs.forEach((x) => subSet.add(Number(x)))
groupMap.set(id, { at: at, sub: subSet })
}
}
} catch (error) {
console.error(
[
'解析失败',
v,
error
].join(' ')
)
}
});
console.log(privateSet);
console.log(groupMap);
console.log("Hello!");
/**
* @type {qq.Config}
*/
var qqconfig = {
platform: QQ_PLATFORM,
data_dir: QQ_DATA_DIR,
};
var client = qq.createClient(qqconfig);
client.on('message.private', (e) => {
if(allPrivate) pushMsg(e);
else if(privateSet.has(e.sender.user_id)) pushMsg(e);
});
client.on('message.group', (e) => {
if(allGroup) pushGroupMsg(e);
else if(allGroupAt && (e.atall || e.atme)) pushGroupMsg(e);
else if(groupMap.has(e.group_id)) {
const group = groupMap.get(e.group_id)
if(group.at) {
if(!e.atall && !e.atme) return;
}
if(group.sub == undefined) pushGroupMsg(e);
else if(group.sub.has(e.sender.user_id)) pushGroupMsg(e);
}
})
/**
*
* @param {MessageElem[]} msgs
*/
function resolveMessage(msgs) {
const text = [];
const images = [];
msgs.forEach((msg) => {
switch (msg.type) {
case 'image':
images.push(msg.url);
break;
default:
text.push(msg.text);
break;
}
})
return {
text: text.join(' '),
images
}
}
/**
* @param {qq.PrivateMessageEvent} e
*/
function pushMsg(e) {
const sender = e.friend ? e.friend.remark : e.nickname;
const { text, images } = resolveMessage(e.message);
const msg = [
'<b>',
escapeHTML(sender),
'</b>',
'\n',
escapeHTML(text)
].join('');
push(msg, images);
}
/**
* @param {qq.GroupMessageEvent} e
*/
function pushGroupMsg(e) {
const group = e.group_name;
const friend = client.pickFriend(e.sender.user_id);
const sender = e.sender.card || friend?.remark || e.sender.nickname;
const { text, images } = resolveMessage(e.message);
const msg = [
'<i>[',
escapeHTML(group),
']</i>\n',
'<b>',
escapeHTML(sender),
'</b>',
'\n',
escapeHTML(text)
].join('');
push(msg, images);
}
/**
* @param {string} text
*/
async function push(text, images = [], retries = 0) {
if(retries > 0) console.log('推送失败,重试第' + retries + '次');
try {
let body;
if(images.length == 0) {
body = {
token: PUSH_TARGET,
text: text,
html: true,
thread_id: THREAD_ID
}
}
else if(images.length == 1) {
body = {
token: PUSH_TARGET,
version: 2,
method: 'sendPhoto',
params: {
message_thread_id: THREAD_ID,
photo: images[0],
caption: text,
parse_mode: 'HTML'
}
}
}
else {
const media = [];
images.forEach((img) => {
media.push({
type: 'image',
media: img,
captoin: text,
parse_mode: 'HTML'
})
})
body = {
token: PUSH_TARGET,
version: 2,
method: 'sendMediaGroup',
params: {
message_thread_id: THREAD_ID,
media
}
}
}
console.log(JSON.stringify(body));
const res = await fetch(API_URL, {
method: 'POST',
headers: new Headers({
"Content-Type": "application/json",
}),
body: JSON.stringify(body)
});
if(res.ok) {
console.log('推送成功');
}
else {
console.error(res.statusText);
if(retries < MAX_RETRIES) push(text, images, retries + 1);
}
} catch (error) {
console.error(error);
if(retries < MAX_RETRIES) push(text, images, retries + 1);
}
}
/**
*
* @param {string} text
* @returns string
*/
function escapeHTML(text) {
return text.replace('<', '<').replace('>', '>').replace('&', '"');
}
// 下面是登录相关
client.on('system.login.slider', (e) => {
console.log('输入滑块地址获取的ticket后继续。\n滑块地址: ' + e.url)
process.stdin.once('data', (data) => {
client.submitSlider(data.toString().trim())
})
})
client.on('system.login.qrcode', (e) => {
console.log('扫码完成后回车继续: ')
process.stdin.once('data', () => {
client.login()
})
})
client.on('system.login.device', (e) => {
console.log('请选择验证方式:(1:短信验证 其他:扫码验证)')
process.stdin.once('data', (data) => {
if (data.toString().trim() === '1') {
client.sendSmsCode()
console.log('请输入手机收到的短信验证码:')
process.stdin.once('data', (res) => {
client.submitSmsCode(res.toString().trim())
})
} else {
console.log('扫码完成后回车继续:' + e.url)
process.stdin.once('data', () => {
client.login()
})
}
})
})
const account = Number(process.env.QQ_ACCOUNT);
const password = process.env.QQ_PASSWORD;
client.login(account, password);
// Ctrl+C
function exitHandler() {
if(client.isOnline) {
client.logout(false).then(() => {
console.log('退出登录~');
process.exit(0);
})
}
}
process.on('SIGINT', exitHandler)