-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (160 loc) · 4.62 KB
/
index.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
// index.js
/* eslint-disable no-undef */
import {bot} from './util/init.js';
import {sendMessage} from './util/util.js'
import { config } from 'dotenv';
import express from 'express';
import {log} from './db/logs.js'
import { singleChat,groupChat,handleFile, handleImage,handleAudio,handleVideo,
handleMiniProgram,
handleGIF,handleTransfer,handlePush } from './util/handle.js';
import router from './router/router.js';
import staticRouter from './router/static.js';
import {saveWechatConfig} from './db/wechat.js';
import {saveWechatFriends,saveWechatRooms} from './util/wechat.js';
// import {Jimp} from 'jimp';
import bodyParser from 'body-parser';
import {transporter,mailOptions} from './util/mailer.js';
const port = 4000;
config();
const app = express();
app.use(bodyParser.json({'limit':'800kb'}));
app.use(express.urlencoded({'extended':false,'limit':'800kb'}));
app.use(router);
app.use(staticRouter);
export async function prepareBot() {
bot.on("message", async (message) => {
const contact = message.talker();
if (contact.self()) {
return;
}
let {payload} = message;
let {talkerId,listenerId,text} = payload;
if (message.room()) {
groupChat(message)
return;
}
switch (message.type()) {
case 0:
await sendMessage(talkerId, "感谢老板抬爱,祝老板在缅A发财")
break;
case 1:
await handleFile(message,talkerId)
break;
case 2:
await handleAudio(message,talkerId)
break;
case 5:
await handleGIF(message,talkerId)
break;
case 7:
await singleChat(talkerId,listenerId,text)
break;
case 6:
await handleImage(message,talkerId)
break;
case 11:
{ const xmlstr = payload.text;
handleTransfer(xmlstr,talkerId)
break;
}
case 14:
{
handleArticle(payload.text,talkerId)
break;
}
case 9:
await handleMiniProgram(message,talkerId)
break;
case 15:
await handleVideo(message,talkerId)
break;
default:
break;
}
});
bot.on("friendship", async (friendship) => {
try {
const contact = friendship.contact();
switch (friendship.type()) {
case bot.Friendship.Type.Receive:
await friendship.accept()
await contact.say(process.env.WELCOME_TEXT)
break
}
} catch (e) {
console.error(e)
}
});
bot.on('scan', (qrcode) => {
saveWechatConfig({loginurl:qrcode,wechatid:'',username:'',avatar:'',friends:''})
})
bot.on("login", async (user) => {
const selfContact = bot.currentUser;
let dataUrl = ""
try {
const selfAvatarFileBox = await selfContact.avatar();
if (selfAvatarFileBox.buffer) {
const buffer = await selfAvatarFileBox.toBuffer();
const base64 = buffer.toString('base64');
dataUrl = `data:image/jpeg;base64,${base64}`;
}
} catch (e) {
log('error', "获取头像失败"+e);
}
saveWechatConfig({username:user.payload.name,wechatid:user.payload.id,avatar:dataUrl,loginurl:'',friends:''})
log('info', "机器人登录成功,账号名:"+user.payload.name);
})
bot.on('logout', async (user)=>{
log('info', user.payload.name+"退出登录");
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
})
bot.on("error", () => {
// let text = e
// if(`${e}`.includes("AssertionError: 400 != 400")){
// text = "获取微信二维码超时5分钟"
// }
// if(`${e}`.includes("1101 == 0")){
// text = "微信退出登录"
// }
// log('error', text);
})
bot.on("ready",async ()=>{
let roomList = await bot.Room.findAll()
const contactList = await bot.Contact.findAll();
if(roomList && contactList){
await saveWechatRooms(roomList)
await saveWechatFriends(contactList)
await handlePush(roomList)
}
})
await bot.start();
await bot.ready();
return bot;
}
async function startBot() {
app.listen(port, async () => {
console.log(`Web server listening at http://localhost:${port}`);
// await open(`http://localhost:${port}`);
});
await prepareBot();
}
startBot().catch((e)=>{
console.log("Bot Catch",e);
});
process.on('exit', async(code) => {
log('warning', "程序退出"+code);
});
process.on('SIGINT', async () => {
log('warning', "程序退出");
await bot.logout();
process.exit();
});
process.on('uncaughtException', async (err) => {
console.error('抛出异常退出:', err);
});