-
Notifications
You must be signed in to change notification settings - Fork 38
/
bot.js
251 lines (233 loc) · 6.41 KB
/
bot.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
require("dotenv").config();
let Datastore = require("nedb-promises");
const { WechatyBuilder } = require("wechaty");
let { FileBox } = require("file-box");
let { v1: uuid } = require("uuid");
let _ = require("lodash");
let fastify = require("fastify")({
logger: true,
});
let UserDB = Datastore.create("./users.db");
let RoomDB = Datastore.create("./rooms.db");
// let { PuppetWechat4u } = require("wechaty-puppet-wechat4u");
// let { EventLogger } = require("wechaty-plugin-contrib");
let sleep = function () {
return new Promise(function (resolve) {
return setTimeout(resolve, _.random(1.2, 3.2) * 1000);
});
};
bot = WechatyBuilder.build({
name: "bot", // generate xxxx.memory-card.json and save login data for the next login
puppet: "wechaty-puppet-wechat4u",
});
// let bot = new PuppetWechat4u();
bot.on("scan", function (qrcode) {
console.log(qrcode);
if (qrcode) {
return require("qrcode-terminal").generate(qrcode, {
small: true,
});
}
})
.on("friendship", async function (friendship) {
var contact;
// 自动通过好友, 并发送拉入群提醒
await sleep();
switch (friendship.type()) {
case bot.Friendship.Type.Receive:
return await friendship.accept();
case bot.Friendship.Type.Confirm:
contact = friendship.contact();
return await sendWebhook(contact);
}
})
.on("room-join", async function (room, inviteeList, inviter) {
var i, invitee, len, results;
results = [];
for (i = 0, len = inviteeList.length; i < len; i++) {
invitee = inviteeList[i];
if (invitee.self()) {
// unless room.payload.ownerId is inviter.id
// return inviter.say "仅限群主邀请才可获得推送地址"
await room.say(
"大家好,我是推送精灵, 通过接口可以控制我发送消息到群上."
);
results.push(await sendRoomWebHook(inviter, room));
} else {
results.push(void 0);
}
}
return results;
})
.on("room-invite", async function (roomInvitation) {
return await roomInvitation.accept();
})
.on("message", async function (message) {
var text;
text = message.text();
if (text === "webhook" || text === "推送地址") {
return await sendWebhook(message.talker());
}
})
.on("error", console.error);
let sendWebhook = async function (contact) {
var _send, token, user;
user = await UserDB.findOne({
contactid: contact.id,
});
_send = async function (token) {
return await contact.say(
`发送地址: ${process.env.DOMAIN}/send/${token}?msg=xxx`
);
};
if (user) {
return await _send(user.token);
}
token = uuid();
await UserDB.insert({
contactid: contact.id,
token: token,
});
return await _send(token);
};
let sendRoomWebHook = async function (contact, room) {
var _send, r, token;
_send = async function (token) {
return await room.say(
`发送地址: ${process.env.DOMAIN}/room/${token}?msg=xxx`
);
};
r = await RoomDB.findOne({
contactid: contact.id,
roomid: room.id
});
if (r) {
return await _send(r.token);
}
token = uuid();
await RoomDB.insert({
roomid: room.id,
token: token,
contactid: contact.id,
});
return await _send(token);
};
fastify.register(require("@fastify/rate-limit"), {
max: 100,
global: false,
});
fastify.get(
"/send/:token",
{
config: {
rateLimit: {
max: 10,
keyGenerator: function (req) {
return req.params.token;
},
},
},
},
async function (request, reply) {
var contact, msg, token, user;
({ msg } = request.query);
({ token } = request.params);
user = await UserDB.findOne({
token: token,
});
if (!user) {
return {
status: false,
msg: "token not exists",
};
}
contact = bot.Contact.load(user.contactid);
contact.say(msg);
return {
status: true,
};
}
);
fastify.get(
"/room/:token",
{
config: {
rateLimit: {
max: 10,
keyGenerator: function (req) {
return req.params.token;
},
},
},
},
async function (request, reply) {
var msg, room, token;
({ msg } = request.query);
({ token } = request.params);
room = await RoomDB.findOne({
token: token,
});
if (!room) {
return {
status: false,
msg: "room token not exists",
};
}
room = bot.Room.load(room.roomid);
room.say(msg);
return {
status: true,
};
}
);
fastify.post(
"/send/:token",
{
config: {
rateLimit: {
max: 10,
keyGenerator: function (req) {
return req.params.token;
},
},
},
},
async function (request, reply) {
var contact, image, msg, token, user;
({ msg } = request.body);
({ token } = request.params);
user = await UserDB.findOne({
token: token,
});
if (!user) {
return {
status: false,
msg: "token not exists",
};
}
contact = bot.Contact.load(user.contactid);
if (typeof msg === "string") {
await contact.say(msg);
return {
status: true,
};
}
if (msg.type === "image") {
image = FileBox.fromUrl(msg.url);
await contact.say(image);
return {
status: true,
};
}
return {
status: false,
msg: "unsupported msg type",
};
}
);
let start = async function () {
await bot.start();
await fastify.listen({port : process.env.PORT || 3000});
return console.log("listen " + process.env.PORT || 3000);
};
start();