-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwechat.js
More file actions
260 lines (221 loc) · 8.72 KB
/
Copy pathwechat.js
File metadata and controls
260 lines (221 loc) · 8.72 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
const Wechat = require('wechat4u');
const qrcode = require('qrcode-terminal');
const fs = require('fs');
const CronJob = require('cron').CronJob;
let bot
let UserName
try {
bot = new Wechat(require('./sync-data.json'));
} catch (e) {
bot = new Wechat();
}
// console.log('-==================================',bot.PROP)
if (bot.PROP.uin) {
bot.restart();
} else {
bot.start();
}
bot.on('login', () => {
console.log('登录成功');
bot.getContact().then(res=>{
var members = res.MemberList
members.forEach((obj,idx)=>{
// console.log(obj)
// console.log(obj.UserName)
var HeadImgUrl = obj.HeadImgUrl
bot.getHeadImg(HeadImgUrl).then(res => {
fs.writeFileSync(`./tx2/${idx}.jpg`, res.data)
}).catch(err => {
console.log(err)
})
})
})
// console.log(bot.contacts)
UserName = bot.user.UserName
fs.writeFileSync('./sync-data.json', JSON.stringify(bot.botData));
});
let boss
bot.on('contacts-updated', contacts => {
// console.log('all contact',contacts)
// if (!boss) {
if (bot.Contact.getSearchUser('G.M').length) {
//console.log('bot contact',bot.Contact.getSearchUser('甜筒'))
boss = bot.Contact.getSearchUser('G.M')[0].UserName;
console.log('获取目标用户成功: ', boss);
// bot.sendMsg('还有五分钟就下班了', boss)
// .catch(err => {
// bot.emit('send error', err);
// });
}
});
//早上提醒
new CronJob('00 30 07 * * *', function () {
if (boss) {
bot.sendMsg('GM,早上好,今天点天气看起来很好呀,别忘了吃早餐(蜂蜜水))哦~', boss)
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
//下班打卡提醒
new CronJob('00 50 18 * * *', function () {
if (boss) {
bot.sendMsg('GM,已经下班20分钟啦,别忘了打卡哦[微笑]。', boss)
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
//生日提醒
new CronJob('00 00 00 13 08 *', function () {
if (boss) {
bot.sendMsg('GM,今天是你的生日哦,生日快乐~[蛋糕]', boss)
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
//睡觉提醒
new CronJob('00 30 23 * * *', function () {
if (boss) {
bot.sendMsg('GM,已经晚上11点半了,该休息了,请注意休息时间哦,晚安。', boss)
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
//吃饭提醒
new CronJob('00 00 12 * * *', function () {
if (boss) {
bot.sendMsg('老铁们,还有30分钟就可以吃饭了!', '@@133d82f1d01752e90c7e8de79da12a46dfa0f4fa0ccdddd95f13420332206eb3')
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
new CronJob('00 30 18 * * *', function () {
if (boss) {
bot.sendMsg('老铁们,下班啦!!!!赶紧下班回家!!!', '@@133d82f1d01752e90c7e8de79da12a46dfa0f4fa0ccdddd95f13420332206eb3')
.catch(err => {
bot.emit('send error', err);
});
}
}, null, true, 'Asia/Shanghai');
bot.on('uuid', uuid => {
qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {
small: true
})
console.log('二维码链接:', 'https://login.weixin.qq.com/qrcode/' + uuid)
})
let recallMsgUser;
let recallMsgObj = {}
bot.on('message', msg => {
recallMsgUser = bot.contacts[msg.FromUserName].getDisplayName()
console.log('收到消息:'+ JSON.stringify(msg))
switch (msg.MsgType) {
//文字类型消息
case bot.CONF.MSGTYPE_TEXT:
// console.log('【接收文本数据:】'+JSON.stringify(msg))
recallMsgObj[msg.MsgId] = {
'MsgId':msg.MsgId,
'FromUserName':msg.FromUserName,
'Content':msg.Content
}
fs.writeFileSync(`./msg.json`,JSON.stringify(recallMsgObj))
if(UserName!=msg.FromUserName){
// fs.writeFileSync(`./msg.json`,JSON.stringify(recallMsgObj))
// fs.appendFile(`./msg.json`, JSON.stringify(recallMsgObj), function () {
// console.log('追加内容完成');
// });
let userFullName = bot.contacts[msg.FromUserName].getDisplayName()
let content = msg.Content
console.log(`接收消息:${userFullName}:`+content)
}else{
console.log('不要给自己发消息')
}
break
//消息撤回
case bot.CONF.MSGTYPE_RECALLED:
// let singleMsg = recallMsgObj[msg.FromUserName]
// console.log('【接收撤回数据:】'+JSON.stringify(msg))
let msgIdStarIndex = msg.Content.indexOf('<msgid>')
let msgIdEndIndex = msg.Content.indexOf('</msgid>')
let msgId = msg.Content.substring(msgIdStarIndex+7,msgIdEndIndex)
//console.log(`[msg:${msg.Content}][msgIdStarIndex:${msgIdStarIndex}][msgIdEndIndex:${msgIdEndIndex}]`+'msgId:'+msgId)
let recallUser ;
let recallMsg = recallMsgObj[msgId].Content
if(/<msg>/.test(recallMsg)){//撤回表情
console.log('【撤回的消息】:'+JSON.stringify(msg))
bot.sendMsg({
file: fs.createReadStream(`./media/${msgId}.gif`),
filename: `${msg.MsgId}.gif`
}, msg.FromUserName)
.catch(err => {
bot.emit('error', err)
})
}else if(/@{1}/.test(recallMsg)){
console.log('群撤回@@@:',JSON.stringify(msg))
// console.log('群撤回@@@msgcontent:',JSON.stringify(recallMsg))
let maohaoIndex = recallMsg.indexOf(':')
let userString = recallMsg.substr(0,maohaoIndex)
recallMsg = recallMsg.substring(maohaoIndex+1,)
console.log('撤回人的fromusername:'+userString)
try{
recallUser =bot.contacts[userString].getDisplayName()
}catch(e){
bot.sendMsg(e, msg.FromUserName)
console.log(`用户串[${userString}]`)
}
bot.sendMsg(`[${recallUser}]撤回消息:`+recallMsg, msg.FromUserName).catch(err => {
bot.emit('发送消息错误', err)
})
console.log(`[${recallUser}]撤回消息:`+recallMsg)
}else
if(recallMsg.indexOf("\n")>-1){
console.log('群撤回nnn:',msgId)
// console.log('群撤回nnnmsgcontent:',JSON.stringify(recallMsg))
let maohaoIndex = recallMsg.indexOf(':')
let userString = recallMsg.substr(0,maohaoIndex)
recallMsg = recallMsg.substring(maohaoIndex+1,)
console.log('nnn撤回人的fromusername:'+userString)
recallUser = userString
bot.sendMsg(`[${recallUser}]撤回消息:`+recallMsg, msg.FromUserName)
console.log(`[${recallUser}]撤回消息:`+recallMsg)
}else{
console.log('个人对话撤回',msgId)
recallUser = bot.contacts[recallMsgObj[msgId].FromUserName].getDisplayName()
bot.sendMsg(`[${recallUser}]撤回消息:\n`+recallMsg, msg.FromUserName)
console.log(`[${recallUser}]撤回消息:`+recallMsg)
}
break
case bot.CONF.MSGTYPE_IMAGE:
/**
* 图片消息
*/
// console.log('【接收图片数据:】'+JSON.stringify(msg))
console.log('【图片消息】,保存到本地')
bot.getMsgImg(msg.MsgId).then(res => {
fs.writeFileSync(`./media/${msg.MsgId}.jpg`, res.data)
}).catch(err => {
bot.emit('error', err)
})
break
case bot.CONF.MSGTYPE_EMOTICON:
/**
* 表情消息
*/
console.log('【表情消息】,保存到本地:'+JSON.stringify(msg))
recallMsgObj[msg.MsgId] = {
'MsgId':msg.MsgId,
'FromUserName':msg.FromUserName,
'Content':msg.Content
}
fs.writeFileSync(`./msg.json`,JSON.stringify(recallMsgObj))
bot.getMsgImg(msg.MsgId).then(res => {
fs.writeFileSync(`./media/${msg.MsgId}.gif`, res.data)
}).catch(err => {
bot.emit('error', err)
})
break
}
})