-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
69 lines (66 loc) · 2 KB
/
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
var WechatyBuilder = require('wechaty').WechatyBuilder;
const wechaty = WechatyBuilder.build() // get a Wechaty instance
var nodemailer = require('nodemailer');
var qrTerminal = require('qrcode-terminal');
const settings = require('./settings');
var transporter = nodemailer.createTransport({
host: settings.host,
port: settings.port,
secure: true,
auth: {
user: settings.emailAddress,
pass: settings.emailPassword,
},
});
wechaty
.on('scan', (qrcode, status) => {
console.log("Scan QR Code to login: ");
qrTerminal.generate(qrcode, { small: true })
})
.on('login', user => console.log(`User ${user} logged in`))
.on('message', async message => {
if(message.text() === "") return;
var room = "Direct Message";
if(message.room()) {
room = await message.room().topic();
}
var eMessage = {
from: "wangchongjun@hanvos-kent.com",
to: "wangchongjun@hanvos-kent.com",
subject: `Chat Message in ${room} by ${message.talker().name()}`,
text: message.text(),
};
const fileTypeList = [
wechaty.Message.Type.Attachment,
wechaty.Message.Type.Video,
]
if (fileTypeList.includes(message.type())) {
const fileBox = await message.toFileBox()
eMessage.subject = `File in ${room} by ${message.talker().name()}`
eMessage.text = ``
eMessage.attachments = [{
filename: fileBox._name,
content: fileBox.stream,
}]
}
const imageTypeList = [
wechaty.Message.Type.Image,
]
if (imageTypeList.includes(message.type())) {
const fileBox = await message.toFileBox()
eMessage.subject = `Image in ${room} by ${message.talker().name()}`
eMessage.text = ``
eMessage.attachments = [{
filename: "image.jpeg",
content: fileBox.stream,
}]
}
transporter.sendMail(eMessage, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
})
wechaty.start()