|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | +import fs from 'node:fs/promises'; |
| 4 | +import { WebSocketServer, WebSocket } from 'ws'; |
| 5 | +import type { IncomingMessage } from 'node:http'; |
| 6 | +import { |
| 7 | + IChannelService, ChannelSessionHandler, SessionService, |
| 8 | + readImageAsDataUrl, isEmptyContent, |
| 9 | + type ChannelMessageArgs, type ILogger, type MessageContent, |
| 10 | +} from 'channel.base'; |
| 11 | +import { OnebotSessionHandler } from './OnebotSessionHandler'; |
| 12 | + |
| 13 | +export interface OnebotMessageArgs extends ChannelMessageArgs { |
| 14 | + messageType: 'private' | 'group'; |
| 15 | + userId: number; |
| 16 | + groupId?: number; |
| 17 | + messageId: number; |
| 18 | + nickname: string; |
| 19 | +} |
| 20 | + |
| 21 | +export interface OnebotServiceOptions { |
| 22 | + wsHost: string; |
| 23 | + wsPort: number; |
| 24 | + accessToken?: string; |
| 25 | + requireMention: boolean; |
| 26 | + logger?: ILogger; |
| 27 | + filterEvent: (eventId: string) => Promise<boolean>; |
| 28 | + onReceiveMessage: (userId: string, args: OnebotMessageArgs, query: MessageContent) => Promise<void>; |
| 29 | +} |
| 30 | + |
| 31 | +interface PendingCall { |
| 32 | + resolve: (data: any) => void; |
| 33 | + reject: (err: Error) => void; |
| 34 | +} |
| 35 | + |
| 36 | +export class OnebotService implements IChannelService { |
| 37 | + private wss: WebSocketServer | null = null; |
| 38 | + private connections = new Set<WebSocket>(); |
| 39 | + private pendingCalls = new Map<string, PendingCall>(); |
| 40 | + private selfId: string = ''; |
| 41 | + private logger?: ILogger; |
| 42 | + private options: OnebotServiceOptions; |
| 43 | + |
| 44 | + constructor(options: OnebotServiceOptions) { |
| 45 | + this.options = options; |
| 46 | + this.logger = options.logger; |
| 47 | + } |
| 48 | + |
| 49 | + createSessionHandler(session: SessionService): ChannelSessionHandler { |
| 50 | + return new OnebotSessionHandler(session, this); |
| 51 | + } |
| 52 | + |
| 53 | + dispose() { |
| 54 | + for (const ws of this.connections) { |
| 55 | + try { ws.close(); } catch (_) {} |
| 56 | + } |
| 57 | + this.connections.clear(); |
| 58 | + if (this.wss) { |
| 59 | + this.wss.close(); |
| 60 | + this.wss = null; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + start() { |
| 65 | + const { wsHost, wsPort, accessToken } = this.options; |
| 66 | + this.wss = new WebSocketServer({ host: wsHost, port: wsPort }); |
| 67 | + this.logger?.info(`OneBot WS server listening on ${wsHost}:${wsPort}`); |
| 68 | + |
| 69 | + this.wss.on('connection', (ws: WebSocket, req: IncomingMessage) => { |
| 70 | + if (accessToken && !this.verifyToken(req, accessToken)) { |
| 71 | + this.logger?.warn('OneBot WS connection rejected: invalid token'); |
| 72 | + ws.close(4001, 'Unauthorized'); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + this.logger?.info('OneBot WS client connected'); |
| 77 | + this.connections.add(ws); |
| 78 | + |
| 79 | + ws.on('message', (raw: Buffer) => { |
| 80 | + try { |
| 81 | + const data = JSON.parse(raw.toString()); |
| 82 | + if (data.echo) { |
| 83 | + this.handleEchoResponse(data); |
| 84 | + } else { |
| 85 | + this.handleEvent(data); |
| 86 | + } |
| 87 | + } catch (e: any) { |
| 88 | + this.logger?.error(`OneBot WS message parse error: ${e.message}`); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + ws.on('close', () => { |
| 93 | + this.connections.delete(ws); |
| 94 | + this.logger?.info('OneBot WS client disconnected'); |
| 95 | + }); |
| 96 | + |
| 97 | + ws.on('error', (err) => { |
| 98 | + this.logger?.error(`OneBot WS error: ${err.message}`); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + this.wss.on('error', (err) => { |
| 103 | + this.logger?.error(`OneBot WS server error: ${err.message}`); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private verifyToken(req: IncomingMessage, token: string): boolean { |
| 108 | + const auth = req.headers['authorization'] ?? ''; |
| 109 | + if (auth === `Bearer ${token}` || auth === `Token ${token}`) return true; |
| 110 | + const url = new URL(req.url ?? '', `http://${req.headers.host}`); |
| 111 | + return url.searchParams.get('access_token') === token; |
| 112 | + } |
| 113 | + |
| 114 | + private handleEchoResponse(data: any) { |
| 115 | + const pending = this.pendingCalls.get(data.echo); |
| 116 | + if (pending) { |
| 117 | + this.pendingCalls.delete(data.echo); |
| 118 | + if (data.retcode === 0) { |
| 119 | + pending.resolve(data.data); |
| 120 | + } else { |
| 121 | + pending.reject(new Error(`OneBot API error: ${data.msg ?? data.wording ?? 'unknown'} (retcode=${data.retcode})`)); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private handleEvent(data: any) { |
| 127 | + if (data.post_type === 'meta_event') { |
| 128 | + this.handleMetaEvent(data); |
| 129 | + } else if (data.post_type === 'message') { |
| 130 | + this.handleMessageEvent(data).catch((e: any) => { |
| 131 | + this.logger?.error(`handleMessageEvent error: ${e.stack}`); |
| 132 | + }); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private handleMetaEvent(data: any) { |
| 137 | + if (data.meta_event_type === 'lifecycle' && data.sub_type === 'connect') { |
| 138 | + this.selfId = String(data.self_id ?? ''); |
| 139 | + this.logger?.info(`OneBot connected, self_id=${this.selfId}`); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private async handleMessageEvent(data: any) { |
| 144 | + const messageType: 'private' | 'group' = data.message_type; |
| 145 | + const userId: number = data.user_id; |
| 146 | + const groupId: number | undefined = data.group_id; |
| 147 | + const messageId: number = data.message_id; |
| 148 | + const nickname: string = data.sender?.card || data.sender?.nickname || String(userId); |
| 149 | + const segments: any[] = Array.isArray(data.message) ? data.message : []; |
| 150 | + |
| 151 | + if (!await this.options.filterEvent(`onebot_message_${messageId}`)) return; |
| 152 | + |
| 153 | + // Check @bot mention in group |
| 154 | + if (messageType === 'group' && this.options.requireMention) { |
| 155 | + const mentioned = segments.some( |
| 156 | + seg => seg.type === 'at' && String(seg.data?.qq) === this.selfId |
| 157 | + ); |
| 158 | + if (!mentioned) return; |
| 159 | + } |
| 160 | + |
| 161 | + // Parse segments into content |
| 162 | + const query = await this.parseSegments(segments, messageId); |
| 163 | + if (isEmptyContent(query)) return; |
| 164 | + |
| 165 | + const sessionId = messageType === 'private' |
| 166 | + ? `onebot:private:${userId}` |
| 167 | + : `onebot:group:${groupId}:${userId}`; |
| 168 | + |
| 169 | + await this.options.onReceiveMessage(String(userId), { |
| 170 | + sessionId, |
| 171 | + messageType, |
| 172 | + userId, |
| 173 | + groupId, |
| 174 | + messageId, |
| 175 | + nickname, |
| 176 | + }, query); |
| 177 | + } |
| 178 | + |
| 179 | + private async parseSegments(segments: any[], messageId: number): Promise<MessageContent> { |
| 180 | + const parts: Array<{ type: string; text?: string; image_url?: { url: string } }> = []; |
| 181 | + let hasImage = false; |
| 182 | + |
| 183 | + for (const seg of segments) { |
| 184 | + if (seg.type === 'text' && seg.data?.text) { |
| 185 | + const text = seg.data.text.trim(); |
| 186 | + if (text) parts.push({ type: 'text', text }); |
| 187 | + } else if (seg.type === 'image' && seg.data?.url) { |
| 188 | + try { |
| 189 | + const filePath = await this.downloadFile(seg.data.url, messageId, '.png'); |
| 190 | + const dataUrl = await readImageAsDataUrl(filePath); |
| 191 | + parts.push({ type: 'image_url', image_url: { url: dataUrl } }); |
| 192 | + hasImage = true; |
| 193 | + } catch (e: any) { |
| 194 | + this.logger?.error(`Failed to download image: ${e.message}`); |
| 195 | + } |
| 196 | + } |
| 197 | + // at, reply, face, forward etc. are ignored |
| 198 | + } |
| 199 | + |
| 200 | + if (parts.length === 0) return ''; |
| 201 | + if (!hasImage) return parts.map(p => p.text ?? '').join(' ').trim(); |
| 202 | + return parts as any; |
| 203 | + } |
| 204 | + |
| 205 | + private async downloadFile(url: string, messageId: number, ext: string): Promise<string> { |
| 206 | + const resp = await fetch(url); |
| 207 | + if (!resp.ok) throw new Error(`Download failed: ${resp.status}`); |
| 208 | + const buffer = Buffer.from(await resp.arrayBuffer()); |
| 209 | + const filePath = path.join(os.tmpdir(), `onebot_${messageId}_${Date.now()}${ext}`); |
| 210 | + await fs.writeFile(filePath, buffer); |
| 211 | + return filePath; |
| 212 | + } |
| 213 | + |
| 214 | + // --- Public API methods --- |
| 215 | + |
| 216 | + async callApi(action: string, params: Record<string, any> = {}): Promise<any> { |
| 217 | + const ws = this.getActiveConnection(); |
| 218 | + if (!ws) throw new Error('No active OneBot WebSocket connection'); |
| 219 | + |
| 220 | + const echo = `${action}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`; |
| 221 | + const payload = JSON.stringify({ action, params, echo }); |
| 222 | + |
| 223 | + return new Promise((resolve, reject) => { |
| 224 | + const timer = setTimeout(() => { |
| 225 | + this.pendingCalls.delete(echo); |
| 226 | + reject(new Error(`OneBot API call timeout: ${action}`)); |
| 227 | + }, 15_000); |
| 228 | + |
| 229 | + this.pendingCalls.set(echo, { |
| 230 | + resolve: (data) => { clearTimeout(timer); resolve(data); }, |
| 231 | + reject: (err) => { clearTimeout(timer); reject(err); }, |
| 232 | + }); |
| 233 | + |
| 234 | + ws.send(payload); |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + async sendTextMessage(target: { userId?: number; groupId?: number }, text: string): Promise<void> { |
| 239 | + const message = [{ type: 'text', data: { text } }]; |
| 240 | + if (target.groupId) { |
| 241 | + await this.callApi('send_group_msg', { group_id: target.groupId, message }); |
| 242 | + } else if (target.userId) { |
| 243 | + await this.callApi('send_private_msg', { user_id: target.userId, message }); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + async sendImageMessage(target: { userId?: number; groupId?: number }, fileUrl: string): Promise<void> { |
| 248 | + const message = [{ type: 'image', data: { file: fileUrl } }]; |
| 249 | + if (target.groupId) { |
| 250 | + await this.callApi('send_group_msg', { group_id: target.groupId, message }); |
| 251 | + } else if (target.userId) { |
| 252 | + await this.callApi('send_private_msg', { user_id: target.userId, message }); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + async sendFileMessage(target: { userId?: number; groupId?: number }, filePath: string, fileName: string): Promise<void> { |
| 257 | + if (target.groupId) { |
| 258 | + await this.callApi('upload_group_file', { group_id: target.groupId, file: filePath, name: fileName }); |
| 259 | + } else if (target.userId) { |
| 260 | + await this.callApi('upload_private_file', { user_id: target.userId, file: filePath, name: fileName }); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + private getActiveConnection(): WebSocket | null { |
| 265 | + for (const ws of this.connections) { |
| 266 | + if (ws.readyState === WebSocket.OPEN) return ws; |
| 267 | + } |
| 268 | + return null; |
| 269 | + } |
| 270 | +} |
0 commit comments