-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathmedia-file-bot.js
56 lines (42 loc) · 1.08 KB
/
media-file-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
import qrTerm from 'qrcode-terminal'
import {
Wechaty,
Message,
} from 'wechaty'
const welcome = `
=============== Powered by Wechaty ===============
-------- https://github.com/chatie/wechaty --------
I'm a bot, I can save file to local for you!
__________________________________________________
Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = WechatyBuilder.build()
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.on('error', onError)
bot.start()
.catch(console.error)
function onScan (qrcode, status) {
qrTerm.generate(qrcode, { small: true }) // show qrcode on console
}
function onLogin (user) {
console.log(`${user} login`)
}
function onLogout (user) {
console.log(`${user} logout`)
}
function onError (e) {
console.error(e)
}
async function onMessage(msg) {
console.log(`RECV: ${msg}`)
if (msg.type() !== Message.Type.Text) {
const file = await msg.toFileBox()
const name = file.name
console.log('Save file to: ' + name)
file.toFile(name)
}
}