171 lines
4.3 KiB
JavaScript
171 lines
4.3 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* WeChat-Nanobot Bridge Adapter
|
||
* 将 weixin-agent-sdk 连接到 nanobot bridge
|
||
*/
|
||
|
||
const axios = require('axios');
|
||
|
||
// 配置
|
||
// 默认使用 localhost,如需外网访问请改为服务器公网 IP
|
||
// 例如: http://129.211.173.87:5000/chat
|
||
const NANOBOT_BRIDGE_URL = process.env.NANOBOT_BRIDGE_URL || 'http://127.0.0.1:5000/chat';
|
||
const CONNECT_TIMEOUT = 5000;
|
||
const REQUEST_TIMEOUT = 70000;
|
||
|
||
console.log('🔌 WeChat-Nanobot Bridge Adapter');
|
||
console.log(' 桥接地址:', NANOBOT_BRIDGE_URL);
|
||
|
||
// 模拟 Agent 接口(实际使用时替换为 weixin-agent-sdk)
|
||
class NanobotAgent {
|
||
constructor() {
|
||
this.conversations = new Map();
|
||
}
|
||
|
||
async chat(request) {
|
||
const { conversationId, text, media } = request;
|
||
|
||
console.log(`\n📨 收到消息 [${conversationId}]: ${text.substring(0, 50)}...`);
|
||
|
||
try {
|
||
const response = await axios.post(
|
||
NANOBOT_BRIDGE_URL,
|
||
{
|
||
conversationId,
|
||
text,
|
||
media
|
||
},
|
||
{
|
||
timeout: REQUEST_TIMEOUT,
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
}
|
||
}
|
||
);
|
||
|
||
console.log(`📤 回复: ${response.data.text.substring(0, 50)}...`);
|
||
|
||
return {
|
||
text: response.data.text,
|
||
media: response.data.media || null
|
||
};
|
||
|
||
} catch (error) {
|
||
console.error('❌ 调用失败:', error.message);
|
||
|
||
if (error.code === 'ECONNREFUSED') {
|
||
return {
|
||
text: '⚠️ 服务未启动,请稍后重试'
|
||
};
|
||
}
|
||
|
||
if (error.code === 'ETIMEDOUT') {
|
||
return {
|
||
text: '⏱️ 处理超时,请简化问题后重试'
|
||
};
|
||
}
|
||
|
||
return {
|
||
text: '❌ 服务暂时不可用'
|
||
};
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查 bridge 是否可用
|
||
async function checkBridge() {
|
||
try {
|
||
const healthUrl = NANOBOT_BRIDGE_URL.replace('/chat', '/health');
|
||
const response = await axios.get(healthUrl, {
|
||
timeout: CONNECT_TIMEOUT
|
||
});
|
||
|
||
if (response.data.status === 'ok') {
|
||
console.log('✅ Bridge 服务正常');
|
||
return true;
|
||
}
|
||
} catch (error) {
|
||
console.error('❌ Bridge 连接失败:', error.message);
|
||
console.log('\n请确保:');
|
||
console.log(' 1. Bridge 服务已启动: ./scripts/start.sh');
|
||
console.log(' 2. 地址正确:', NANOBOT_BRIDGE_URL);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 模拟运行(测试用)
|
||
async function simulate() {
|
||
console.log('\n🧪 模拟运行模式\n');
|
||
|
||
const agent = new NanobotAgent();
|
||
const testConversationId = 'test_user_123';
|
||
|
||
// 测试消息
|
||
const testMessages = [
|
||
'你好',
|
||
'能帮我查一下今天的天气吗',
|
||
'谢谢!'
|
||
];
|
||
|
||
for (const message of testMessages) {
|
||
console.log(`\n--- 测试消息 ---`);
|
||
const response = await agent.chat({
|
||
conversationId: testConversationId,
|
||
text: message
|
||
});
|
||
|
||
console.log(`回复: ${response.text}`);
|
||
|
||
// 模拟延迟
|
||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||
}
|
||
|
||
console.log('\n✅ 模拟测试完成');
|
||
}
|
||
|
||
// 实际运行(需要 weixin-agent-sdk)
|
||
async function runWithWeixinSDK() {
|
||
try {
|
||
const { login, start } = require('weixin-agent-sdk');
|
||
|
||
const agent = new NanobotAgent();
|
||
|
||
console.log('\n📱 准备登录微信...');
|
||
await login();
|
||
|
||
console.log('🚀 启动消息处理...');
|
||
await start(agent);
|
||
|
||
} catch (error) {
|
||
console.error('❌ weixin-agent-sdk 未安装:', error.message);
|
||
console.log('\n请安装:');
|
||
console.log(' npm install weixin-agent-sdk');
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
async function main() {
|
||
const args = process.argv.slice(2);
|
||
|
||
// 检查 bridge
|
||
if (!await checkBridge()) {
|
||
process.exit(1);
|
||
}
|
||
|
||
// 根据参数选择模式
|
||
if (args.includes('--simulate') || args.includes('-s')) {
|
||
await simulate();
|
||
} else if (args.includes('--weixin') || args.includes('-w')) {
|
||
await runWithWeixinSDK();
|
||
} else {
|
||
console.log('\n使用方法:');
|
||
console.log(' node nanobot-adapter.js --simulate # 模拟测试');
|
||
console.log(' node nanobot-adapter.js --weixin # 连接微信(需要 weixin-agent-sdk)');
|
||
console.log('\n环境变量:');
|
||
console.log(' NANOBOT_BRIDGE_URL=http://localhost:5000/chat');
|
||
}
|
||
}
|
||
|
||
main().catch(console.error);
|