forked from shfshanyue/wechat-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
48 lines (43 loc) · 1.1 KB
/
server.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
import Koa from 'koa'
import { Wechaty } from 'wechaty'
import { createBot } from './bot'
import * as message from './event/message'
import * as friendShip from './event/friend-ship'
import * as roomJoin from './event/room-join'
const app = new Koa()
function listen(bot: Wechaty): Promise<{
type: 'scan' | 'login',
data: any
}> {
return new Promise((resolve, reject) => {
bot
.on('scan', (qrcode) => {
resolve({
type: 'scan',
data: qrcode
})
})
.on('login', (user) => {
resolve({
type: 'login',
data: user
})
})
.on('room-join', roomJoin.handleRoomJoin)
.on('friendship', friendShip.handleFriendShip)
.on('message', message.handleMessage)
.start()
})
}
app.use(async (ctx) => {
const bot = createBot()
const { type, data } = await listen(bot)
if (type === 'scan') {
ctx.body = `<img style="width: 300" src="https://devtool.tech/api/qrcode?data=${encodeURIComponent(data)}">`
} else {
ctx.body = '已登录'
}
})
app.listen(3000, () => {
console.log('Listing 3000...')
})