-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount-bot.js
355 lines (306 loc) · 11.7 KB
/
count-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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const {
Wechaty,
Room,
Message,
} = require('wechaty')
const finis = require('finis')
const sd = require('silly-datetime')
const fs = require('fs')
const https = require('https');
const SqliteDB = require('./sqlite.js').SqliteDB;
const ejsexcel = require("ejsexcel");
const qr = require('qr-image');
const util = require("util");
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);
const { FileBox } = require('file-box')
const databasefile = "workcount.db";
const sqliteDB = new SqliteDB(databasefile);
const bot = new Wechaty()
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
const createWorkTotalTableSql = "create table if not exists worktotal(workid INTEGER, userid TEXT, username TEXT, totalnum INTEGER, curnum INTEGER, reporttime TEXT, reportstr TEXT, verify TEXT);";
const createWorkDetailTableSql = "create table if not exists workdetail(workid INTEGER, userid TEXT, username TEXT, totalnum INTEGER, curnum INTEGER, reporttime TEXT, reportstr TEXT, verify TEXT);";
sqliteDB.createTable(createWorkTotalTableSql);
sqliteDB.createTable(createWorkDetailTableSql);
bot.start()
.then(() => console.log('Starter Bot Started.'))
.catch(e => console.error(e))
function onScan (qrcode, status) {
require('qrcode-terminal').generate(qrcode, { small: true }) // show qrcode on console
console.log(qrcode + 'status:' + status)
const qr_png = qr.image(qrcode, { type: 'png' });
qr_png.pipe(fs.createWriteStream('wechat.png')); //登录二维码保存为图片,后面可以通过web服务器,扫码图片登录
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log(qrcodeImageUrl)
const ftqqUrl = [
'https://sc.ftqq.com/selfid.send?text=',
encodeURIComponent('<a href=https://api.qrserver.com/v1/create-qr-code/?data='+qrcode+'>二维码</a><a href=http://*/wechat.png>二维码图片</a>'),
].join('')
if (status == 1) {
const curdate=new Date()
if (curdate.getHours() > 6 && curdate.getHours() < 23) {
//https://sc.ftqq.com/3.version
//每天7点到22点期间,通过方糖发送登录提醒
https.get(ftqqUrl, (res) => {
console.log(`Got response: ${res.statusCode}`);
// consume response body
res.resume();
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
}
}
}
function onLogin (user) {
console.log(`${user} login`)
}
function onLogout(user) {
console.log(`${user} logout`)
}
async function onMessage (msg) {
const room = msg.room()
const from = msg.from()
const text = msg.text()
if (!from) {
return
}
const fromname = from.name()
if (msg.type() === Message.Type.Text) {
if(room) {
roomtopic = await room.topic()
if (roomtopic.indexOf('消息测试') > -1) {
if (text.length < 100)
workreport(roomtopic, fromname, text, 1, false)
} else if (roomtopic.indexOf('小程序') == 0) {
if (text == 'get111') {
exportexcel(1, msg)
} else if (text == 'check111') {
exportdebug(1, msg)
} else {
if (text.length < 100)
workreport(roomtopic, fromname, text, 1, true)
}
}
} else {
if (fromname == 'paul') {
if (text == 'get111') {
exportexcel(1, msg)
} else if (text == 'textdebug') {
exporttextdebug(1)
} else if (text == 'check111') {
exportdebug(1, msg)
} else if (text == 'testmsg') {
selftestmsg()
} else {
if (text.length < 100)
workreport('yugs', fromname, text, 1, true)
}
}
}
} else {
if (fromname == 'paul') {
const file = await msg.toFileBox()
const name = file.name
console.log('Save file to: ' + name)
file.toFile(fromname + '/' + name)
}
}
}
function match (str, keywords) {
str = String(str)
var ks = ''
if (Array.isArray(keywords)) {
ks = keywords.map(k => '\\d+' + k).join('|') + '|'
} else if (typeof keywords === 'string') {
ks = `\\d+${keywords}|`
}
var checker = new RegExp(`${ks}\\d+|\\D+`, 'g')
return str.match(checker)
}
function addZero(num, length) {
return (Array(length).join('0') + num).slice(-length);
}
async function sendverifytoroom(verifystr) {
const room = await bot.Room.find({ topic: /^小程序/ })
if (room) {
room.say(verifystr)
} else {
console.log('room not find');
}
}
function workreport(roomtopic, fromname, text, workid, root) {
const curtime=sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
var reportstr = text.replace(/(<br[^>]*>|\s*)/g,'')
reportstr = reportstr.replace(/o/gi, '0');
var reportarr = match(reportstr)
var matchsucess = false
if (reportarr.length == 6) {
if (!isNaN(reportarr[0]) && !isNaN(reportarr[2]) && !isNaN(reportarr[4]) &&
isNaN(reportarr[1]) && isNaN(reportarr[3]) && isNaN(reportarr[5])) {
console.log(curtime + ':[' + roomtopic + ']' + '<' + fromname + '>' + ':' + text)
matchsucess = true
}
}
const curdate=sd.format(new Date(), 'YYYY-MM-DD');
if (matchsucess) {
fs.appendFileSync(roomtopic + '/' + curdate + '_sucess.txt', curtime + ' ' + text +'\r\n');
reportarr[0] = addZero(reportarr[0], 3)
var workdetail = []
workdetail.push(workid)
workdetail.push(reportarr[0])
workdetail.push(reportarr[1])
workdetail.push(reportarr[2])
workdetail.push(reportarr[4])
if (!root)
workdetail.push(curtime)
else
workdetail.push('')
workdetail.push(reportstr)
var querySql = 'select workid, userid, username, totalnum, curnum, reporttime, reportstr, verify from worktotal where workid = '+workid+' and userid ="' +reportarr[0]+ '";';
sqliteDB.queryDataMore(querySql, dataDealMore, workdetail);
} else {
if (!root && reportarr.length > 1 ) {
if (!isNaN(reportarr[0]) && isNaN(reportarr[1])) {
console.log(curtime + ':[' + roomtopic + ']' + '<' + fromname + '>' + ':' + text)
if (reportarr.length > 2 && reportarr[0].length == 3 && reportarr[1].length < 10) {
fs.appendFileSync(roomtopic + '/' + curdate + '_failed.txt', curtime + ':' + text +'\r\n');
console.log(reportarr)
sendverifytoroom('格式错误:'+ text)
}
}
}
}
}
function dataDealMore(objects, workdetail){
var verifyinfo = ''
var verifylog = 'sucess'
var curtotalnum = parseInt(workdetail[3])
var curdiarynum = parseInt(workdetail[4])
if (objects.length > 0) {
var updateSql = ''
if (workdetail[5] != '') {
if (curtotalnum == objects[0].totalnum + curdiarynum) {
workdetail.push('')
var priornum = parseInt(objects[0].curnum)
if (priornum > 0) {
workdetail[4] = String(priornum + curdiarynum)
console.log('add prior num:' + objects[0].curnum);
}
if (objects[0].verify.length > 0 && (objects[0].verify.indexOf('上次报数') == 0 || objects[0].verify.indexOf('第一次报数') == 0)) {
verifylog = '已修改:' + workdetail[6] + ' 需确认:' + objects[0].reportstr+ ' ' +objects[0].verify
verifyinfo = verifylog
}
} else {
var expectnum = objects[0].totalnum + curdiarynum
verifylog = '上次报数' + objects[0].totalnum + ',正常累计' + expectnum
workdetail[3] = objects[0].totalnum
workdetail[4] = objects[0].curnum
workdetail.push(verifylog)
verifyinfo = '需确认:'+ workdetail[6] + ' ' + verifylog
}
updateSql = 'update worktotal set username = "'+workdetail[2]+'",totalnum = '+workdetail[3]+',curnum = '+workdetail[4]+',reporttime = "'+workdetail[5]+'",reportstr = "'+workdetail[6]+'",verify = "'+workdetail[7]+'" where workid = '+workdetail[0]+' and userid ="' +workdetail[1]+ '"';
} else {
verifylog = 'root verify'
workdetail.push(workdetail[6])
updateSql = 'update worktotal set username = "'+workdetail[2]+'",totalnum = '+workdetail[3]+',curnum = '+workdetail[4]+',verify = "'+workdetail[7]+'" where workid = '+workdetail[0]+' and userid ="' +workdetail[1]+ '"';
}
sqliteDB.executeSql(updateSql);
} else {
var insertTotalSql = ''
if (workdetail[5] != '') {
if (curtotalnum == curdiarynum) {
workdetail.push('')
} else {
verifylog = '第一次报数,总数和当日不匹配'
workdetail[3] = 0
workdetail[4] = 0
workdetail.push(verifylog)
verifyinfo = '需确认:'+ workdetail[6] + ' ' + verifylog
}
insertTotalSql = "insert into worktotal(workid, userid, username, totalnum, curnum, reporttime, reportstr, verify) values(?, ?, ?, ?, ?, ?, ?, ?)";
} else {
verifylog = 'root verify'
const curtime=sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
workdetail[5] = curtime
workdetail.push('')
insertTotalSql = "insert into worktotal(workid, userid, username, totalnum, curnum, reporttime, reportstr, verify) values(?, ?, ?, ?, ?, ?, ?, ?)";
}
sqliteDB.insertData(insertTotalSql, workdetail);
}
var insertDetailSql = "insert into workdetail(workid, userid, username, totalnum, curnum, reporttime, reportstr, verify) values(?, ?, ?, ?, ?, ?, ?, ?)";
sqliteDB.insertData(insertDetailSql, workdetail);
console.log('verify result:' + verifylog);
if (verifyinfo.length > 0) {
sendverifytoroom(verifyinfo)
}
}
async function selftestmsg(){
const room = await bot.Room.find({ topic: /^小程序/ })
if (room) {
room.say('测试消息')
} else {
console.log('room not find');
}
}
function exportexcel(workid, msg){
const querySql = "select userid, username, totalnum, case curnum when 0 then '' else curnum end curnum, reporttime, reportstr,verify from worktotal where workid = "+workid+" and totalnum > 0 order by userid";
sqliteDB.queryDataMore(querySql, dataDeal, msg);
const updateSql = 'update worktotal set curnum = 0 where workid = '+workid;
sqliteDB.executeSql(updateSql);
console.log(updateSql)
}
async function dataDeal(objects, msg){
const curdate=sd.format(new Date(), 'YY.MM.DD');
const exlBuf = await readFileAsync("reportexcel/reporttemplate.xlsx");
var data = [[{"curdate":curdate}]]
data.push(objects)
const exlBuf2 = await ejsexcel.renderExcel(exlBuf, data);
const filepath = "reportexcel/" + curdate + ".xlsx"
await writeFileAsync(filepath, exlBuf2);
console.log(filepath + ".xlsx生成完成");
const fileBox = FileBox.fromFile(filepath)
await msg.say(fileBox)
console.log('REPLY: %s', fileBox.toString())
}
function exportdebug(workid, msg){
const querySql = "select userid, username, totalnum, case curnum when 0 then '' else curnum end curnum, reporttime, reportstr,verify from worktotal where workid = "+workid+" order by reporttime";
sqliteDB.queryDataMore(querySql, dataDealDebug, msg);
}
async function dataDealDebug(objects, msg){
const curdate=sd.format(new Date(), 'YY.MM.DD');
const exlBuf = await readFileAsync("reportexcel/reporttemplate.xlsx");
var data = [[{"curdate":curdate}]]
data.push(objects)
const exlBuf2 = await ejsexcel.renderExcel(exlBuf, data);
const filepath = "reportexcel/" + curdate + "_debug.xlsx"
await writeFileAsync(filepath, exlBuf2);
console.log(filepath + ".xlsx生成完成");
const fileBox = FileBox.fromFile(filepath)
await msg.say(fileBox)
console.log('REPLY: %s', fileBox.toString())
}
function exporttextdebug(workid){
const querySql = 'select userid, username, reporttime, reportstr from worktotal where workid = '+workid+' order by reporttime desc';
sqliteDB.queryData(querySql, datadebug);
}
async function datadebug(objects){
const curdate=sd.format(new Date(), 'YYYY-MM-DD');
for(var i = 0; i < objects.length; ++i){
fs.appendFileSync('debug/' + curdate + '_debug.txt', objects[i].reporttime + ' ' + objects[i].reportstr + ' ' + objects[i].userid +'\r\n');
}
console.log('datadebug finish')
}
finis((code, signal, error) => {
console.log('sqliteDB close.')
sqliteDB.close();
// await bot.stop()
bot.stop()
console.log(`Wechaty exit ${code} because of ${signal}/${error})`)
process.exit(1)
})