-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
63 lines (52 loc) · 1.71 KB
/
index.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
import {WechatyBuilder, ScanStatus, log, Message, ContactSelf} from 'wechaty';
import {PuppetPadlocal} from 'wechaty-puppet-padlocal';
import config from './config/config.json';
import {CronCommandController} from './lib/cron-commands/controller';
import {MessageHandler} from './lib/message-handler/handler';
import {MessageCommandController} from './lib/message-handler/commands/controller';
import {MessageRecallHandler} from './lib/message-handler/recall/handler';
function onScan(qrcode: string, status: ScanStatus) {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
log.info('baymax', 'onScan: %s - %s', ScanStatus[status], qrcode);
} else {
log.info('baymax', 'onScan: %s', ScanStatus[status]);
}
}
async function onLogin(user: ContactSelf) {
log.info('baymax', '%s login', user);
cronCommandController.run(bot);
}
async function onMessage(message: Message) {
for await (const handler of messageHandlers) {
if (await handler.handleMessage(message, bot)) {
break;
}
}
}
function onLogout(user: ContactSelf) {
log.info('baymax', '%s logout', user);
}
// order matters
const messageHandlers: MessageHandler[] = [
new MessageCommandController(),
new MessageRecallHandler(),
];
messageHandlers.forEach((handler) => {
handler.setup();
});
const cronCommandController = new CronCommandController();
cronCommandController.setup();
const bot = WechatyBuilder.build({
name: 'baymax',
puppet: new PuppetPadlocal({
token: config.puppet_token,
}),
});
bot
.on('scan', onScan)
.on('login', onLogin)
.on('logout', onLogout)
.on('message', onMessage)
.start()
.then(() => log.info('baymax', 'bot started'))
.catch((e) => log.error('baymax', e));