-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat.js
76 lines (74 loc) · 2.15 KB
/
wechat.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
const fs = require('fs');
const Wechat = require('wechat4u');
const qrcode = require('qrcode-terminal');
const consts = require('./consts').wechat;
let sendQueue = [];
let bot;
try {
module.exports.bot = bot = new Wechat(require(consts.sessionFile));
} catch(e) {
module.exports.bot = bot = new Wechat();
}
if(bot.PROP.uin) {
bot.restart();
setTimeout(() => {
if(!module.exports.ready) {
bot.start();
}
}, consts.loginTimeout);
} else {
bot.start();
}
bot.on("uuid", uuid => {
qrcode.generate(`https://login.weixin.qq.com/l/${uuid}`, {
small: true
});
console.log(`QRCode Link: https://login.weixin.qq.com/qrcode/${uuid}`);
});
bot.on("user-avatar", _ => {
console.log("Please confirm login request.");
});
bot.on("login", () => {
console.log("WeChat Logged in.");
logEvent({id:"[[Server]]"}, "wechat:login", "WeChat Logged in");
fs.writeFileSync(consts.sessionFile, JSON.stringify(bot.botData));
module.exports.ready = true;
sendQueue.forEach(msg => {
send(msg);
});
sendQueue = [];
});
bot.on("error", e => {
logEvent({id:"[[Server]]"}, "wechat:error", e ? e.stack || e : "No error provided");
});
bot.on("message", msg => {
//bot.sendMsg(msg.FromUserName, msg.FromUserName).catch(_ => {});
});
bot.on("message", msg => {
if (msg.MsgType == bot.CONF.MSGTYPE_VERIFYMSG && msg.RecommendInfo.Content === consts.verifyMsg) {
bot.verifyUser(msg.RecommendInfo.UserName, msg.RecommendInfo.Ticket)
.then(_ => {
logEvent({id:"[[Server]]"}, "wechat:verify",
`Verified ${bot.Contact.getDisplayName(msg.RecommendInfo)}`);
console.log(msg.RecommendInfo);
}).catch(_ => {});
}
});
module.exports.send = function send(config) {
const {user = "filehelper", msg = "未知消息"} = config;
if(!module.exports.ready) {
sendQueue.push(config);
return;
}
const contacts = [];
for(const contact in bot.contacts) {
contacts.push(bot.contacts[contact]);
}
contacts
.filter(contact => contact.RemarkName.indexOf(user) > -1)
.map(contact => contact.UserName)
.forEach(username => {
bot.sendMsg(msg, username).catch(_ => {});
});
}
const {logEvent} = require('./utils');