import React, { useState, useEffect } from 'react'; function App() { const [config, setConfig] = useState({ bots: [], openclaw: { url: 'ws://localhost:18789', token: '', enabled: true } }); const [connectionStatus, setConnectionStatus] = useState({ wecom: {}, openclaw: { connected: false } }); const [logs, setLogs] = useState([]); const [showAddBot, setShowAddBot] = useState(false); const [showTestMessage, setShowTestMessage] = useState(false); const [newBot, setNewBot] = useState({ botId: '', secret: '', name: '', enabled: true }); const [testMessage, setTestMessage] = useState({ botId: '', chatId: '', text: '' }); const [testOpenClawMessage, setTestOpenClawMessage] = useState(''); const [testOpenClawResult, setTestOpenClawResult] = useState(null); // 加载配置 useEffect(() => { loadConfig(); setupEventListeners(); // 定期更新连接状态 const statusInterval = setInterval(async () => { const status = await window.electronAPI.getConnectionStatus(); setConnectionStatus(status); }, 3000); return () => { clearInterval(statusInterval); window.electronAPI.removeAllListeners('wecom-event'); window.electronAPI.removeAllListeners('openclaw-event'); }; }, []); const loadConfig = async () => { try { const savedConfig = await window.electronAPI.getConfig(); setConfig(savedConfig); addLog('success', '配置加载成功'); } catch (error) { addLog('error', `加载配置失败:${error.message}`); } }; const setupEventListeners = () => { window.electronAPI.onWeComEvent((event) => { const { eventType, data } = event; if (eventType === 'message') { const body = data.frame?.body; const text = body?.text?.content || body?.voice?.content || '[媒体消息]'; addLog('info', `[WeCom] 收到消息 from ${body?.from?.userid}: ${text.substring(0, 50)}`); } else if (eventType === 'connected') { addLog('success', `[WeCom] Bot ${data.botId} 已连接`); } else if (eventType === 'disconnected') { addLog('warning', `[WeCom] Bot ${data.botId} 断开:${data.reason}`); } else if (eventType === 'error') { addLog('error', `[WeCom] Bot ${data.botId} 错误:${data.error}`); } // 更新连接状态 setTimeout(updateConnectionStatus, 100); }); window.electronAPI.onOpenClawEvent((event) => { const { eventType, data } = event; if (eventType === 'connected') { addLog('success', '[OpenClaw] Gateway 已连接'); } else if (eventType === 'disconnected') { addLog('warning', '[OpenClaw] Gateway 断开'); } else if (eventType === 'error') { addLog('error', `[OpenClaw] 错误:${data?.error}`); } // 更新连接状态 setTimeout(updateConnectionStatus, 100); }); }; const updateConnectionStatus = async () => { const status = await window.electronAPI.getConnectionStatus(); setConnectionStatus(status); }; const addLog = (type, message) => { const timestamp = new Date().toLocaleTimeString(); setLogs(prev => [...prev.slice(-199), { type, message, timestamp }]); }; const saveConfig = async () => { try { await window.electronAPI.saveConfig(config); addLog('success', '配置保存成功'); } catch (error) { addLog('error', `保存配置失败:${error.message}`); } }; const handleAddBot = async () => { if (!newBot.botId || !newBot.secret) { addLog('warning', '请填写 Bot ID 和 Secret'); return; } const updatedBots = [...config.bots, { ...newBot, id: Date.now().toString() }]; setConfig({ ...config, bots: updatedBots }); setNewBot({ botId: '', secret: '', name: '', enabled: true }); setShowAddBot(false); await saveConfig(); addLog('success', `添加机器人:${newBot.name || newBot.botId}`); }; const handleDeleteBot = async (botId) => { await window.electronAPI.disconnectWeCom(botId); const updatedBots = config.bots.filter(b => b.id !== botId); setConfig({ ...config, bots: updatedBots }); await saveConfig(); addLog('info', `删除机器人:${botId}`); }; const handleConnectWeCom = async (bot) => { try { await window.electronAPI.connectWeCom(bot); addLog('info', `正在连接企业微信 Bot: ${bot.botId}`); } catch (error) { addLog('error', `连接失败:${error.message}`); } }; const handleDisconnectWeCom = async (botId) => { await window.electronAPI.disconnectWeCom(botId); addLog('info', `断开企业微信 Bot: ${botId}`); }; const handleConnectOpenClaw = async () => { try { await window.electronAPI.connectOpenClaw(config.openclaw); addLog('info', '正在连接 OpenClaw Gateway'); } catch (error) { addLog('error', `连接失败:${error.message}`); } }; const handleDisconnectOpenClaw = async () => { await window.electronAPI.disconnectOpenClaw(); addLog('info', '断开 OpenClaw Gateway'); }; const handleSaveOpenClawConfig = async () => { try { await saveConfig(); addLog('success', 'OpenClaw 配置已保存'); // 如果已连接,重新连接以应用新配置 if (connectionStatus.openclaw.connected) { addLog('info', '重新连接以应用新配置...'); await handleDisconnectOpenClaw(); setTimeout(() => handleConnectOpenClaw(), 500); } } catch (error) { addLog('error', `保存失败:${error.message}`); } }; const handleSendTestOpenClawMessage = async () => { if (!testOpenClawMessage.trim()) { setTestOpenClawResult({ success: false, message: '请输入测试消息内容' }); return; } if (!connectionStatus.openclaw.connected) { setTestOpenClawResult({ success: false, message: 'OpenClaw 未连接,请先连接 Gateway' }); return; } try { const result = await window.electronAPI.sendTestOpenClawMessage(testOpenClawMessage); setTestOpenClawResult(result); addLog(result.success ? 'success' : 'error', result.message); if (result.success) { setTestOpenClawMessage(''); } } catch (error) { setTestOpenClawResult({ success: false, message: `发送失败:${error.message}` }); addLog('error', `测试消息发送失败:${error.message}`); } }; const handleSendTestMessage = async () => { if (!testMessage.botId || !testMessage.chatId || !testMessage.text) { addLog('warning', '请填写完整信息'); return; } try { const result = await window.electronAPI.sendTestMessage( testMessage.botId, testMessage.chatId, testMessage.text ); if (result.success) { addLog('success', `测试消息已发送到 ${testMessage.chatId}`); setShowTestMessage(false); setTestMessage({ botId: '', chatId: '', text: '' }); } else { addLog('error', `发送失败:${result.error}`); } } catch (error) { addLog('error', `发送失败:${error.message}`); } }; const getStatus = (botId) => { return connectionStatus.wecom[botId]?.connected ? 'connected' : 'disconnected'; }; return (
企业微信智能机器人 - 双向消息桥接 v1.0.0-fix7
暂无配置的机器人
Bot ID: {bot.botId}
{bot.enabled && ● 自动连接}