#!/usr/bin/env node /** * OpenClaw 飞书消息处理器 - 七牛云上传 * * 用途:作为 OpenClaw 的飞书消息中间件,处理七牛云相关命令 * * 使用方式: * 1. 在 OpenClaw 配置中注册为飞书消息处理器 * 2. 或作为独立服务运行,接收 OpenClaw 转发的消息 */ const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const http = require('http'); // ============ 配置 ============ const CONFIG = { port: process.env.QINIU_HANDLER_PORT || 3001, openclawGateway: { host: '127.0.0.1', port: 17733 }, scriptDir: path.join(__dirname, 'scripts'), credentials: path.join(process.env.HOME || process.env.USERPROFILE, '.openclaw/credentials') }; // ============ 工具函数 ============ function log(...args) { const timestamp = new Date().toISOString(); console.log(`[${timestamp}]`, ...args); } // ============ 命令解析 ============ function isQiniuCommand(text) { const trimmed = text.trim(); return /^\/(upload|qiniu-config|qiniu-help)/i.test(trimmed); } // ============ 消息处理 ============ async function handleMessage(message) { const text = message.content?.text || ''; if (!isQiniuCommand(text)) { return { handled: false }; } log('处理七牛云命令:', text); // 调用上传脚本 const uploadScript = path.join(CONFIG.scriptDir, 'upload-to-qiniu.js'); // 解析命令 if (text.trim().startsWith('/upload')) { return await handleUpload(message, uploadScript); } if (text.trim().startsWith('/qiniu-config')) { return await handleConfig(message, uploadScript); } if (text.trim().startsWith('/qiniu-help')) { return await handleHelp(message); } return { handled: false }; } async function handleUpload(message, script) { // TODO: 实现文件下载和上传逻辑 return { handled: true, reply: '🚧 上传功能开发中...\n\n请使用独立监听器模式:\nhttp://47.83.185.237:3000' }; } async function handleConfig(message, script) { const configCmd = message.content.text.replace('/qiniu-config', 'config'); return new Promise((resolve) => { exec(`node "${script}" ${configCmd}`, (error, stdout, stderr) => { if (error) { resolve({ handled: true, reply: `❌ 错误:${stderr || error.message}` }); return; } resolve({ handled: true, reply: '```\n' + stdout + '\n```' }); }); }); } async function handleHelp(message) { const helpText = ` 🍙 七牛云上传 - 使用帮助 📤 上传文件: /upload [目标路径] [存储桶名] /upload --original [存储桶名] ⚙️ 配置管理: /qiniu-config list /qiniu-config set /qiniu-config set-bucket `; return { handled: true, reply: helpText }; } // ============ HTTP 服务器 ============ function startServer() { const server = http.createServer(async (req, res) => { if (req.method !== 'POST') { res.writeHead(405); res.end('Method Not Allowed'); return; } let body = ''; req.on('data', chunk => body += chunk); req.on('end', async () => { try { const event = JSON.parse(body); const result = await handleMessage(event.message); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(result)); } catch (e) { log('处理失败:', e.message); res.writeHead(500); res.end('Internal Server Error'); } }); }); server.listen(CONFIG.port, () => { log(`🚀 七牛云处理器启动,端口:${CONFIG.port}`); }); } // ============ 主函数 ============ function main() { log('🍙 OpenClaw 飞书消息处理器 - 七牛云'); startServer(); } main();