-
Notifications
You must be signed in to change notification settings - Fork 0
/
asker.ts
112 lines (93 loc) · 3.01 KB
/
asker.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
import {
log,
Room,
} from 'wechaty'
import type {
RepoConfig,
ChatoperaOptions,
ChatoperaResponse,
} from './chatopera.js'
import {
Chatbot,
Chatopera,
} from '@chatopera/sdk'
interface RoomBotConfig {
roomId: string;
name: string;
secret: string;
}
async function initBot (defaultOptions?: ChatoperaOptions, repoConfig?: RepoConfig) {
const result: RoomBotConfig[] = []
const token = defaultOptions?.personalAccessToken
if (token) {
const chatopera = new Chatopera(token)
const resp = await chatopera.command('GET', '/chatbot?limit=9999')
if (repoConfig && resp.rc === 0) {
const bots: { clientId: string; name: string; secret: string }[] = resp.data
for (const fullName in repoConfig) {
const owner = fullName.split('/')[0] || 'NOOWNER'
const botName = `osschat_${owner.toLowerCase()}_bot`
let targetBot = bots.find((b) => b.name === botName)
if (!targetBot) {
const createBotRes = await chatopera.command('POST', '/chatbot', {
description: 'osschat bot',
logo: '',
name: botName,
primaryLanguage: 'zh_CN',
trans_zhCN_ZhTw2ZhCn: true,
})
if (createBotRes.rc === 0) {
targetBot = createBotRes.data
}
}
let roomId = repoConfig[fullName]!
if (!Array.isArray(roomId)) {
roomId = [roomId]
}
if (targetBot) {
const options = targetBot
roomId.forEach((r) => result.push({ roomId: r, ...options }))
}
}
}
}
return result
}
function asker (defaultOptions: ChatoperaOptions, repoConfig?: RepoConfig) {
log.verbose('WechatyChatopera', 'asker(%s)', JSON.stringify(defaultOptions))
const botPromise = initBot(defaultOptions, repoConfig)
const findOption = async (roomId?: string): Promise<ChatoperaOptions> => {
const botList = await botPromise
const targetBot = botList.find((b) => b.roomId === roomId)
if (targetBot) {
return { ...defaultOptions, ...targetBot }
} else {
return defaultOptions
}
}
return async function ask (
question: string,
contactId: string,
room?: Room,
): Promise<ChatoperaResponse> {
log.verbose('WechatyChatopera', 'ask(%s, %s, %s)', question, contactId, room)
const options = await findOption(room?.id)
if (!(options.clientId && options.secret)) {
return { botName: '', logic_is_fallback: true, logic_is_unexpected: true, service: { provider: 'BOT_NOT_DEF' }, state: '', string: '' }
}
const chatbot = new Chatbot(options.clientId, options.secret)
if (room) {
contactId = `${await room.topic()}`
}
const cmdRes = await chatbot.command('POST', '/conversation/query', {
faqBestReplyThreshold: options.faqBestReplyThreshold,
faqSuggReplyThreshold: options.faqSuggReplyThreshold,
fromUserId: contactId,
textMessage: question,
})
return cmdRes.data as ChatoperaResponse
}
}
export {
asker,
}