-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.ts
114 lines (95 loc) · 2.77 KB
/
bot.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import LUISClient from 'luis-sdk'
import { generate } from 'qrcode-terminal'
import { Contact, Message, Wechaty } from 'wechaty'
const bot = new Wechaty({
name: 'lijiarui',
})
/**
* Config Luis, see: www.luis.ai
*/
const APPID = '841b05a2-bebf-4b94-a82a-85236679687e'
const APPKEY = '7dbf6405b5154fbca1a8cce9363feab8'
const DOMAIN = 'westus.api.cognitive.microsoft.com'
const LUISclient = LUISClient({
appId : APPID,
appKey : APPKEY,
domain : DOMAIN,
verbose : false,
})
/**
* Bot Action
*/
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => console.log('Starter Bot Started.'))
.catch(e => console.error(e))
function onScan (qrcode: string) {
generate(qrcode, { small: true }) // show qrcode on console
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log(qrcodeImageUrl)
}
function onLogin (user: Contact) {
console.log(`${user} login`)
}
function onLogout (user: Contact) {
console.log(`${user} logout`)
}
/**
* Using Luis to deal with message
*/
async function onMessage (msg: Message) {
const text = msg.text()
const type = msg.type()
const room = msg.room()
const contact = msg.from()
if (msg.self()) {
return
}
if (!contact) {
return
}
if (type !== Message.Type.Text) {
return
}
console.log(msg.toString())
if (room) {
const topic = await room.topic()
if (topic === 'test') {
LUISclient.predict(text, {
// On success of prediction
onSuccess: async (response) => {
console.log(JSON.stringify(response))
if (!response.topScoringIntent) {
return
}
if (response.topScoringIntent.intent === 'Weather.CheckWeatherTime') {
await room.say('意图为:根据天气查询时间', contact)
} else if (response.topScoringIntent.intent === 'Weather.CheckWeatherValue') {
await room.say('意图为:根据时间地点查询天气', contact)
} else if (response.topScoringIntent.intent === 'Weather.QueryWeather') {
await room.say('意图为:确认天气', contact)
} else {
await room.say('你没有触发任何意图', contact)
}
if (response.entities.length > 0) {
await room.say('触发的实体信息为:', contact)
for (let i = 1; i <= response.entities.length; i++) {
await room.say(i + '- ' + response.entities[i - 1].entity, contact)
}
}
},
// On failure of prediction
onFailure: (err: undefined | Error) => {
console.error(err)
}
})
}
return
}
}