@dcrays/dcgchat 0.2.19 → 0.2.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +20 -17
- package/openclaw.plugin.json +4 -2
- package/package.json +3 -6
- package/src/bot.ts +280 -514
- package/src/channel.ts +111 -179
- package/src/monitor.ts +130 -130
- package/src/{api.ts → request/api.ts} +34 -35
- package/src/request/oss.ts +58 -0
- package/src/request/request.ts +198 -0
- package/src/{userInfo.ts → request/userInfo.ts} +36 -34
- package/src/skill.ts +110 -194
- package/src/tool.ts +116 -82
- package/src/transport.ts +108 -0
- package/src/types.ts +75 -64
- package/src/utils/constant.ts +7 -0
- package/src/utils/global.ts +117 -0
- package/src/utils/log.ts +15 -0
- package/src/utils/searchFile.ts +212 -0
- package/README.md +0 -83
- package/src/connection.ts +0 -11
- package/src/log.ts +0 -46
- package/src/oss.ts +0 -72
- package/src/request.ts +0 -201
- package/src/runtime.ts +0 -40
package/src/monitor.ts
CHANGED
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
import type { ClawdbotConfig, RuntimeEnv } from
|
|
2
|
-
import WebSocket from
|
|
3
|
-
import { handleDcgchatMessage } from
|
|
4
|
-
import { resolveAccount } from
|
|
5
|
-
import { setWsConnection } from
|
|
6
|
-
import type { InboundMessage
|
|
7
|
-
import { setMsgParams,setMsgStatus } from
|
|
8
|
-
import { installSkill, uninstallSkill } from
|
|
1
|
+
import type { ClawdbotConfig, RuntimeEnv } from 'openclaw/plugin-sdk'
|
|
2
|
+
import WebSocket from 'ws'
|
|
3
|
+
import { handleDcgchatMessage } from './bot.js'
|
|
4
|
+
import { resolveAccount } from './channel.js'
|
|
5
|
+
import { setWsConnection, getOpenClawConfig } from './utils/global.js'
|
|
6
|
+
import type { InboundMessage } from './types.js'
|
|
7
|
+
import { setMsgParams, setMsgStatus } from './utils/global.js'
|
|
8
|
+
import { installSkill, uninstallSkill } from './skill.js'
|
|
9
|
+
import { dcgLogger } from './utils/log.js'
|
|
10
|
+
import { ignoreToolCommand } from './utils/constant.js'
|
|
9
11
|
|
|
10
12
|
export type MonitorDcgchatOpts = {
|
|
11
|
-
config?: ClawdbotConfig
|
|
12
|
-
runtime?: RuntimeEnv
|
|
13
|
-
abortSignal?: AbortSignal
|
|
14
|
-
accountId?: string
|
|
15
|
-
}
|
|
13
|
+
config?: ClawdbotConfig
|
|
14
|
+
runtime?: RuntimeEnv
|
|
15
|
+
abortSignal?: AbortSignal
|
|
16
|
+
accountId?: string
|
|
17
|
+
}
|
|
16
18
|
|
|
17
|
-
const RECONNECT_DELAY_MS = 3000
|
|
18
|
-
const HEARTBEAT_INTERVAL_MS = 30_000
|
|
19
|
+
const RECONNECT_DELAY_MS = 3000
|
|
20
|
+
const HEARTBEAT_INTERVAL_MS = 30_000
|
|
19
21
|
|
|
20
22
|
function buildConnectUrl(account: Record<string, string>): string {
|
|
21
|
-
const { wsUrl, botToken, userId, domainId, appId } = account
|
|
22
|
-
const url = new URL(wsUrl)
|
|
23
|
-
if (botToken) url.searchParams.set(
|
|
24
|
-
if (userId) url.searchParams.set(
|
|
25
|
-
url.searchParams.set(
|
|
26
|
-
url.searchParams.set(
|
|
27
|
-
return url.toString()
|
|
23
|
+
const { wsUrl, botToken, userId, domainId, appId } = account
|
|
24
|
+
const url = new URL(wsUrl)
|
|
25
|
+
if (botToken) url.searchParams.set('bot_token', botToken)
|
|
26
|
+
if (userId) url.searchParams.set('_userId', userId)
|
|
27
|
+
url.searchParams.set('_domainId', domainId || '1000')
|
|
28
|
+
url.searchParams.set('_appId', appId || '100')
|
|
29
|
+
return url.toString()
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
export async function monitorDcgchatProvider(opts: MonitorDcgchatOpts): Promise<void> {
|
|
31
|
-
const {
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
if (!
|
|
35
|
-
|
|
36
|
-
return
|
|
33
|
+
const { abortSignal, accountId } = opts
|
|
34
|
+
|
|
35
|
+
const config = getOpenClawConfig()
|
|
36
|
+
if (!config) {
|
|
37
|
+
dcgLogger('no config available', 'error')
|
|
38
|
+
return
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
const account = resolveAccount(
|
|
40
|
-
const log = runtime?.log ?? console.log;
|
|
41
|
-
const err = runtime?.error ?? console.error;
|
|
41
|
+
const account = resolveAccount(config, accountId ?? 'default')
|
|
42
42
|
|
|
43
43
|
if (!account.wsUrl) {
|
|
44
|
-
|
|
45
|
-
return
|
|
44
|
+
dcgLogger(` wsUrl not configured`, 'error')
|
|
45
|
+
return
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
let shouldReconnect = true
|
|
49
|
-
let ws: WebSocket | null = null
|
|
50
|
-
let heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
|
48
|
+
let shouldReconnect = true
|
|
49
|
+
let ws: WebSocket | null = null
|
|
50
|
+
let heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
|
51
51
|
|
|
52
52
|
const stopHeartbeat = () => {
|
|
53
53
|
if (heartbeatTimer) {
|
|
54
|
-
clearInterval(heartbeatTimer)
|
|
55
|
-
heartbeatTimer = null
|
|
54
|
+
clearInterval(heartbeatTimer)
|
|
55
|
+
heartbeatTimer = null
|
|
56
56
|
}
|
|
57
|
-
}
|
|
57
|
+
}
|
|
58
58
|
|
|
59
59
|
const startHeartbeat = () => {
|
|
60
|
-
stopHeartbeat()
|
|
60
|
+
stopHeartbeat()
|
|
61
61
|
heartbeatTimer = setInterval(() => {
|
|
62
62
|
if (ws?.readyState === WebSocket.OPEN) {
|
|
63
63
|
const heartbeat = {
|
|
64
|
-
messageType:
|
|
64
|
+
messageType: 'openclaw_bot_heartbeat',
|
|
65
65
|
_userId: Number(account.userId) || 0,
|
|
66
|
-
source:
|
|
66
|
+
source: 'client',
|
|
67
67
|
content: {
|
|
68
68
|
bot_token: account.botToken,
|
|
69
|
-
status:
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
ws.send(JSON.stringify(heartbeat))
|
|
73
|
-
// log(`dcgchat[${account.accountId}]: heartbeat sent, ${JSON.stringify(heartbeat)}`);
|
|
69
|
+
status: '1'
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
ws.send(JSON.stringify(heartbeat))
|
|
74
73
|
}
|
|
75
|
-
}, HEARTBEAT_INTERVAL_MS)
|
|
76
|
-
}
|
|
74
|
+
}, HEARTBEAT_INTERVAL_MS)
|
|
75
|
+
}
|
|
77
76
|
|
|
78
77
|
const connect = () => {
|
|
79
|
-
if (!shouldReconnect) return
|
|
78
|
+
if (!shouldReconnect) return
|
|
80
79
|
|
|
81
|
-
const connectUrl = buildConnectUrl(account as Record<string, any>)
|
|
82
|
-
|
|
83
|
-
ws = new WebSocket(connectUrl)
|
|
80
|
+
const connectUrl = buildConnectUrl(account as Record<string, any>)
|
|
81
|
+
dcgLogger(`connecting to ${connectUrl}`)
|
|
82
|
+
ws = new WebSocket(connectUrl)
|
|
84
83
|
|
|
85
|
-
ws.on(
|
|
86
|
-
|
|
87
|
-
setWsConnection(ws)
|
|
88
|
-
startHeartbeat()
|
|
89
|
-
})
|
|
84
|
+
ws.on('open', () => {
|
|
85
|
+
dcgLogger(`socket connected`)
|
|
86
|
+
setWsConnection(ws)
|
|
87
|
+
startHeartbeat()
|
|
88
|
+
})
|
|
90
89
|
|
|
91
|
-
ws.on(
|
|
92
|
-
|
|
93
|
-
let parsed: { messageType?: string; content: any };
|
|
90
|
+
ws.on('message', async (data) => {
|
|
91
|
+
let parsed: { messageType?: string; content: any }
|
|
94
92
|
try {
|
|
95
|
-
parsed = JSON.parse(data.toString())
|
|
93
|
+
parsed = JSON.parse(data.toString())
|
|
96
94
|
} catch {
|
|
97
|
-
|
|
98
|
-
return
|
|
95
|
+
dcgLogger(`用户消息解析失败: invalid JSON received`, 'error')
|
|
96
|
+
return
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
99
|
+
dcgLogger(`${parsed?.messageType}, ${data.toString()}`)
|
|
100
|
+
if (parsed.messageType === 'openclaw_bot_heartbeat') {
|
|
101
|
+
dcgLogger(`heartbeat ack received, ${data.toString()}`)
|
|
102
|
+
return
|
|
104
103
|
}
|
|
105
104
|
try {
|
|
106
|
-
parsed.content = JSON.parse(parsed.content)
|
|
105
|
+
parsed.content = JSON.parse(parsed.content)
|
|
107
106
|
} catch {
|
|
108
|
-
|
|
109
|
-
return
|
|
107
|
+
dcgLogger(`用户消息content字段解析失败: invalid JSON received`, 'error')
|
|
108
|
+
return
|
|
110
109
|
}
|
|
111
|
-
|
|
112
|
-
if (parsed.messageType ==
|
|
113
|
-
const msg = parsed as unknown as InboundMessage
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
|
|
111
|
+
if (parsed.messageType == 'openclaw_bot_chat') {
|
|
112
|
+
const msg = parsed as unknown as InboundMessage
|
|
113
|
+
if (!ignoreToolCommand.includes(msg.content.text?.trim())) {
|
|
114
|
+
setMsgStatus('running')
|
|
115
|
+
}
|
|
116
|
+
// 设置获取用户消息消息参数
|
|
116
117
|
setMsgParams({
|
|
117
118
|
userId: msg._userId,
|
|
118
119
|
token: msg.content.bot_token,
|
|
119
120
|
sessionId: msg.content.session_id,
|
|
120
121
|
messageId: msg.content.message_id,
|
|
121
|
-
domainId: account.domainId ||
|
|
122
|
+
domainId: account.domainId || 1000,
|
|
122
123
|
appId: account.appId || '100',
|
|
123
124
|
botId: msg.content.bot_id,
|
|
124
|
-
agentId: msg.content.agent_id
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
125
|
+
agentId: msg.content.agent_id
|
|
126
|
+
})
|
|
127
|
+
msg.content.app_id = account.appId || '100'
|
|
128
|
+
msg.content.domain_id = account.domainId || '1000'
|
|
129
|
+
|
|
130
|
+
await handleDcgchatMessage(msg, account.accountId)
|
|
131
|
+
} else if (parsed.messageType == 'openclaw_bot_event') {
|
|
132
|
+
const { event_type, operation_type, skill_url, skill_code, skill_id, bot_token, websocket_trace_id } = parsed.content
|
|
133
|
+
? parsed.content
|
|
134
|
+
: ({} as Record<string, any>)
|
|
135
|
+
const content = {
|
|
136
|
+
event_type,
|
|
137
|
+
operation_type,
|
|
138
|
+
skill_url,
|
|
139
|
+
skill_code,
|
|
140
|
+
skill_id,
|
|
141
|
+
bot_token,
|
|
142
|
+
websocket_trace_id
|
|
143
|
+
}
|
|
144
|
+
if (event_type === 'skill') {
|
|
145
|
+
if (operation_type === 'install' || operation_type === 'enable' || operation_type === 'update') {
|
|
146
|
+
installSkill({ path: skill_url, code: skill_code }, content)
|
|
147
|
+
} else if (operation_type === 'remove' || operation_type === 'disable') {
|
|
148
|
+
uninstallSkill({ code: skill_code }, content)
|
|
146
149
|
} else {
|
|
147
|
-
|
|
150
|
+
dcgLogger(`openclaw_bot_event unknown event_type: ${event_type}, ${data.toString()}`)
|
|
148
151
|
}
|
|
149
152
|
} else {
|
|
150
|
-
|
|
153
|
+
dcgLogger(`openclaw_bot_event unknown operation_type: ${operation_type}, ${data.toString()}`)
|
|
151
154
|
}
|
|
152
155
|
} else {
|
|
153
|
-
|
|
156
|
+
dcgLogger(`ignoring unknown messageType: ${parsed.messageType}`, 'error')
|
|
154
157
|
}
|
|
158
|
+
})
|
|
155
159
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
setWsConnection(null);
|
|
161
|
-
log(
|
|
162
|
-
`dcgchat[${account.accountId}]: disconnected (code=${code}, reason=${reason?.toString() || ""})`,
|
|
163
|
-
);
|
|
160
|
+
ws.on('close', (code, reason) => {
|
|
161
|
+
stopHeartbeat()
|
|
162
|
+
setWsConnection(null)
|
|
163
|
+
dcgLogger(`disconnected (code=${code}, reason=${reason?.toString() || ''})`, 'error')
|
|
164
164
|
if (shouldReconnect) {
|
|
165
|
-
|
|
166
|
-
setTimeout(connect, RECONNECT_DELAY_MS)
|
|
165
|
+
dcgLogger(`reconnecting in ${RECONNECT_DELAY_MS}ms...`)
|
|
166
|
+
setTimeout(connect, RECONNECT_DELAY_MS)
|
|
167
167
|
}
|
|
168
|
-
})
|
|
168
|
+
})
|
|
169
169
|
|
|
170
|
-
ws.on(
|
|
171
|
-
|
|
172
|
-
})
|
|
173
|
-
}
|
|
170
|
+
ws.on('error', (e) => {
|
|
171
|
+
dcgLogger(`WebSocket error: ${String(e)}`, 'error')
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
174
|
|
|
175
|
-
connect()
|
|
175
|
+
connect()
|
|
176
176
|
|
|
177
177
|
await new Promise<void>((resolve) => {
|
|
178
178
|
if (abortSignal?.aborted) {
|
|
179
|
-
shouldReconnect = false
|
|
180
|
-
ws?.close()
|
|
181
|
-
resolve()
|
|
182
|
-
return
|
|
179
|
+
shouldReconnect = false
|
|
180
|
+
ws?.close()
|
|
181
|
+
resolve()
|
|
182
|
+
return
|
|
183
183
|
}
|
|
184
184
|
abortSignal?.addEventListener(
|
|
185
|
-
|
|
185
|
+
'abort',
|
|
186
186
|
() => {
|
|
187
|
-
|
|
188
|
-
stopHeartbeat()
|
|
189
|
-
shouldReconnect = false
|
|
190
|
-
ws?.close()
|
|
191
|
-
resolve()
|
|
187
|
+
dcgLogger(`socket stopping`)
|
|
188
|
+
stopHeartbeat()
|
|
189
|
+
shouldReconnect = false
|
|
190
|
+
ws?.close()
|
|
191
|
+
resolve()
|
|
192
192
|
},
|
|
193
|
-
{ once: true }
|
|
194
|
-
)
|
|
195
|
-
})
|
|
193
|
+
{ once: true }
|
|
194
|
+
)
|
|
195
|
+
})
|
|
196
196
|
}
|
|
@@ -1,49 +1,46 @@
|
|
|
1
|
-
import { post } from
|
|
2
|
-
import type { IStsToken, IStsTokenReq } from
|
|
3
|
-
import { getUserTokenCache, setUserTokenCache } from
|
|
1
|
+
import { post } from './request.js'
|
|
2
|
+
import type { IStsToken, IStsTokenReq } from '../types.js'
|
|
3
|
+
import { getUserTokenCache, setUserTokenCache } from './userInfo.js'
|
|
4
|
+
import { dcgLogger } from '../utils/log.js'
|
|
4
5
|
|
|
5
6
|
export const getStsToken = async (name: string, botToken: string) => {
|
|
6
7
|
// 确保 userToken 已缓存(如果未缓存会自动获取并缓存)
|
|
7
|
-
await getUserToken(botToken)
|
|
8
|
+
await getUserToken(botToken)
|
|
8
9
|
|
|
9
10
|
const response = await post<IStsTokenReq, IStsToken>(
|
|
10
|
-
|
|
11
|
+
'/user/getStsToken',
|
|
11
12
|
{
|
|
12
13
|
sourceFileName: name,
|
|
13
|
-
isPrivate: 1
|
|
14
|
+
isPrivate: 1
|
|
14
15
|
},
|
|
15
|
-
{ botToken }
|
|
16
|
-
)
|
|
16
|
+
{ botToken }
|
|
17
|
+
)
|
|
17
18
|
|
|
18
19
|
if (!response || !response.data || !response.data.bucket) {
|
|
19
|
-
throw new Error(
|
|
20
|
+
throw new Error('获取 OSS 临时凭证失败')
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
return response.data
|
|
23
|
-
}
|
|
23
|
+
return response.data
|
|
24
|
+
}
|
|
24
25
|
export const generateSignUrl = async (file_url: string, botToken: string) => {
|
|
25
26
|
try {
|
|
26
27
|
// 确保 userToken 已缓存(如果未缓存会自动获取并缓存)
|
|
27
|
-
await getUserToken(botToken)
|
|
28
|
-
|
|
28
|
+
await getUserToken(botToken)
|
|
29
|
+
|
|
29
30
|
const response = await post<any>(
|
|
30
|
-
|
|
31
|
-
{
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
{ botToken },
|
|
36
|
-
);
|
|
31
|
+
'/user/generateSignUrl',
|
|
32
|
+
{ loudPlatform: 0, fileName: file_url },
|
|
33
|
+
{ botToken }
|
|
34
|
+
)
|
|
37
35
|
if (response.code === 0 && response.data) {
|
|
38
36
|
// @ts-ignore
|
|
39
37
|
return response.data?.filePath
|
|
40
38
|
}
|
|
41
39
|
return ''
|
|
42
|
-
|
|
43
40
|
} catch (error) {
|
|
44
41
|
return ''
|
|
45
42
|
}
|
|
46
|
-
}
|
|
43
|
+
}
|
|
47
44
|
|
|
48
45
|
/**
|
|
49
46
|
* 通过 botToken 查询 userToken
|
|
@@ -51,16 +48,18 @@ export const generateSignUrl = async (file_url: string, botToken: string) => {
|
|
|
51
48
|
* @returns userToken
|
|
52
49
|
*/
|
|
53
50
|
export const queryUserTokenByBotToken = async (botToken: string): Promise<string> => {
|
|
54
|
-
const response = await post<{botToken: string}, {token: string}>(
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const response = await post<{ botToken: string }, { token: string }>(
|
|
52
|
+
'/organization/queryUserTokenByBotToken',
|
|
53
|
+
{ botToken }
|
|
54
|
+
)
|
|
57
55
|
|
|
58
56
|
if (!response || !response.data || !response.data.token) {
|
|
59
|
-
|
|
57
|
+
dcgLogger('获取绑定的用户信息失败', 'error')
|
|
58
|
+
return ''
|
|
60
59
|
}
|
|
61
60
|
|
|
62
|
-
return response.data.token
|
|
63
|
-
}
|
|
61
|
+
return response.data.token
|
|
62
|
+
}
|
|
64
63
|
|
|
65
64
|
/**
|
|
66
65
|
* 获取 userToken(优先从缓存获取,缓存未命中则调用 API)
|
|
@@ -69,17 +68,17 @@ export const queryUserTokenByBotToken = async (botToken: string): Promise<string
|
|
|
69
68
|
*/
|
|
70
69
|
export const getUserToken = async (botToken: string): Promise<string> => {
|
|
71
70
|
// 1. 尝试从缓存获取
|
|
72
|
-
const cachedToken = getUserTokenCache(botToken)
|
|
71
|
+
const cachedToken = getUserTokenCache(botToken)
|
|
73
72
|
if (cachedToken) {
|
|
74
|
-
return cachedToken
|
|
73
|
+
return cachedToken
|
|
75
74
|
}
|
|
76
75
|
|
|
77
76
|
// 2. 缓存未命中,调用 API 获取
|
|
78
|
-
|
|
79
|
-
const userToken = await queryUserTokenByBotToken(botToken)
|
|
77
|
+
dcgLogger(`[api] cache miss, fetching userToken for botToken=${botToken.slice(0, 10)}...`)
|
|
78
|
+
const userToken = await queryUserTokenByBotToken(botToken)
|
|
80
79
|
|
|
81
80
|
// 3. 缓存新获取的 token
|
|
82
|
-
setUserTokenCache(botToken, userToken)
|
|
81
|
+
setUserTokenCache(botToken, userToken)
|
|
83
82
|
|
|
84
|
-
return userToken
|
|
85
|
-
}
|
|
83
|
+
return userToken
|
|
84
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs'
|
|
2
|
+
// @ts-ignore
|
|
3
|
+
import OSS from 'ali-oss'
|
|
4
|
+
import { getStsToken, getUserToken } from './api.js'
|
|
5
|
+
import { dcgLogger } from '../utils/log.js'
|
|
6
|
+
|
|
7
|
+
/** 将 File/路径/Buffer 转为 ali-oss put 所需的 Buffer 或 ReadableStream */
|
|
8
|
+
async function toUploadContent(
|
|
9
|
+
input: File | string | Buffer
|
|
10
|
+
): Promise<{ content: Buffer | ReturnType<typeof createReadStream>; fileName: string }> {
|
|
11
|
+
if (Buffer.isBuffer(input)) {
|
|
12
|
+
return { content: input, fileName: 'file' }
|
|
13
|
+
}
|
|
14
|
+
if (typeof input === 'string') {
|
|
15
|
+
return {
|
|
16
|
+
content: createReadStream(input),
|
|
17
|
+
fileName: input.split('/').pop() ?? 'file'
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// File: ali-oss 需要 Buffer/Stream,用 arrayBuffer 转 Buffer
|
|
21
|
+
const buf = Buffer.from(await input.arrayBuffer())
|
|
22
|
+
return { content: buf, fileName: input.name }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const ossUpload = async (file: File | string | Buffer, botToken: string) => {
|
|
26
|
+
await getUserToken(botToken)
|
|
27
|
+
|
|
28
|
+
const { content, fileName } = await toUploadContent(file)
|
|
29
|
+
const data = await getStsToken(fileName, botToken)
|
|
30
|
+
|
|
31
|
+
const options: OSS.Options = {
|
|
32
|
+
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
|
|
33
|
+
accessKeyId: data.tempAccessKeyId,
|
|
34
|
+
accessKeySecret: data.tempAccessKeySecret,
|
|
35
|
+
// 从STS服务获取的安全令牌(SecurityToken)。
|
|
36
|
+
stsToken: data.tempSecurityToken,
|
|
37
|
+
// 填写Bucket名称。
|
|
38
|
+
bucket: data.bucket,
|
|
39
|
+
endpoint: data.endPoint,
|
|
40
|
+
region: data.region,
|
|
41
|
+
secure: true
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const client = new OSS(options)
|
|
45
|
+
|
|
46
|
+
const name = `${data.uploadDir}${data.ossFileKey}`
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const objectResult = await client.put(name, content)
|
|
50
|
+
if (objectResult?.res?.status !== 200) {
|
|
51
|
+
dcgLogger(`OSS 上传失败, ${objectResult?.res?.status}`)
|
|
52
|
+
}
|
|
53
|
+
dcgLogger(JSON.stringify(objectResult))
|
|
54
|
+
return objectResult.name || objectResult.url
|
|
55
|
+
} catch (error) {
|
|
56
|
+
dcgLogger(`OSS 上传失败: ${error}`, 'error')
|
|
57
|
+
}
|
|
58
|
+
}
|