-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveFile.ts
118 lines (96 loc) · 2.87 KB
/
saveFile.ts
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
const QrcodeTerminal = require('qrcode-terminal')
var nodemailer = require('nodemailer');
// import { inspect } from 'util'
import { createWriteStream, writeFileSync } from 'fs'
import {
Config,
Message,
MsgType,
Wechaty,
} from '../'
const bot = Wechaty.instance({ profile: Config.DEFAULT_PROFILE })
var flag1 = 0
var toEmail = ''
bot
.on('scan', (url, code) => {
if (!/201|200/.test(String(code))) {
const loginUrl = url.replace(/\/qrcode\//, '/l/')
QrcodeTerminal.generate(loginUrl)
}
console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
.on('login', user => {
console.log(`${user.name()} logined`)
bot.say('请输入您要绑定的邮箱~');
})
.on('message', m => {
console.log(`RECV: ${m}`)
// console.log(inspect(m))
saveRawObj(m.rawObj)
var reg=/^\d{5,12}@([q]{2}|[Q]{2})\.com$/;
var isok = reg.test(`${m}`);
if (isok) {
flag1 = 1
toEmail = `${m}`
bot.say('绑定成功,您收到的文件将以邮件形式发送到您的邮箱~');
}
if( flag1 === 1 ) {
if ( m.type() === MsgType.IMAGE
|| m.type() === MsgType.EMOTICON
|| m.type() === MsgType.VIDEO
|| m.type() === MsgType.VOICE
|| m.type() === MsgType.MICROVIDEO
|| m.type() === MsgType.APP
|| (m.type() === MsgType.TEXT && m.typeSub() === MsgType.LOCATION) // LOCATION
) {
saveMediaFile(m)
}
}
})
.init()
.catch(e => console.error('bot.init() error: ' + e))
function saveMediaFile(message: Message) {
const filename2 = message.filename()
const fromer = message.from()
console.log('IMAGE local filename: ' + filename2)
const fileStream = createWriteStream(filename2)
console.log('start to readyStream()')
message.readyStream()
.then(stream => {
stream.pipe(fileStream)
.on('close', () => {
console.log('finish readyStream()')
})
})
.catch(e => console.log('stream error:' + e))
var transporter = nodemailer.createTransport({
service: 'qq',
auth: {
user: '[email protected]',
pass: 'fblewvrhepfwhedj' //授权码,通过QQ获取
}
});
var mailOptions = {
from: '[email protected]', // 发送者
to: toEmail, // 接受者,可以同时发送多个,以逗号隔开
subject: fromer+'发来'+filename2, // 标题
//text: 'Hello world', // 文本
html: '您好!您的微信好友'+fromer+'给您发来'+filename2,
attachments:[
{
filename : filename2,
path: filename2
}
]
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
return;
}
console.log('发送成功');
});
}
function saveRawObj(o) {
writeFileSync('rawObj.log', JSON.stringify(o, null, ' ') + '\n\n\n', { flag: 'a' })
}