-
Notifications
You must be signed in to change notification settings - Fork 7
/
getting-started.ts
67 lines (53 loc) · 1.42 KB
/
getting-started.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
/**
* Wechaty - WeChat Bot SDK for Personal Account, Powered by TypeScript, Docker, and 💖
* - https://github.com/chatie/wechaty
*/
import {
Contact,
Message,
ScanStatus,
Wechaty,
} from 'wechaty'
import { generate } from 'qrcode-terminal'
const bot = new Wechaty({ name: 'lijiarui' })
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => console.log('StarterBot', 'Starter Bot Started.'))
.catch(e => console.error('StarterBot', e))
function onScan (qrcode: string, status: ScanStatus) {
generate(qrcode) // show qrcode on console
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log('StarterBot', '%s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
}
function onLogin (user: Contact) {
console.log('StarterBot', '%s login', user)
}
function onLogout (user: Contact) {
console.log('StarterBot', '%s logout', user)
}
async function onMessage (msg: Message) {
const contact = msg.from()
console.log('StarterBot', msg.toString())
if (msg.self()) {
return
}
if (!contact) {
return
}
const room = msg.room()
if (room) {
const topic = await room.topic()
if (topic === 'test') {
if (msg.text().toLowerCase() === 'ding') {
await room.say('dong', contact)
}
}
return
}
}